views:

129

answers:

0

I'm having trouble figuring out the proper way to get validation to work using annotations and a model driven approach. As you can see I have a simple form:

        <s:form action="register" namespace="/authenticate">
            <s:textfield name="username" label="Username"/>
            <s:password name="password" label="Password"/>
            <s:textfield name="firstName" label="First Name"/>
            <s:textfield name="lastName" label="Last Name"/>
            <s:textfield name="email" label="Email Address"/>
            <s:textfield name="phoneNumber" label="Phone Number"/>
            <s:submit/>
        </s:form>

And my Register action looks like this:

public class Register extends ActionSupport implements ModelDriven
{
    private User user = new User();

    public String execute()
    {
        return SUCCESS;
    }

    @Override
    public Object getModel()
    {
        return user;
    }
}

My User class is where I believe that the Validation annotations should actually go.

@Entity
public class User 
{   
    private String username;
    private String password;
    private String firstName;
    private String lastName;
    private String email;
    private String phoneNumber;

    @RequiredStringValidator(message="Username is required")
        public String getUsername() 
        {
           return username;
        }
        public void setUsername(String username) 
        {
           this.username = username;
        }

        //etc.

Update: Fixed it (partially) by doing the following: 1) Creating a result for "input" even though I think it should have happened automatically 2) @VisitorFieldValidator(message="",appendPrefix=false) needed to be above getModel()

One thing I noticed is that if I have more than one form on the page, and there's a field error for one field on one form, it will also appear as an error for the other field in the other form.