Hi all,
I'm migrating my web app from Spring 2.5.6 to Spring 3.0.2 and thought I would also use the latest JSR-303 validation.
I used to validate my (form) beans in 3 steps. Step 2 and 3 are optional depending on the data being validated:
1: My Validator (which implements org.springframework.validation.Validator) would invoke a Commons-Validator backed validator:
Code:<!-- Commons-Validator backed bean validator. --> <bean id="beanValidator" class="org.springmodules.validation.commons.DefaultBeanValidator"> <property name="validatorFactory"> <ref local="validatorFactory" /> </property> </bean>I had configured the appropriate validations with a validation.xml.Code:@Autowired private Validator beanValidator; @Override public void validate(final Object command, final Errors errors) { // Invoke the Commons-Validator backed validator beanValidator.validate(command, errors); }
2. Any custom validation that did NOT require access to the request object was done in the validate method:
3. Any custom validation that did require access to the request object was done in the controller's onBindAndValidate method.Code:@Override public void validate(final Object command, final Errors errors) { final CreateAccountForm form = (CreateAccountForm) command; // Invoke the Commons-Validator backed validator beanValidator.validate(command, errors); if (!errors.hasFieldErrors("password1") && !errors.hasFieldErrors("password2")) { // Only report possible password conflicts when there are no other // errors with either password1 or password2 if (!form.getPassword1().equals(form.getPassword2())) { errors .reject( "formErrors.passwordMismatch", "The supplied passwords did not match. Please make sure both passwords are typed exactly the same."); errors .rejectValue("password1", "formErrors.password1Mismatch", "This password must be exactly the same as the second password."); errors .rejectValue("password2", "formErrors.password2Mismatch", "This password must be exactly the same as the first password."); } } }
How can each of the above steps be achieved with Spring MVC 3? So far I understand 1 can be achieved easily by annotating the backing form's properties with @NotEmpty. @Email etc. and then use the @Valid annotation in my handling method:
But where can I perform my custom validation? If I use a custom validator as per below will 1 still happen?Code:@RequestMapping(value = "/createAccount", method = RequestMethod.POST) public String handleCreateAccount(final HttpServletRequest request, @Valid final CreateAccountForm form, final Model model) {
Also with the above will a custom binding initializer as exampled in Pet Clinic still have effect?Code:@InitBinder protected void initBinder(WebDataBinder binder) { binder.setValidator(new CreateAccountValidator()); }
Many thanks for any input,Code:<!-- - This bean processes annotated handler methods, applying PetClinic-specific PropertyEditors - for request parameter binding. It overrides the default AnnotationMethodHandlerAdapter. --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer"> <bean class="org.springframework.samples.petclinic.web.ClinicBindingInitializer"/> </property> </bean>
Nes


Reply With Quote
