1 15 package org.apache.tapestry.form.validator; 16 17 import org.apache.tapestry.IMarkupWriter; 18 import org.apache.tapestry.IRequestCycle; 19 import org.apache.tapestry.form.FormComponentContributorContext; 20 import org.apache.tapestry.form.IFormComponent; 21 import org.apache.tapestry.form.ValidationMessages; 22 import org.apache.tapestry.valid.ValidationConstraint; 23 import org.apache.tapestry.valid.ValidationStrings; 24 import org.apache.tapestry.valid.ValidatorException; 25 26 32 public class MaxLength extends BaseValidator 33 { 34 private int _maxLength; 35 36 public MaxLength() 37 { 38 39 } 40 41 public MaxLength(String initializer) 42 { 43 super(initializer); 44 } 45 46 public void setMaxLength(int maxLength) 47 { 48 _maxLength = maxLength; 49 } 50 51 public void validate(IFormComponent field, ValidationMessages messages, Object object) 52 throws ValidatorException 53 { 54 String string = (String ) object; 55 56 if (string.length() > _maxLength) 57 throw new ValidatorException(buildMessage(messages, field), 58 ValidationConstraint.MAXIMUM_WIDTH); 59 } 60 61 protected String buildMessage(ValidationMessages messages, IFormComponent field) 62 { 63 return messages.formatValidationMessage( 64 getMessage(), 65 ValidationStrings.VALUE_TOO_LONG, 66 new Object [] 67 { new Integer (_maxLength), field.getDisplayName() }); 68 } 69 70 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, 71 FormComponentContributorContext context, IFormComponent field) 72 { 73 context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js"); 74 75 StringBuffer buffer = new StringBuffer ("function(event) { validate_max_length(event, "); 76 buffer.append(context.getFieldDOM()); 77 buffer.append(", "); 78 buffer.append(_maxLength); 79 buffer.append(", '"); 80 buffer.append(buildMessage(context, field)); 81 buffer.append("'); }"); 82 83 context.addSubmitListener(buffer.toString()); 84 } 85 86 } 87 | Popular Tags |