How to override registerCustomerValidator in RegisterControl
Posted: Fri Nov 16, 2012 3:39 pm
Based on DemoSite code, I extended the Customer entity and add a new property fullname, and I want to validate this new property in RegisterController.
My code:
and
I also tried to override bean definition in applicationContext.xml
but it doesn't work, the RegisterCustomerValidator is still used to validate customer register.
Could you please tell me how to override registerCustomerValidator in RegisterControl?
My code:
Code: Select all
@Component("blRegisterCHPCustomerValidator")
public class RegisterCHPCustomerValidator extends RegisterCustomerValidator {
public RegisterCHPCustomerValidator() {}
@SuppressWarnings("unchecked")
public boolean supports(Class clazz) {
return clazz.equals(CHPRegisterCustomerForm.class);
}
@Override
public void validate(Object obj, Errors errors, boolean useEmailForUsername) {
CHPRegisterCustomerForm form = (CHPRegisterCustomerForm) obj;
Customer customerFromDb = customerService.readCustomerByUsername(form.getCustomer().getUsername());
if (customerFromDb != null) {
if (useEmailForUsername) {
errors.rejectValue("customer.emailAddress", "emailAddress.used", null, null);
} else {
errors.rejectValue("customer.username", "username.used", null, null);
}
}
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "passwordConfirm", "passwordConfirm.required");
errors.pushNestedPath("customer");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "fullname", "fullname.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "emailAddress", "emailAddress.required");
errors.popNestedPath();
if (!errors.hasErrors()) {
if (!form.getPassword().matches(getValidatePasswordExpression())) {
errors.rejectValue("password", "password.invalid", null, null);
}
if (!form.getPassword().equals(form.getPasswordConfirm())) {
errors.rejectValue("password", "passwordConfirm.invalid", null, null);
}
if (!GenericValidator.isEmail(form.getCustomer().getEmailAddress())) {
errors.rejectValue("customer.emailAddress", "emailAddress.invalid", null, null);
}
}
}
}
and
Code: Select all
@Controller
@RequestMapping("/register")
public class RegisterController extends BroadleafRegisterController {
@Resource(name="blRegisterCHPCustomerValidator")
private RegisterCHPCustomerValidator registerCustomerValidator;
@RequestMapping(method=RequestMethod.GET)
public String register(HttpServletRequest request, HttpServletResponse response, Model model,
@ModelAttribute("registrationForm") RegisterCustomerForm registerCustomerForm) {
return super.register(registerCustomerForm, request, response, model);
}
@RequestMapping(method=RequestMethod.POST)
public String processRegister(HttpServletRequest request, HttpServletResponse response, Model model,
@ModelAttribute("registrationForm") RegisterCustomerForm registerCustomerForm, BindingResult errors) throws ServiceException {
return processRegister(registerCustomerForm, errors, request, response, model);
}
@ModelAttribute("registrationForm")
public CHPRegisterCustomerForm initCustomerRegistrationForm() {
RegisterCustomerForm superForm = super.initCustomerRegistrationForm();
CHPRegisterCustomerForm form = new CHPRegisterCustomerForm();
form.setCustomer(superForm.getCustomer());
return form;
}
}
I also tried to override bean definition in applicationContext.xml
Code: Select all
<bean id="blRegisterCustomerValidator"
class="com.XXX.controller.account.validator.RegisterCHPCustomerValidator" scope="singleton"/>
but it doesn't work, the RegisterCustomerValidator is still used to validate customer register.
Could you please tell me how to override registerCustomerValidator in RegisterControl?