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.contrib.valid;
016    
017    import org.apache.tapestry.valid.IValidator;
018    import org.apache.tapestry.valid.StringValidator;
019    import org.apache.tapestry.valid.ValidField;
020    
021    /**
022     * Backwards compatible version of the 1.0.7 ValidatingTextField component. <table border=1>
023     * <tr>
024     * <td>Parameter</td>
025     * <td>Type</td>
026     * <td>Read / Write</td>
027     * <td>Required</td>
028     * <td>Default</td>
029     * <td>Description</td>
030     * </tr>
031     * <tr>
032     * <td>text</td>
033     * <td>java.lang.String</td>
034     * <td>R / W</td>
035     * <td>yes</td>
036     * <td>&nbsp;</td>
037     * <td>The text inside the text field.
038     * <p>
039     * When the form is submitted, the binding is only updated if the value is valid.</td>
040     * </tr>
041     * <tr>
042     * <td>minimumLength</td>
043     * <td>int</td>
044     * <td>R</td>
045     * <td>no</td>
046     * <td>0</td>
047     * <td>The minimum length (number of characters read) for the field. The value provided in the
048     * request is trimmed of leading and trailing whitespace.
049     * <p>
050     * If a field is not required and no value is given, then minimumLength is ignored. Minimum length
051     * only applies if <em>some</em> non-null value is given.</td>
052     * </tr>
053     * <tr>
054     * <td>required</td>
055     * <td>boolean</td>
056     * <td>R</td>
057     * <td>no</td>
058     * <td>false</td>
059     * <td>If true, then a non-null value must be provided. A value consisting only of whitespace is
060     * considered null.</td>
061     * </tr>
062     * <tr>
063     * <td>displayName</td>
064     * <td>String</td>
065     * <td>R</td>
066     * <td>yes</td>
067     * <td>&nbsp;</td>
068     * <td>A textual name for the field that is used when formulating error messages.</td>
069     * </tr>
070     * </table>
071     * <p>
072     * May not have a body. May have informal parameters.
073     * 
074     * @author Howard Lewis Ship
075     * @since 1.0.8
076     * @see org.apache.tapestry.valid.ValidField
077     */
078    
079    public abstract class ValidatingTextField extends ValidField
080    {
081        public abstract int getMinimumLength();
082    
083        public abstract boolean isRequired();
084    
085        public abstract String getText();
086    
087        public abstract void setText(String value);
088    
089        /*
090         * (non-Javadoc)
091         * 
092         * @see org.apache.tapestry.valid.ValidField#getValue()
093         */
094        public Object getValue()
095        {
096            return getText();
097        }
098    
099        /*
100         * (non-Javadoc)
101         * 
102         * @see org.apache.tapestry.valid.ValidField#setValue(java.lang.Object)
103         */
104        public void setValue(Object value)
105        {
106            setText((String) value);
107        }
108    
109        /**
110         * Overrides {@link ValidField#getValidator()}to construct a validator on the fly.
111         */
112        public IValidator getValidator()
113        {
114            StringValidator validator = new StringValidator();
115    
116            if (isParameterBound("required"))
117                validator.setRequired(isRequired());
118    
119            if (isParameterBound("minimumLength"))
120                validator.setMinimumLength(getMinimumLength());
121    
122            return validator;
123        }
124    }