This page last changed on Nov 29, 2004 by jcarreira.

I've been using the validator in webwork and have found myself duplicating a few of the validations throughout the app, so after asking a few questions I was told what to do.

basically I have a creation form to create a Car, and then I have another to Edit and Update the Car, I wanted to create just one validation file that would validate that Car, I was using 2 validation files, CreateCarAction-validation.xml and UpdateCarAction-validation.xml and both were identical.

what I had to do was:

first i had to expose the Car object in my action, then I had to create a validation file for the action, and placed this file in the same package as the CarAction in this example;

CarAction-validation.xml

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>
    <field name="car">
        <field-validator type="visitor">
            <message>Car Visitor: </message>
        </field-validator>
    </field>
</validators>

next I had to create Car-validation.xml file and put this file in the same package as the Car class

Car-validation.xml

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>

    <field name="name">
        <field-validator type="requiredstring">
            <message>Name is REQUIRED</message>
        </field-validator>
    </field>
    <field name="model">
        <field-validator type="requiredstring">
            <message>Model is REQUIRED</message>
        </field-validator>
    </field>

</validators>

so now if the input strings car.model and car.name are empty you should get fieldErrors added to the car.name and car.model fields, you just need to make sure that in your action you expose the Car object.

heres the CarAction.java file:

public class CarAction extends ActionSupport {

    private Car car = new Car();

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public String execute() {
        return SUCCESS;
    }


}

heres my Car bean:
public class Car {
    private String name;
    private String model;

    public Car() {}

   public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }
}

and then the car.vm file:
<form method="get">

<table border="1" align="center">

    #tag( TextField "label=Car ID" "name=car.id" "value=car.id" )
    #tag( TextField "label=Car Name" "name=car.name" "value=car.name" )
    #tag( TextField "label=Car Model" "name=car.model" "value=car.model" )

    #tag( Submit "value='Submit Car'" "align=center")

</table>

</form>

Document generated by Confluence on Dec 14, 2004 16:37