1 Reply Latest reply on Apr 27, 2010 3:39 PM by Nikos Paraskevopoulos

    Custom Component Validator

    Ben  Smythn Newbie

      Hi, I have created a facelet custom component with my own validator and in the constructorI add the validator.

             public TextComponent() {
                      addValidator(new NifValidator());

      My problem is occurs when I submit the value to the component, the validator is called several times. The first time it validates it does two validations, on the second call it validates three times and so on. I've looked everywhere but I still don't realize what could be wrong with my component.

        • 1. Re: Custom Component Validator
          Nikos Paraskevopoulos Novice


          The validators are added to the component during view creation, i.e. the first GET request. Afterwards it is the job of the JSF state saving mechanism to preserve the state of the component tree and its attached resources, like validators. When the view is restored, new objects are created to represent the components and JSF calls their restoreState() method. In your case, the TextComponent also creates its validator. Then the state restore mechanism takes place, adding the previous instance(s) of the validator. So on the first POST there are two validators, etc.

          I'd suggest one of the following:

          1) Attach the validator in the normal way, even if the component and/or validator are custom:

          <my:text ...>
            <my:validator ... />

          2) Create a custom JSP/Facelets tag handler that creates a component and attaches the validator at the proper time (I believe the apply() method of the com.sun.facelets.FaceletHandler, but check docs/code to be sure).