How to Use ValidatorsValidators verify data is within parameters specified by the developer. This tutorial will discuss the following topics: Standard JSF Validation
The required attribute is available on JSF input tags. There are also three types of validators that come with JSF:
In our demo, a bean named user has been defined in the faces.config file as a session scope bean. The ice:inputText component will take the user's age. If you attempt to input an age that is less than 1 or greater than 120 you will get the standard JSF error message. The following is the basic code snippet used for the standard validation: <!-- age validator --> <ice:inputText id="age" value="#{user.age}"> <f:validateLongRange maximum="120" minimum="1"/> </ice:inputText> <ice:message style="color: red;" id="ageError" for="age"/>
Application Level Validation
In the following example, the user clicks the 'Register' button which calls the register() method. If our application level validation fails, we can manually construct a FacesMessage in our bean code and display it via the ice:message tag in our page: bean public String register(){ FacesContext context = FacesContext.getCurrentInstance(); if(StringUtils.isEmpty(user.getName())){ FacesMessage message = new FacesMessage(); message.setSeverity(FacesMessage.SEVERITY_ERROR); message.setSummary("Name Field is Blank"); message.setDetail("Name Field is Blank.."); context.addMessage("tutorialForm:name",message); return "error" } return "success" }
Custom Validator
Implement the Validator interfacePhoneNumberValidator.java public class PhoneNumberValidator implements Validator{ /** phone number in form of xxx-xxxx*/ private static final String PHONE_NUM = "[0-9]{3}[-]{1}[0-9]{4}"; ... } Implement the validate() methodPhoneNumberValidator.java public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException{ /* create a mask*/ Pattern mask = Pattern.compile(PHONE_NUM); /* retrieve the string value of the field*/ String phoneNumber = (String)value; /*check to ensure that the value is a phone number*/ Matcher matcher = mask.matcher(phoneNumber); if(!matcher.matches()){ FacesMessqage msg = new FacesMessage(); message.setDetail(" Phone number not in valid format"); message.setSumamry("Phone number not in valid format"); message.setSeverity(FacesMessage.SEVEROTY_ERROR); throw new ValidatorException(message); } } Register the Validator in faces.config<validator> <validator-id>phoneNumberValidator</validator-id> <validator-class>com.icesoft.icefaces.tutorial.validators.custom.PhoneNumberValidator</validator-class> </validator> } Use the Tag in your Page<ice:inputText id=phoneNumber value="#{user.phoneNumber}"> <f:validator validatorId="phoneNumberValidator"/> </ice:inputText>
Custom Validator in Backing Beans
backing bean public void validateEmail(FacesContext context, UIComponent validate, Object value){ String email = (String)value; if(email.indexOf('@')==-1){ ((UIInput)validate).setValid(false); FacesMessage msg = new FacesMessage("Invalid Email"); context.addMessage(validate.getClientId(context), msg); } } Tag usage:
<ice:inputText id="email" value="#{user.email}" validator="#{user.validateEmail}" required="true"/>
Single Submit (ICEfaces 2) / Partial Submit (ICEfaces 1)
ICEfaces 2 Single Submit Applied to Form Elements <h:form> <icecore:singleSubmit /> <ice:inputText id="age" value="#{user.age}" > <f:validateLongRange maximum="120" minimum="1"/> </ice:inputText> </h:form> ICEfaces 1 Partial Submit Attribute <ice:inputText id="age" value="#{user.age}" partialSubmit="true"> <f:validateLongRange maximum="120" minimum="1"/> </ice:inputText>
Tutorial Source Code Downloads
|
Validators
© Copyright 2021 ICEsoft Technologies Canada Corp.