Custom Validators using JSF

Custom Validators using JSF
The custom validators are user-defined validators that are created to meet application-specific validation. (For Standard Validators check here)

Custom Validators can be created in 2 ways

  • Implementing Validator Interface
  • Use Bean Method

Example: Lets create a JSF page to check if the person is eligible to vote using custom validators

Steps:
1. Create cvalidator java class and implement validator, write a function for validation
2. JSF Managed Bean
3. Register Custom validator class in faces-config.xml or using Annotations
4. Bind it with the UI component in JSF page

Implementing Validator Interface:
Create a java class and implement validator interface.

cvalidator.java

JSF Managed Bean – age.java

Registering Custom Validator class:
This can be done in 2 ways,

  • Register in faces-config.xml
  • using Annotations

Faces-config.xml:

Annotations:

index.xhtml


Bean Method:
JSF ManagedBean – age.java

index.xhtml

Output:


out1

 

 

 

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *