0

I am struggling with a custom validator for a text field. It seems that the custom validation only works AFTER the required validation is executed. This means that a field without a requiredValidator cannot be custom validated - is that true? What I want to do:

I have a text field. The value is only required if a specific value in another field is selected (here this is a checkbox group). It is a dependant validation. My custom validator works fine until the text field is required - but this should not be the case.

checkbox does not have the specific value -> text field can be blank
checkbox holds the value -> text field must have a value.

Any ideas?

    <xp:inputText id="inputText1" disableClientSideValidation="true">
</xp:inputText>

<xp:inputText id="inputText2" disableClientSideValidation="true">

    <xp:this.validators>
        <xp:customValidator message="err">
            <xp:this.validate><![CDATA[#{javascript:if(getComponentValue("inputText1").length>0 && getComponentValue("inputText2").length==0) postValidationError(this, "foo")}]]></xp:this.validate>
        </xp:customValidator>

    </xp:this.validators>
</xp:inputText>

Where getComponentValue is a method to receive either the value with getValue or getSubmittedValue from the component and postValidationError is a method to add a faces message.

EDIT & FINAL ANSWER

Conclusion and a sample here: http://mardou.dyndns.org/Privat/osnippets.nsf/id/OBUE-95BLK4

Oliver Busse
  • 3,375
  • 1
  • 16
  • 26
  • Supplying sample code of what you're trying will help get answers. – TimWagaman Feb 26 '13 at 21:05
  • Yes, that's right, but this was my first post and I was not sure how much code I can provide as this is a combination of XSP, SSJS and other stuff related to my question ;) I will try to clear it up by providing some code - promised! I just have to tear down this problem to a simple construct. – Oliver Busse Feb 27 '13 at 01:06
  • Your validator cannot work: If *inputText2* is empty, the customValidator is not triggered (just add print something to the console and you will see what I mean). – Sven Hasselbach Feb 27 '13 at 05:14
  • Yes that is the problem but I had no luck with the required validator either. Can you give me a hint for that way? – Oliver Busse Feb 27 '13 at 07:54
  • I have added an example to my answer – Sven Hasselbach Feb 27 '13 at 08:24

1 Answers1

3

The required validator is always the first validator which will be executed during the validation. That means that the answer to the first part of your question is YES.

But this does not mean that you need a required validator to use a custom validator: This part of your question has to be answered with a clear NO.

The required validator is a special kind of "hack", because a validator is only executed if your component receives a new value (aka not blank).

I am not sure why you have a problem with a custom validator - in the scenario you are describing you are just using a required validator...

EDIT:

Just "turn your validators around": Add the custom validator from inputText2 to inputText1 and it should work.

EDIT 2:

<xp:inputText id="inputText1" disableClientSideValidation="true">
   <xp:this.validators>
      <xp:customValidator message="err">
         <xp:this.validate><![CDATA[#{javascript:
            var val = getComponent("inputText2").getSubmittedValue();
               if( val.equals("") == true )
                  return false;
               null}]]>
         </xp:this.validate>
      </xp:customValidator>
   </xp:this.validators>
</xp:inputText>

<xp:inputText id="inputText2" disableClientSideValidation="true" />
Sven Hasselbach
  • 10,455
  • 1
  • 18
  • 26
  • Hi Sven! Thanks for your post! This was my first attempt: using a "calculated" required validator depending of the value of the other field. That did not work. The custom validator was intended to check the value of the first field and then pass a new faces message if the second field is blank. – Oliver Busse Feb 27 '13 at 00:54