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