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