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.NumberValidator;
019 import org.apache.tapestry.valid.ValidField;
020
021 /**
022 * Backwards compatible version of the 1.0.7 NumericField component. <table
023 * 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>value</td>
034 * <td>{@link Number}</td>
035 * <td>R / W</td>
036 * <td>yes</td>
037 * <td> </td>
038 * <td>The value to be updated.
039 * <p>
040 * When the form is submitted, this parameter is only updated if the value is
041 * valid.
042 * <p>
043 * When rendering, a null value will render as the empty string. A value of zero
044 * will render normally.
045 * <p>
046 * When the form is submitted, the type of the binding is used to determine what
047 * kind of object to convert the string to.</td>
048 * </tr>
049 * <tr>
050 * <td>minimum</td>
051 * <td>{@link Number}</td>
052 * <td>R</td>
053 * <td>no</td>
054 * <td> </td>
055 * <td>The minimum value accepted for the field.</td>
056 * </tr>
057 * <tr>
058 * <td>maximum</td>
059 * <td>{@link Number}</td>
060 * <td>R</td>
061 * <td>no</td>
062 * <td> </td>
063 * <td>The maximum value accepted for the field.</td>
064 * </tr>
065 * <tr>
066 * <td>required</td>
067 * <td>boolean</td>
068 * <td>R</td>
069 * <td>no</td>
070 * <td>false</td>
071 * <td>If true, then a non-null value must be provided. If the field is not
072 * required, and a null (all whitespace) value is supplied in the field, then
073 * the value parameter is <em>not</em> updated.</td>
074 * </tr>
075 * <tr>
076 * <td>displayName</td>
077 * <td>String</td>
078 * <td>R</td>
079 * <td>yes</td>
080 * <td> </td>
081 * <td>A textual name for the field that is used when formulating error
082 * messages.</td>
083 * </tr>
084 * <tr>
085 * <td>type</td>
086 * <td>String</td>
087 * <td>R</td>
088 * <td>yes</td>
089 * <td> </td>
090 * <td>The class name used to convert the value entered. See
091 * {@link NumberValidator#setValueType(String)}</td>
092 * </tr>
093 * </table>
094 * <p>
095 * May not contain a body. May have informal parameters.
096 *
097 * @author Howard Lewis Ship
098 * @since 1.0.8
099 * @see ValidField
100 */
101
102 public abstract class NumericField extends ValidField
103 {
104
105 public abstract Number getMinimum();
106
107 public abstract Number getMaximum();
108
109 public abstract boolean isRequired();
110
111 public abstract String getType();
112
113 /**
114 * Overrides {@link ValidField#getValidator()}to construct a validator on
115 * the fly.
116 */
117
118 public IValidator getValidator()
119 {
120 NumberValidator validator = new NumberValidator();
121
122 if (isParameterBound("minimum")) validator.setMinimum(getMinimum());
123
124 if (isParameterBound("maximum")) validator.setMaximum(getMaximum());
125
126 if (isParameterBound("required")) validator.setRequired(isRequired());
127
128 if (isParameterBound("type")) validator.setValueType(getType());
129
130 return validator;
131 }
132 }