There are various ways of validations available in ADF ,we can validate an attribute at entity level or at Jsf level as necessary. In jspx page the validate property of a component allows us to write our own custom validation logic in bean.
<af:inputText value="#{bindings.PhoneNumber.inputValue}"
label="#{bindings.PhoneNumber.hints.label}"
required="#{bindings.PhoneNumber.hints.mandatory}"
columns="#{bindings.PhoneNumber.hints.displayWidth}"
maximumLength="#{bindings.PhoneNumber.hints.precision}"
shortDesc="#{bindings.PhoneNumber.hints.tooltip}"
id="it2"
validator="#{poctestbean.validatephnnumber}">
<f:validator binding="#{bindings.PhoneNumber.validator}"/>
</af:inputText>
What if we want to reuse a validation logic throughout the application, ex. Email ,phone number etc.? This is to ensure the consistency of the validation logic across the application.
Lets take an example of Phone number validation, suppose a developer decides that a phone number is valid only with country code ex.+91 the other developer in his form allows user to enter without country code .Here they will be using different validation logic which results inconsistent validation rule.
This is where we need JSF validator to define our custom logic to be reused throughout the application, an indivisual developer just need to add that validator to his page .
In below picture you can see how we can add our cusom validation logic without using a JSF custom validator.
let see how we implement JSF custom validator -
Step 1- create a java class and implement the validator interface
Step 2- write your custom logic here which can be reused when ever we need to add validaton for a phone number field in JSF page
Step 3- Resister your class in faces-config.xml
Step 4-add the validator component for phone number field
Step 5- Choose your validator ID from the available list
Step 6-
The JSF code should look like -
<af:inputText value="#{bindings.PhoneNumber.inputValue}"
label="#{bindings.PhoneNumber.hints.label}"
required="#{bindings.PhoneNumber.hints.mandatory}"
columns="#{bindings.PhoneNumber.hints.displayWidth}"
maximumLength="#{bindings.PhoneNumber.hints.precision}"
shortDesc="#{bindings.PhoneNumber.hints.tooltip}"
id="it2"
>
<f:validator binding="#{bindings.PhoneNumber.validator}"/>
<f:validator validatorId="validatePhNumber"/>
</af:inputText>
Step 7 -
Run the page and enter an invalid phone number (less that 9 digit) and observe the exception it displays .
Done .
Hope this post is useful Thanks!!1
<af:inputText value="#{bindings.PhoneNumber.inputValue}"
label="#{bindings.PhoneNumber.hints.label}"
required="#{bindings.PhoneNumber.hints.mandatory}"
columns="#{bindings.PhoneNumber.hints.displayWidth}"
maximumLength="#{bindings.PhoneNumber.hints.precision}"
shortDesc="#{bindings.PhoneNumber.hints.tooltip}"
id="it2"
validator="#{poctestbean.validatephnnumber}">
<f:validator binding="#{bindings.PhoneNumber.validator}"/>
</af:inputText>
What if we want to reuse a validation logic throughout the application, ex. Email ,phone number etc.? This is to ensure the consistency of the validation logic across the application.
Lets take an example of Phone number validation, suppose a developer decides that a phone number is valid only with country code ex.+91 the other developer in his form allows user to enter without country code .Here they will be using different validation logic which results inconsistent validation rule.
This is where we need JSF validator to define our custom logic to be reused throughout the application, an indivisual developer just need to add that validator to his page .
In below picture you can see how we can add our cusom validation logic without using a JSF custom validator.
let see how we implement JSF custom validator -
Step 1- create a java class and implement the validator interface
Step 2- write your custom logic here which can be reused when ever we need to add validaton for a phone number field in JSF page
Step 3- Resister your class in faces-config.xml
Step 4-add the validator component for phone number field
Step 6-
The JSF code should look like -
<af:inputText value="#{bindings.PhoneNumber.inputValue}"
label="#{bindings.PhoneNumber.hints.label}"
required="#{bindings.PhoneNumber.hints.mandatory}"
columns="#{bindings.PhoneNumber.hints.displayWidth}"
maximumLength="#{bindings.PhoneNumber.hints.precision}"
shortDesc="#{bindings.PhoneNumber.hints.tooltip}"
id="it2"
>
<f:validator binding="#{bindings.PhoneNumber.validator}"/>
<f:validator validatorId="validatePhNumber"/>
</af:inputText>
Step 7 -
Run the page and enter an invalid phone number (less that 9 digit) and observe the exception it displays .
Done .
Hope this post is useful Thanks!!1
0 comments:
Post a Comment