How to specify a validator for an input component inside a composite component?

Should I register a custom validator in faces-config.xml if I use JSF 2.0.4? My custom validator uses the Validator interface, which is javax.faces.validator.Validator .

 <cc:myComp id="customcomp1" ... /> <cc:myComp id="customcomp2" ...> <f:validator id="myvalidator" for="myComp" /> </cc:myComp> 

myComp.xhtml

 <cc:interface> <cc:attribute ... /> <!-- more attributes --> </cc:interface> <cc:implementation> <h:panelGroup layout="block"> <h:inputText id="firstName" ... /> <h:inputText id="middleName" ... /> <h:inputText id="lastName" ... /> </h:panelGroup> </cc:implementation> 
+5
source share
3 answers

According to the code example in your updated question, you don't seem to delegate the validator to the correct input at all, so the validator is simply ignored at all.

You need to define the desired input (for which you want to add a validator) as <composite:editableValueHolder> in <composite:interface> .

 <cc:interface> <cc:editableValueHolder name="forName" targets="inputId" /> ... </cc:interface> <cc:implementation> ... <h:inputText id="inputId" ... /> ... </cc:implementation> 

The above <composite:editableValueHolder> basically says that any <f:validator for="forName"> should be applied on <h:inputText id="inputId"> <f:validator for="forName"> . So then do the following:

 <cc:myComp> <f:validator id="myValidator" for="forName" /> </cc:myComp> 

You can even use the same value in name and targets , but the key point is that <composite:editableValueHolder> must be present so that JSF knows which input component the validator should target. namely, to be more than one input component in the composite, you see.

+5
source

If you are working with JSF 2, I don’t think you need to touch the faces-config.xml file to create the Validator client. You can simply use the @FacesValidator annotation to declare a Validator . It should be something like this:

 @FacesValidator("myValidator") public class MyValidator implements Validator { @Override public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { // Your logic } } 

Then you can start using it on your .xhtml page, for example, with the <f:validator> :

 <f:validator validatorId="myValidator" /> 
+3
source

Not. This is not required for Jsf 2.0. Just mark your validator with @FacesValidator . Annotations automatically register your validator. No xml required.

+1
source

All Articles