001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.form.validator; 016 017 import java.util.Collection; 018 019 import org.apache.tapestry.IMarkupWriter; 020 import org.apache.tapestry.IRequestCycle; 021 import org.apache.tapestry.form.FormComponentContributorContext; 022 import org.apache.tapestry.form.IFormComponent; 023 import org.apache.tapestry.form.ValidationMessages; 024 import org.apache.tapestry.json.JSONObject; 025 import org.apache.tapestry.multipart.UploadPart; 026 import org.apache.tapestry.valid.ValidationConstants; 027 import org.apache.tapestry.valid.ValidationConstraint; 028 import org.apache.tapestry.valid.ValidationStrings; 029 import org.apache.tapestry.valid.ValidatorException; 030 031 /** 032 * {@link org.apache.tapestry.form.validator.Validator} that ensures a value was supplied. 033 * 034 * @author Howard Lewis Ship 035 * @since 4.0 036 */ 037 public class Required extends BaseValidator 038 { 039 public Required() 040 { 041 } 042 043 public Required(String initializer) 044 { 045 super(initializer); 046 } 047 048 public boolean getAcceptsNull() 049 { 050 return true; 051 } 052 053 public void validate(IFormComponent field, ValidationMessages messages, Object object) 054 throws ValidatorException 055 { 056 if (field.isDisabled()) 057 return; 058 059 if ((object == null) 060 || (String.class.isInstance(object) && (((String) object).length() == 0)) 061 || (Collection.class.isInstance(object) && ((Collection) object).isEmpty()) 062 || (UploadPart.class.isInstance(object) && ((UploadPart) object).getSize() < 1)) 063 { 064 String message = buildMessage(messages, field); 065 throw new ValidatorException(message, ValidationConstraint.REQUIRED); 066 } 067 } 068 069 public String buildMessage(ValidationMessages messages, IFormComponent field) 070 { 071 return messages.formatValidationMessage( 072 getMessage(), 073 ValidationStrings.REQUIRED_FIELD, 074 new Object[] 075 { field.getDisplayName() }); 076 } 077 078 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, 079 FormComponentContributorContext context, IFormComponent field) 080 { 081 if(field.isDisabled()) 082 return; 083 084 context.registerForFocus(ValidationConstants.REQUIRED_FIELD); 085 086 JSONObject profile = context.getProfile(); 087 088 accumulateProperty(profile, ValidationConstants.REQUIRED, field.getClientId()); 089 090 accumulateProfileProperty(field, profile, 091 ValidationConstants.REQUIRED, buildMessage(context, field)); 092 } 093 094 /** 095 * Returns true, that's what Required means! 096 */ 097 public boolean isRequired() 098 { 099 return true; 100 } 101 }