1 15 package org.apache.tapestry.valid; 16 17 import org.apache.tapestry.IMarkupWriter; 18 import org.apache.tapestry.IRequestCycle; 19 import org.apache.tapestry.Tapestry; 20 import org.apache.tapestry.form.AbstractFormComponent; 21 import org.apache.tapestry.form.Form; 22 23 35 36 public abstract class ValidField extends AbstractFormComponent 37 { 38 public abstract boolean isHidden(); 39 40 public abstract boolean isDisabled(); 41 42 public abstract Object getValue(); 43 44 public abstract void setValue(Object value); 45 46 47 48 public abstract String getDisplayName(); 49 50 54 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) 55 { 56 IValidationDelegate delegate = getForm().getDelegate(); 57 58 delegate.writePrefix(writer, cycle, this, null); 59 60 writer.beginEmpty("input"); 61 62 writer.attribute("type", isHidden() ? "password" : "text"); 63 64 if (isDisabled()) 65 writer.attribute("disabled", "disabled"); 66 67 writer.attribute("name", getName()); 68 69 String value = readValue(); 70 if (value != null) 71 writer.attribute("value", value); 72 73 renderIdAttribute(writer, cycle); 74 75 renderInformalParameters(writer, cycle); 76 77 delegate.writeAttributes(writer, cycle, this, null); 78 79 IValidator validator = getValidator(); 80 81 if (validator == null) 82 throw Tapestry.createRequiredParameterException(this, "validator"); 83 84 validator.renderValidatorContribution(this, writer, cycle); 85 86 writer.closeTag(); 87 88 delegate.writeSuffix(writer, cycle, this, null); 89 } 90 91 95 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) 96 { 97 String value = cycle.getParameter(getName()); 98 99 updateValue(value); 100 } 101 102 protected String readValue() 103 { 104 IValidator validator = getValidator(); 105 if (validator == null) 106 throw Tapestry.createRequiredParameterException(this, "validator"); 107 108 IValidationDelegate delegate = getForm().getDelegate(); 109 110 if (delegate.isInError()) 111 return delegate.getFieldInputValue(); 112 113 Object value = getValue(); 114 115 String result = validator.toString(this, value); 116 117 return result; 118 } 119 120 protected void updateValue(String value) 121 { 122 Object objectValue = null; 123 124 IValidator validator = getValidator(); 125 if (validator == null) 126 throw Tapestry.createRequiredParameterException(this, "validator"); 127 128 IValidationDelegate delegate = getForm().getDelegate(); 129 130 delegate.recordFieldInputValue(value); 131 132 try 133 { 134 objectValue = validator.toObject(this, value); 135 } 136 catch (ValidatorException ex) 137 { 138 delegate.record(ex); 139 return; 140 } 141 142 setValue(objectValue); 143 } 144 145 public abstract IValidator getValidator(); 146 } | Popular Tags |