1 15 package org.apache.tapestry.form; 16 17 import java.text.MessageFormat ; 18 19 import org.apache.hivemind.HiveMind; 20 import org.apache.tapestry.IForm; 21 import org.apache.tapestry.IMarkupWriter; 22 import org.apache.tapestry.IRequestCycle; 23 import org.apache.tapestry.valid.ValidationConstraint; 24 import org.apache.tapestry.valid.ValidatorException; 25 26 33 public class RequirableFieldSupportImpl implements RequirableFieldSupport 34 { 35 38 public void render(RequirableField component, IMarkupWriter writer, IRequestCycle cycle) 39 { 40 IForm form = component.getForm(); 41 42 if (component.isRequired() && form.isClientValidationEnabled()) 43 { 44 String function = "require(event, document." + form.getName() + "." + component.getName() + ",'" + buildRequiredMessage(component) + "')"; 45 46 form.addEventHandler(FormEventType.SUBMIT, function); 47 } 48 } 49 50 53 public void rewind(RequirableField component, IMarkupWriter writer, IRequestCycle cycle) 54 { 55 try 56 { 57 String value = component.getSubmittedValue(cycle); 58 59 if (component.isRequired() && HiveMind.isBlank(value)) 60 { 61 throw new ValidatorException(buildRequiredMessage(component), ValidationConstraint.REQUIRED); 62 } 63 64 component.bind(writer, cycle); 65 } 66 catch (ValidatorException e) 67 { 68 component.getForm().getDelegate().record(e); 69 } 70 } 71 72 protected String buildRequiredMessage(RequirableField component) 73 { 74 return MessageFormat.format(component.getRequiredMessage(), new Object [] { component.getDisplayName() }); 75 } 76 } 77 | Popular Tags |