how to avoid duplicating validation rules
matt.drees Feb 29, 2012 11:01 PMHi AeroGear folks,
I'm curious to hear what others think about this.
In the AeroGear kitchensink archetype, the validation rules for the fields are written directly in index.html, as well as in Member.java.
<!-- For now mapping bean validation constraints from server side model is a manual task --> <form name="reg" id="reg" data-ajax="false"> <fieldset> <legend>Register a user:</legend> <div> <label for="name">Name:</label> <input type="text" name="name" id="name" placeholder="Your Name" required autofocus/> </div> <div> <label for="email">Email:</label> <input type="email" name="email" id="email" placeholder="Your Email" required/> </div> <div> <label for="phoneNumber">Phone ${symbol_pound}:</label> <input type="tel" name="phoneNumber" id="phoneNumber" pattern="[0-9]{10,12}" placeholder="Your Phone ${symbol_pound}" required/> </div> <div id="formMsgs"></div> <div data-role="controlgroup" data-type="horizontal"> <input type="submit" name="register" id="register" value="Register"/> <input type="button" name="cancel" id="cancel" value="Cancel"/> </div> </fieldset> </form>
and
@NotNull @Size(min = 1, max = 25, message = "1-25 letters and spaces") @Pattern(regexp = "[A-Za-z ]*", message = "Only letters and spaces") private String name; @NotNull @NotEmpty @Email(message = "Invalid format") private String email; @NotNull @Size(min = 10, max = 12, message = "10-12 Numbers") @Digits(fraction = 0, integer = 12, message = "Not valid") @Column(name = "phone_number") private String phoneNumber;
And of course, as the comment implies, it'd be nice to not have to copy the validation rules manually (like the phone number regex).
Has anyone done tried to tackle this problem?