Page 1 of 1

How to override registerCustomerValidator in RegisterControl

Posted: Fri Nov 16, 2012 3:39 pm
by aaron8tang
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:

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?

Re: How to override registerCustomerValidator in RegisterControl

Posted: Fri Nov 16, 2012 4:49 pm
by jefffischer
I would have thought declaring your validator in app context xml with the bean id of "blRegisterCustomerValidator" would have taken care of it. Have you tried moving this bean declaration to your applicationContext-servlet.xml?

Re: How to override registerCustomerValidator in RegisterControl

Posted: Fri Nov 16, 2012 9:42 pm
by aaron8tang
Thanks! It works.

Why it doesn't work when I put the declaration in applicationContext.xml?