1 15 package org.apache.tapestry.form; 16 17 import java.util.Iterator ; 18 import java.util.Locale ; 19 20 import org.apache.hivemind.HiveMind; 21 import org.apache.hivemind.service.ThreadLocale; 22 import org.apache.tapestry.IMarkupWriter; 23 import org.apache.tapestry.IRequestCycle; 24 import org.apache.tapestry.coerce.ValueConverter; 25 import org.apache.tapestry.form.validator.Validator; 26 import org.apache.tapestry.valid.IValidationDelegate; 27 import org.apache.tapestry.valid.ValidatorException; 28 29 36 public class ValidatableFieldSupportImpl implements ValidatableFieldSupport 37 { 38 private ValueConverter _converter; 39 40 private ThreadLocale _threadLocale; 41 42 public void setValueConverter(ValueConverter converter) 43 { 44 _converter = converter; 45 } 46 47 public void setThreadLocale(ThreadLocale threadLocale) 48 { 49 _threadLocale = threadLocale; 50 } 51 52 protected Iterator getValidatorsIterator(ValidatableField component) 53 { 54 return (Iterator ) _converter.coerceValue(component.getValidators(), Iterator .class); 55 } 56 57 61 public void render(ValidatableField component, IMarkupWriter writer, IRequestCycle cycle) 62 { 63 IValidationDelegate delegate = component.getForm().getDelegate(); 64 65 String value = delegate.isInError() ? delegate.getFieldInputValue() 66 : getTranslatedValue(component); 67 68 component.render(writer, cycle, value); 69 } 70 71 75 public void renderContributions(ValidatableField component, IMarkupWriter writer, 76 IRequestCycle cycle) 77 { 78 if (component.getForm().isClientValidationEnabled()) 79 { 80 Locale locale = component.getPage().getLocale(); 81 FormComponentContributorContext context = new FormComponentContributorContextImpl( 82 locale, cycle, component); 83 84 component.getTranslator().renderContribution(writer, cycle, context, component); 85 86 Iterator validators = getValidatorsIterator(component); 87 88 while (validators.hasNext()) 89 { 90 Validator validator = (Validator) validators.next(); 91 92 validator.renderContribution(writer, cycle, context, component); 93 } 94 } 95 } 96 97 protected String getTranslatedValue(ValidatableField component) 98 { 99 Object value = component.readValue(); 100 101 return (value != null) ? component.getTranslator().format(component, value) : ""; 102 } 103 104 108 public void bind(ValidatableField component, IMarkupWriter writer, IRequestCycle cycle, 109 String value) 110 { 111 IValidationDelegate delegate = component.getForm().getDelegate(); 112 113 delegate.recordFieldInputValue(value); 114 115 try 116 { 117 Object object = HiveMind.isBlank(value) ? null : component.getTranslator().parse( 118 component, 119 value); 120 121 boolean isNonNull = object != null; 122 123 Iterator validators = getValidatorsIterator(component); 124 125 ValidationMessages messages = new ValidationMessagesImpl(_threadLocale.getLocale()); 126 127 while (validators.hasNext()) 128 { 129 Validator validator = (Validator) validators.next(); 130 131 if (isNonNull || validator.getAcceptsNull()) 132 validator.validate(component, messages, object); 133 } 134 135 137 140 component.writeValue(object); 141 } 142 catch (ValidatorException ex) 143 { 144 delegate.record(ex); 145 } 146 } 147 } 148 | Popular Tags |