0% found this document useful (0 votes)
26 views

Validator in JSF

Validators in JavaServer Faces verify user input data. This document describes how to create a simple application that uses a double range validator to validate a Celsius temperature field is within a valid numeric range. It involves adding a text field, validator, converter, and message component then writing code to process value changes and display errors. The validator ensures the input is between -273.15 and 1000, and the message notifies the user of invalid data.

Uploaded by

vinh_kaka
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Validator in JSF

Validators in JavaServer Faces verify user input data. This document describes how to create a simple application that uses a double range validator to validate a Celsius temperature field is within a valid numeric range. It involves adding a text field, validator, converter, and message component then writing code to process value changes and display errors. The validator ensures the input is between -273.15 and 1000, and the message notifies the user of invalid data.

Uploaded by

vinh_kaka
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

What Is a Validator

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.

Designing the Application


1. Create a new web application project and name it SimpleValidationExample. Figure 1 shows the page you will create in the following steps.

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

By Rahul Kumar Page 1 of 5

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

Using the Double Range Validator


The Double Range validator tests whether the value of an input component is within a specified range. The data type must be floating point or convertible to floating point. The Message component you added in the previous section will indicate to the user if the validation fails. 1. Open the Validators section of the Components Palette. Drag the Double Range Validator from the Palette and drop it on the Text Field component. The double range validator is a
Validator in JSF By Rahul Kumar Page 2 of 5

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

By Rahul Kumar Page 3 of 5

Testing the Application


1. Run the application. 2. Delete 0.0 from the text field and press the Enter key without entering a value. Verify that a validation error is displayed for the text field, as shown in Figure 4.

3. Enter various numbers to test the application. Figure 5 shows the results when you enter 100

Validator in JSF

By Rahul Kumar Page 4 of 5

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

By Rahul Kumar Page 5 of 5

You might also like