001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.valid;
016
017 import org.apache.tapestry.IMarkupWriter;
018 import org.apache.tapestry.IRequestCycle;
019 import org.apache.tapestry.Tapestry;
020 import org.apache.tapestry.form.AbstractFormComponent;
021 import org.apache.tapestry.form.Form;
022
023 /**
024 * A {@link Form}component that creates a text field that allows for validation
025 * of user input and conversion between string and object values.
026 * <p>
027 * A ValidatingTextField uses an {@link IValidationDelegate} to track errors and
028 * an {@link IValidator}to convert between strings and objects (as well as
029 * perform validations). The validation delegate is shared by all validating
030 * text fields in a form, the validator may be shared my multiple elements as
031 * desired.
032 *
033 * @author Howard Lewis Ship
034 */
035
036 public abstract class ValidField extends AbstractFormComponent
037 {
038
039 public abstract boolean isHidden();
040
041 public abstract boolean isDisabled();
042
043 public abstract Object getValue();
044
045 public abstract void setValue(Object value);
046
047 /** Parameter. */
048
049 public abstract String getDisplayName();
050
051 /**
052 * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter,
053 * org.apache.tapestry.IRequestCycle)
054 */
055 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
056 {
057 IValidationDelegate delegate = getForm().getDelegate();
058
059 delegate.registerForFocus(this,
060 delegate.isInError() ? ValidationConstants.ERROR_FIELD
061 : ValidationConstants.NORMAL_FIELD);
062
063 delegate.writePrefix(writer, cycle, this, null);
064
065 writer.beginEmpty("input");
066
067 writer.attribute("type", isHidden() ? "password" : "text");
068
069 if (isDisabled()) writer.attribute("disabled", "disabled");
070
071 writer.attribute("name", getName());
072
073 String value = readValue();
074 if (value != null) writer.attribute("value", value);
075
076 renderIdAttribute(writer, cycle);
077
078 renderInformalParameters(writer, cycle);
079
080 delegate.writeAttributes(writer, cycle, this, null);
081
082 IValidator validator = getValidator();
083
084 if (validator == null)
085 throw Tapestry.createRequiredParameterException(this, "validator");
086
087 if (validator.isRequired())
088 delegate.registerForFocus(this, ValidationConstants.REQUIRED_FIELD);
089
090 validator.renderValidatorContribution(this, writer, cycle);
091
092 writer.closeTag();
093
094 delegate.writeSuffix(writer, cycle, this, null);
095 }
096
097 /**
098 * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter,
099 * org.apache.tapestry.IRequestCycle)
100 */
101 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
102 {
103 String value = cycle.getParameter(getName());
104
105 updateValue(value);
106 }
107
108 protected String readValue()
109 {
110 IValidator validator = getValidator();
111 if (validator == null)
112 throw Tapestry.createRequiredParameterException(this, "validator");
113
114 IValidationDelegate delegate = getForm().getDelegate();
115
116 if (delegate.isInError()) return delegate.getFieldInputValue();
117
118 Object value = getValue();
119
120 String result = validator.toString(this, value);
121
122 return result;
123 }
124
125 protected void updateValue(String value)
126 {
127 Object objectValue = null;
128
129 IValidator validator = getValidator();
130 if (validator == null)
131 throw Tapestry.createRequiredParameterException(this, "validator");
132
133 IValidationDelegate delegate = getForm().getDelegate();
134
135 delegate.recordFieldInputValue(value);
136
137 try
138 {
139 objectValue = validator.toObject(this, value);
140 }
141 catch (ValidatorException ex)
142 {
143 delegate.record(ex);
144 return;
145 }
146
147 setValue(objectValue);
148 }
149
150 public abstract IValidator getValidator();
151 }