Validator objects

JSF Validators test the value of a string or JavaScript™ object (string, number, date) to see if the value conforms to JSF validation rules. For example, the date validator can check if a string is correctly formatted as a date and that the date is within a specified range.

JSF Validators are used by other JWL components when performing field validation, input assist and managing client-side data.

Using a Validator

To check if the value of an input field is a number and is valid, first create a converter and a validator. For example, create an integer converter and create a validator that checks if a number is between 18 and 21:
<script>
     hX_5.addConverter("1", new hX_5.NumberConverter("strict:1", "pattern:##", "locale:,.%‰-€"));
     hX_5.addValidator("2", new hX_5.NumberValidator("required:true", "minimum-bound:18", "maximum-bound:21"));
</script>
Use a converter to convert the value of an input field to a JavaScript number and then validate the number:
<script>
     var c = hX_5.getConverterById("1");
     var v = hX_5.getValidatorById("2");
     var x = document.getElementById("TestField");
     if (c!=null && v!= null && x!=null) {
         var n = c.stringToValue(x.value);
         if (n!=null) {
         			if (v.validate(n)) {
             	alert ("Number is OK");
             } else {
             alert ("Number not valid: " + v.lastError());
             }
         } else {
             alert ("Not a number: " + c.lastError());
         }
     }
</script>
In a page where validators are generated by a JSF tag, you can access a validator by retrieving it from the behavior that created it. For example, assume the following JSF tag was put in a page:
<h:inputText styleClass="inputText" id="text1"
size="6" value="#{myBean.myBigDecimal01}">
     <f:convertNumber pattern="##0.0"/>
     <hx:inputHelperSpinner delta="0.1"/>
     <hx:inputHelperAssist validation="true" errorClass="inputText_Error"/>
     <f:validateDoubleRange minimum="-100.0" maximum="100.0"></f:validateDoubleRange>
</h:inputText>
You can retrieve the validator emitted by the JSF-generated behavior by finding the behavior and retrieving the validator-id from the behavior.
<script>
     var vID = hX_5.getBehaviorById("form1:text1", "validate", "onblur").getAttribute("validator");
     var vld = hX_5.getValidatorById(vID);
</script>

hx calls to create and find a validator

hX_5.addValidator("id", new hX_5.NameValidator(attributes)); where

id

The ID to be assigned to this validator. The ID must be unique across all created validators within the page (or portal). Any string value may be used as the ID.

validatorName

The name of the JSF validator. See below for the list of validators.

attributes

Comma separated list of attributes where each attribute is a quoted string consisting of the attribute name and value separated by a colon, for example "label:MyLabel".

To get a validator object:

var x = hX_5.getValidatorById("id"); where

id

The ID to be assigned to this validator. The ID must be unique across all created validators within the page (or portal). Any string value may be used as the ID.

Common API calls on a validator

To validate a value:

var x = vld.validate(value, cvt); where

vld

A validator object.

value

A JavaScript object of the type of the converter (Number, Date/Time or String)

cvt

A converter object

returns

A JavaScript object of the type of the converter (Number, DateTime or String). If the conversion cannot be done, null is returned and lastError is set.

To retrieve information about why the last validation failed:

var x = vld.lastError();

var x = cvt.stringToValue("string"); where

vld

A validator object.

returns

A JavaScript object of the type of the converter (Number, DateTime or String). If the conversion cannot be done, null is returned and lastError is set.

To change an attribute of a validator:

vld.setAttribute("arg"); where

vld

A validator object.

arg

A JSF attribute/value string.

To get an attribute of a validator:

var x = vld.getAttribute("argname"); where

vld

A validator object.

argname

A string containing the name of the attribute.

returns

A JavaScript object of the type of the converter (Number, DateTime or String). If the conversion cannot be done, null is returned and lastError is set.

Table 1. Validator objects

Validator

Description

NumberValidator Validate a string containing a Java-formatted number or a JavaScript number object
DateValidator Validate a string containing a Java-formatted date or a JavaScript date object
StringValidator Validate a string containing a Java-formatted masked string or a JavaScript string
Related concepts
JSF Widget Library (JWL)

Feedback