Hi , everyone
Let me give you a hint about validator. We have to memorize the 4 different usage of validator attribute.
1. CDI validator.
For example
@Named ("xxxValidator")
public class XXXValidator implements Validator{
@Override
public void validate(FacesContext context, UIComponent compoent, Object object) throws ValidatorException {....}
}
<h:inputText ... >
<f:validator binding="#{xxxValidator}"/>
</h:inputText>
2. Use @FacesValidator
@FacesValidator ("xxxValidator")
public class XXXValidator implements Validator{
@Overrides
public void validate (FacesContext context, UIComponent component, Object value) throws ValdiatorException{
....
}
}
<h:inputText ...>
<f:validator validatorId="xxxValidator"/>
</h:inputText>
3. Use validator defined in faces-config.xml
For example,
package com.practice;
public class XXXValidator implements Validator{
@overrides
public void validate (FaceContext context, UIComponent component, Object value) throws ValidatorException{
...
}
}
In faces-config.xml
<validator>
<validator-id>customValidator</validator-id>
<validator-class>com.practice.XXXValidator</validator-class> //make sure it is a fully qualified name
</validator>
<h:inputText ...>
<f:validator validatorId="customValidator"/>
</h:inputText>
4. Use bean validator
@Named ("myBean")
public class examBean {
public void validateExamName (FacesContext context, UIComponent component, Object value) throws ValidateException {...}
}
<h:inputText ... validator="#{myBean.validateExamName}">
2 and 3 are the same except that one uses annotation while another one uses faces-config.xml
1 , 2, 3 uses <f:validator> facelet. When you are using CDI validator, you use binding = "validator name" When you are using validator, you use validatorId="validator id".
4 is different as the bean itself has its own validator method taking FacesContext, UIComponent and Object as arguments.
In the exam, you may be asked to change from case 1 to case 4, or something like that.