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.util.RegexpMatcher; 23 import org.apache.tapestry.valid.ValidationConstraint; 24 import org.apache.tapestry.valid.ValidationStrings; 25 import org.apache.tapestry.valid.ValidatorException; 26 27 34 public class Pattern extends BaseValidator 35 { 36 private RegexpMatcher _matcher = new RegexpMatcher(); 40 41 private String _pattern; 42 43 public Pattern() 44 { 45 } 46 47 public Pattern(String initializer) 48 { 49 super(initializer); 50 } 51 52 public void validate(IFormComponent field, ValidationMessages messages, Object object) 53 throws ValidatorException 54 { 55 String input = (String ) object; 56 57 if (!_matcher.matches(_pattern, input)) 58 throw new ValidatorException(buildMessage(messages, field), 59 ValidationConstraint.PATTERN_MISMATCH); 60 } 61 62 private String buildMessage(ValidationMessages messages, IFormComponent field) 63 { 64 return messages.formatValidationMessage( 65 getMessage(), 66 ValidationStrings.REGEX_MISMATCH, 67 new Object [] 68 { field.getDisplayName() }); 69 } 70 71 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, 72 FormComponentContributorContext context, IFormComponent field) 73 { 74 context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js"); 75 76 String pattern = _matcher.getEscapedPatternString(_pattern); 77 String message = buildMessage(context, field); 78 79 StringBuffer buffer = new StringBuffer ("function(event) { validate_regexp(event, "); 80 buffer.append(context.getFieldDOM()); 81 buffer.append(", '"); 82 buffer.append(pattern); 83 buffer.append("', '"); 84 buffer.append(message); 85 buffer.append("'); }"); 86 87 context.addSubmitListener(buffer.toString()); 88 } 89 90 public void setPattern(String pattern) 91 { 92 _pattern = pattern; 93 } 94 95 } 96 | Popular Tags |