3

An action class has a property to be validated. The property is a class (MyClass1), one of whose properties is also a class (MyClass2).

Each of the classes to be validated will be used by multiple actions, so I defined a "MyClass*-validation.xml" for each.

The problem I am having is trying to use the visitor validator in MyClass1 to validate MyClass2.

MyAction-validation.xml:

<validators>
    <field name="class1">
        <field-validator type="visitor">
            <message></message>
        </field-validator>
    </field>
</validators>

MyClass1-validation.xml:

<validators>
    <field name="class2">
        <field-validator type="visitor">
            <message></message>
        </field-validator>
    </field>
</validators>

MyClass2-validation.xml:

<validators>
    <field name="myInt">
        <field-validator type="conversion">
            <message>myInt conversion</message>
        </field-validator>
    </field>
</validators>

If I submit a form with field class1.class2.myInt="a", fieldErrors() does not contain the conversion error, as was my expectation.

Is it possible to chain visitor validators indefinitely, or is one level of visitor the maximum? If it is possible, what am I doing wrong?


Undesirable (in my opinion) solution/workaround: Since I couldn't figure out what I was doing wrong, I eventually tried not chaining the validation from MyClass1-validation.xml to MyClass2-validation.xml, and instead changed the definition of MyAction-validation.xml:

<validators>
    <field name="class1.class2">
        <field-validator type="visitor">
            <message></message>
        </field-validator>
    </field>
    <field name="class1">
        <field-validator type="visitor">
            <message></message>
        </field-validator>
    </field>
</validators>

This change addressed the problem (fieldErrors() now contains the conversion error), but I would very much prefer to be able to chain the validators as was my initial intention.

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Matt
  • 4,515
  • 5
  • 22
  • 29

1 Answers1

3

Yes, it is actually possible to chain visitors validators indefinitely. The conversion validator get the full field name and checks whether conversion error map holds that name or not. The problem is that in case of multiple chained visitor validators conversion validator cannot get the actual full field name (e.g. class1.class2.myInt).

You can easily test chaining of visitor validators with some other validator in your MyClass2-validation.xml file (e.g. int validator).

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • Cool... so it is already chainable by default, and the only validator not working when chained through visitor is the `conversion` one ? – Andrea Ligios May 09 '14 at 08:25
  • @AndreaLigios: Can't say for sure it is the only one, but seems like that. :) – Aleksandr M May 09 '14 at 08:40
  • Interesting to know that other packaged validators work, unfortunate for me that this one doesn't, but at least I have a work-around. Thanks! – Matt May 09 '14 at 16:15