org.springframework.validation.BeanPropertyBindingResult Exception
Asked Answered
D

2

12

Hi I am a new to spring framework. I have done a small example where I tried to validate my input field using spring validation api. This is the code

@RequestMapping(value = "/applicationFormSubmit", method = RequestMethod.POST)
    public String insertdata( @ModelAttribute("applicationForm") @Valid ApplicationFormBean applicationFormBean, @RequestParam("file") MultipartFile file, BindingResult result,Model model)
{
    if(result.hasErrors())
    {

        return "applicationForm";           
    }
      try {
            Blob blob = Hibernate.createBlob(file.getInputStream());

          //  applicationFormBean..setFilename(file.getOriginalFilename());
            applicationFormBean.setSignature(blob);
          //  applicationFormBean.setContentType(file.getContentType());
        } catch (IOException e) {
            e.printStackTrace();
        }
    applicationFormUserService.insertApplicationData(applicationFormBean);

    return "applicationForm";       

}

But when I submit the from with blank value its given me the following error

Field error in object 'applicationForm' on field 'applicantName': rejected value []; codes [NotEmpty.applicationForm.applicantName,NotEmpty.applicantName,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [applicationForm.applicantName,applicantName]; arguments []; default message [applicantName]]; default message [Please enter your nnnn.]

Field error in object 'applicationForm' on field 'applicantName': rejected value []; codes [Size.applicationForm.applicantName,Size.applicantName,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [applicationForm.applicantName,applicantName]; arguments []; default message [applicantName],20,2]; default message [this is worng ]
Disavow answered 17/7, 2014 at 11:48 Comment(0)
L
22

Please change the line in your code, which is shown below.

public String insertdata(
    @ModelAttribute("applicationForm") @Valid ApplicationFormBean applicationFormBean,
    BindingResult result,
    Model model,
    @RequestParam("file") MultipartFile file)

As stated in this Spring MVC tutorial:

The BindingResult must come right after the model object that is validated or else Spring will fail to validate the object and throw an exception.

This tutorial also mentions this problem.

Likewise answered 18/7, 2014 at 6:13 Comment(1)
Great. Been struggling with this for 2 hours.. And I just remember I did the same mistake couple years back.. easy to miss.Rat
M
2

Regarding to @ASADUL I solved the problem also with changing the method's signature order.

In my case the HttpServletRequest request was next to @ModelAttribute(FORM_MODEL_KEY) @Valid MemberForm memberForm which causes the error.

@PostMapping(URLS.MEMBER_PROCESSING)
    public String processMemberForm(@ModelAttribute(FORM_MODEL_KEY) @Valid MemberForm memberForm, HttpServletRequest request, BindingResult bindingResult, RedirectAttributes redirectAttributes) {

I changed to

@PostMapping(URLS.MEMBER_PROCESSING)
    public String processMemberForm(@ModelAttribute(FORM_MODEL_KEY) @Valid MemberForm memberForm, BindingResult bindingResult, HttpServletRequest request, RedirectAttributes redirectAttributes) {

Note that HttpServletRequest request must put next after BindingResult bindingResult and @ModelAttribute(FORM_MODEL_KEY) @Valid MemberForm memberForm, BindingResult bindingResult must be in this order.

Manville answered 13/8, 2021 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.