5

Should I register a custom validator in faces-config.xml if I'm using JSF 2.0.4? My custom validator uses 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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Narayana Nagireddi
  • 729
  • 1
  • 13
  • 33
  • You seem to be using a custom component or a composite component. You need to show how you have created it. Perhaps you simply didn't delegate the validator properly. – BalusC Jan 09 '12 at 12:10

3 Answers3

5

As per the code example in your updated question, you seem to not be delegating the validator to the right input at all, so the validator is simply ignored altogether.

You need to define the desired input (for which you'd like to attach a validator) as a <composite:editableValueHolder> in the <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 tells that any <f:validator for="forName"> must be applied on the <h:inputText id="inputId">. So, the following should then do it:

<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 there must be a <composite:editableValueHolder> present so that JSF knows on what input component exactly the validator should be targeted, there can namely be more than one input component in the composite, you see.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
3

If you are working with JSF 2, I don't think you need to touch the faces-config.xml file to create a customer Validator. You can simply use the annotation @FacesValidator 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 in your .xhtml page with, for instance, <f:validator> tag:

<f:validator validatorId="myValidator" />
Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
  • I did exactly as mentioned above in the myValidator class but still it's not being called. – Narayana Nagireddi Jan 09 '12 at 04:19
  • 1
    @Altair: can you post the code for your `Validator` as well as your composite component? A `Validator` should be registered for a single input, not the whole component. – Mr.J4mes Jan 09 '12 at 05:17
1

No. That is not necessary with Jsf 2.0. Just annotate your validator with @FacesValidator. The annotation registers your validator automatically. No xml needed.

Matt Handy
  • 29,855
  • 2
  • 89
  • 112