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.TapestryUtils; 022 import org.apache.tapestry.form.FormComponentContributorContext; 023 import org.apache.tapestry.form.IFormComponent; 024 import org.apache.tapestry.form.ValidationMessages; 025 import org.apache.tapestry.valid.ValidationConstants; 026 import org.apache.tapestry.valid.ValidationConstraint; 027 import org.apache.tapestry.valid.ValidationStrings; 028 import org.apache.tapestry.valid.ValidatorException; 029 030 /** 031 * {@link org.apache.tapestry.form.validator.Validator} that ensures a value was supplied. 032 * 033 * @author Howard Lewis Ship 034 * @since 4.0 035 */ 036 public class Required extends BaseValidator 037 { 038 public Required() 039 { 040 } 041 042 public Required(String initializer) 043 { 044 super(initializer); 045 } 046 047 public boolean getAcceptsNull() 048 { 049 return true; 050 } 051 052 public void validate(IFormComponent field, ValidationMessages messages, Object object) 053 throws ValidatorException 054 { 055 if ((object == null) 056 || (String.class.isInstance(object) && (((String) object).length() == 0)) 057 || (Collection.class.isInstance(object) && ((Collection) object).isEmpty())) 058 { 059 String message = buildMessage(messages, field); 060 throw new ValidatorException(message, ValidationConstraint.REQUIRED); 061 } 062 } 063 064 private String buildMessage(ValidationMessages messages, IFormComponent field) 065 { 066 return messages.formatValidationMessage( 067 getMessage(), 068 ValidationStrings.REQUIRED_FIELD, 069 new Object[] 070 { field.getDisplayName() }); 071 } 072 073 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, 074 FormComponentContributorContext context, IFormComponent field) 075 { 076 context.registerForFocus(ValidationConstants.REQUIRED_FIELD); 077 078 StringBuffer buffer = new StringBuffer("function(event) { Tapestry.require_field(event, '"); 079 buffer.append(field.getClientId()); 080 buffer.append("', "); 081 buffer.append(TapestryUtils.enquote(buildMessage(context, field))); 082 buffer.append("); }"); 083 084 context.addSubmitHandler(buffer.toString()); 085 } 086 087 /** 088 * Returns true, that's what Required means! 089 */ 090 public boolean isRequired() 091 { 092 return true; 093 } 094 }