Validator in JSF
Validator in JSF
If your application collects information from users, for example a login and a password, then it is important that you verify the user data. The IDE provides a set of components for validating user input. These validators, which you access from the Validators section of the Components Palette, are as follows: Double Range Validator. Checks whether the local value of a component is within a certain range. The value must be floating-point or convertible to floating-point. Length Validator. Checks whether the length of a component's local value is within a certain range. The value must be a java.lang.String. Long Range Validator. Checks whether the local value of a component is within a certain range. The value must be any numeric type or String that can be converted to a long. You can use more than one validator on an input component to validate for different criteria. When you use a validator, your component also needs a message component to indicate when the validation fails.
2. From the Basic section of the Components Palette, drag a Text Field component and drop it on the page. Set the label property to Celsius and the text property to 0.0. The label and text properties are under the Appearance section of the Properties window. The text value is the default value displayed at runtime.
3. Place a Label component on the page. Set the text of the label to Fahrenheit.
Validator in JSF
4. Place a Static Text component to the right of the Fahrenheit label. Set the text property to 32.0.
Requiring a Value
The simplest validation ensures that an input field has some sort of value. For this application, you want to check that the Text Field component contains at least one character before the page is submitted. You also need a message component to indicate when the validation fails. 1. Select the Text Field component and set its required property to True by selecting the checkbox in the Properties window. The required property is under the Data section of the Properties window. A value of True requires that the user enter a value for the input field. If the user does not enter a value and tries to submit the page, a standard validation error message is returned. A red asterisk appears next to the Celsius label to indicate that the required property is set.
2. From the Basic section of the Palette, drag a Message component onto the page and drop
it to the right of the Text Field component.
3. Hold down the Ctrl+Shift keys and drag a line from the Message component to the Text
Field. The text of the Message component changes to Message summary for textField1, as shown in Figure 2. 2
non-visual component. The default value doubleRangeValidator1 appears in the Outline window and in the validator property in the Properties window, as shown in Figure 3.
2. In the Outline window, select doubleRangeValidator1. 3. Set the range for the validator in the Properties window: a. Set the maximum property to 1000.0 b. Set the minimum property to -273.15 4. Open the Converters section of the Palette. Drag the Double Converter from the Palette and drop it on the Text Field component. This converter specifies that the Text Field component returns a Double object rather than a String. The default value doubleConverter1 appears in the Properties window and in the Outline window.
Adding Code
1. Double-click the Text Field component to open its source code in the Java Editor. 2. Add the following code (shown in bold) to the textField1_processValueChange action method.
Validator in JSF
3. Enter various numbers to test the application. Figure 5 shows the results when you enter 100
Validator in JSF
4. Verify that the validation error displays when you enter a value outside the range and the conversion error displays when you enter a String value. Figure 6 shows the result when you enter -1000. Note that when a validation or conversion error occurs, the value change listener method is not called and the value in the Static Text component does not change.
Validator in JSF