views:

109

answers:

1

This might be a basic question: I'm trying to understand how the Struts validation code below works:

<field property="myField" depends="validwhen, maxlength">
    <msg name="validwhen" key="error1.key" />
    <msg name="maxlength" key="error2.key"/>
    <var>
        <var-name>test</var-name>
        <var-value> isFieldsShown == "no" </var-value>
    </var>
    <var>
        <var-name>maxlength</var-name>
        <var-value>128</var-value>
    </var>
</field>

Is it like, first the validwhen rule checked and if it succeeds then the maxlength rule is checked? OR, if the validwhen returns true, then the field is considered valid without going to the maxlength rule at all?

A: 

It has been a while since I worked with the validator framework but from what I remember it goes something like the following.

When you validate a field, that field is considered valid if all of the validations pass. If at least one validation fails then the field is considered invalid.

It should make sense that if there is a list of validators to run, the validation process should exit at the first fail (no sense in running extra costly validations if the field was marked invalid because a validation already failed).

The documentation seems to indicate what I remember:

validator.dtd - field.depends attribute: The comma-delimited list of validators to apply against this field. For the field to succeed, all the validators must succeed.

Field.validate: Run all validations in the depends clause over each item in turn, returning when the first one fails.

dpb