2

Now in my save action,i defined a model which called booking and it is as following:

Class BookingAction {
       private Booking booking;
       ...
}

Class Booking {
  private String bookingNo;
  private String status;
  ...
  private List<Part>parts = new ArrayList<Part>(); 
  ...
}

Class Part {
  private String partNo;
  ...
}

I also defined a validation xml file for that action,e.g

<validators>
    <field name="booking.status">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>${getText("MandatoryFieldEmpty",{"%{getText(\"BookingMain.status\")}"})}</message>
        </field-validator>
    </field>

    <field name="booking.bookedBy">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
        <message>${getText("MandatoryFieldEmpty",{"%{getText(\"BookingMain.bookedBy\")}"})}</message>
    </field-validator>
    </field>
....

Can I define this kinda of configuration for Part too?

The Part is in a List and the List is Booking's property, does anybody could tell me if i could have Part's validation in BookingAction-validation.xml?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Chailie
  • 451
  • 2
  • 11
  • 24

1 Answers1

1

You can use the Visitor Validator;

You should add the <validator type="visitor"> snippet related to parts object in your BookingAction-validation.xml to activate the Visitor validation;

Then, you'll need to create an Part-validation.xml under the package of the Part Object (instead of the package of the Action object), and specify there the rule for a single Part element.

Struts2 Validation Interceptor will take care of validating each element of the List by using this second file.

As a nice side effect, if you include a List<Part> object in another Action, your validation for Part object will be already there, with no need to rewrite it in another file (you will only need to declare the validator snippet in your Action-validation.xml file).


EDIT

You can specify different contexts for triggering a further, complementary validation of the same bean by using a more specific Bean-context-validation.xml file, only in some specific cases.

Read this detailed example, especially the Visitor Validation Example (and the following Visitor Validation with the Expression Validator) part.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243