Alex
Alex

Reputation: 7491

Why Spring BindingResult or validator do not show any errors?

I am trying to validate the Spring Bean containing email but neither the validator nor the BindingResult do not show any error when email in the request Bean comes as empty string. Please, see the following code:

Bean:

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springmodules.validation.bean.conf.loader.annotation.handler.Email;
import org.springmodules.validation.bean.conf.loader.annotation.handler.NotEmpty;

@Component("grouponRedemptionFormBean")
@Scope("prototype")
public class GrouponRedemptionBean {

@NotEmpty(message = "Please enter your email addresss.")
@Email(message = "Please correct your email.")
private String  email;
   …
}

Controller:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
public class GrouponVoucherRedemptionController {

@Autowired
@Qualifier("defaultBeanValidator")
private Validator validator;

@RequestMapping(value="/groupon-redemption.ep", method=RequestMethod.POST)
public String PostGrouponRedemption(@Valid @ModelAttribute GrouponRedemptionBean grouponRedemptionBean, BindingResult bindingResult,
HttpServletRequest request, HttpServletResponse response, Model model){

    Errors errors = new BeanPropertyBindingResult(grouponRedemptionBean, "grouponRedemptionFormBean");
    validator.validate(grouponRedemptionBean, errors);
    if(errors.hasErrors()) {
        bindingResult.addAllErrors(errors);
    }
    if (bindingResult.hasErrors()) {
        return GROUPON_REDEMPTION_VIEW;
    }    
...

XML configuration:

<mvc:annotation-driven />

Upvotes: 1

Views: 7799

Answers (2)

ibrahimyanik
ibrahimyanik

Reputation: 332

You can use

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

Or this

<bean id="validator"
  class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

Upvotes: 1

Roman
Roman

Reputation: 6656

You have wrong argument order - BindingResult must be immediately after the model object (GrouponRedemptionBean in your case). See the documentation:

The Errors or BindingResult parameters have to follow the model object that is being bound immediately as the method signature might have more than one model object and Spring will create a separate BindingResult instance for each of them so the following sample won't work:

Invalid ordering of BindingResult and @ModelAttribute.

@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet, Model model, BindingResult result) { ... }

Note, that there is a Model parameter in between Pet and BindingResult. To get this working you have to reorder the parameters as follows:

@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, Model model) { ... }

Upvotes: 7

Related Questions