I have a form to create a user and a corresponding command object. It looks like so:
public class CreateUserForm {
private String name;
private String email;
private String password1;
private String password2;
private Boolean enabled = true;
... getters and setters ...
}
I have a user entity object that is to be created and stored based on the form input. The entity has annotated validation rules and looks like so:
@Entity
public class SiteUser {
@Id
private String id;
@NotEmpty
private String name;
@NotEmpty
@Email
private String email;
@NotEmpty
@Pattern(regexp="^[a-zA-Z0-9_]{6,20}$")
private String password;
@NotEmpty
private Boolean enabled;
... getters and setters ...
}
The form input should essentially have the same validation rules apart from the fact that the form input needs to check if the password1
and password2
fields are identical.
My first thought is to add the same validation annotations to the form object and create a custom validator that checks to see if the password1
and password2
fields are the same. However, I feel like I'm not doing something right by putting the same validation rules across two objects that are so similar.
So I'm looking for any advice on how to consolidate the process of validating and storing my entity based on the form input.