1 15 package org.apache.tapestry.form.validator; 16 17 import org.apache.hivemind.util.PropertyUtils; 18 import org.apache.tapestry.IMarkupWriter; 19 import org.apache.tapestry.IRequestCycle; 20 import org.apache.tapestry.form.FormComponentContributorContext; 21 import org.apache.tapestry.form.IFormComponent; 22 import org.apache.tapestry.form.ValidationMessages; 23 import org.apache.tapestry.valid.ValidationConstraint; 24 import org.apache.tapestry.valid.ValidationStrings; 25 import org.apache.tapestry.valid.ValidatorException; 26 27 33 public class Required extends BaseValidator 34 { 35 public Required() 36 { 37 } 38 39 public Required(String initializer) 40 { 41 super(initializer); 42 } 43 44 public boolean getAcceptsNull() 45 { 46 return true; 47 } 48 49 public void validate(IFormComponent field, ValidationMessages messages, Object object) 50 throws ValidatorException 51 { 52 if (object == null) 53 { 54 String message = buildMessage(messages, field); 55 throw new ValidatorException(message, ValidationConstraint.REQUIRED); 56 } 57 } 58 59 private String buildMessage(ValidationMessages messages, IFormComponent field) 60 { 61 return messages.formatValidationMessage( 62 getMessage(), 63 ValidationStrings.REQUIRED_TEXT_FIELD, 64 new Object [] 65 { field.getDisplayName() }); 66 } 67 68 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, 69 FormComponentContributorContext context, IFormComponent field) 70 { 71 StringBuffer buffer = new StringBuffer ("function(event) { require(event, "); 72 buffer.append(context.getFieldDOM()); 73 buffer.append(", '"); 74 buffer.append(buildMessage(context, field)); 75 buffer.append("'); }"); 76 77 context.addSubmitListener(buffer.toString()); 78 } 79 80 } 81 | Popular Tags |