Cannot get validation working with Spring Boot and Thymeleaf

26,571

Found the answer due to the tutorial here. I have to change my method signature from:

public String doRegistration( @Valid CustomerLicenseRegistration customerLicenseRegistration, 
Model model, 
BindingResult bindingResult )

to:

public String doRegistration( @Valid CustomerLicenseRegistration customerLicenseRegistration, 
BindingResult bindingResult, 
Model model )

Notice how the BindingResult has to be immediately after the object I have annotated with @Valid.

Share:
26,571

Related videos on Youtube

Wim Deblauwe
Author by

Wim Deblauwe

Software Engineer & Team Leader working with Java/Spring mostly. Author of Taming Thymeleaf and Practical Guide to Building an API Back End with Spring Boot

Updated on January 05, 2020

Comments

  • Wim Deblauwe
    Wim Deblauwe over 4 years

    I have a Spring Boot application (using version 1.2.3) with 1 controller that shows a form. This all works fine, but now I want to add validation. I have this method in my controller:

    @RequestMapping(value = "/licensing", method = RequestMethod.POST)
    public String doRegistration( @Valid CustomerLicenseRegistration customerLicenseRegistration, Model model, BindingResult bindingResult )
    {
        if( bindingResult.hasErrors())
        {
            logger.debug( "There are errors! {}", bindingResult );
            return "customer/license-registration";
        }
        logger.debug( "customerLicenseRegistration: " + customerLicenseRegistration );
        CustomerLicense customerLicense = m_licenseService.createCustomerLicense( customerLicenseRegistration );
        model.addAttribute( "customerLicense", customerLicense );
        return "customer/license-registration-done";
    }
    

    If I now type something invalid, I get the "Whitelabel error page" after submit and my breakpoint inside the method is never hit (If I remove the @Valid annotation, the breakpoint does get hit). The error page shows:

    Whitelabel Error Page
    
    This application has no explicit mapping for /error, so you are seeing this as a fallback.
    
    Mon May 18 09:42:27 CEST 2015
    There was an unexpected error (type=Bad Request, status=400).
    Validation failed for object='customerLicenseRegistration'. Error count: 1
    

    Spring seems to notice that the object is not valid, but it does not show the form again so the user can fix his mistake. What am I doing wrong?

  • Jimmy
    Jimmy almost 9 years
    Thanks buddy for answer. I put hours in understanding what was the problem and it came with just method signature mis match. Work for me. thanks. :)
  • Jimmy
    Jimmy almost 9 years
    sure. I just forgot in happiness :)
  • Francis Fredrick Valero
    Francis Fredrick Valero about 8 years
    Thanks :) These are the things that annoy me in spring hahahah
  • Argb32
    Argb32 almost 8 years
    In my case everything worked well until I changed form encoding type to multipart. The signature change resolved the issue. Thank you.
  • quangkid
    quangkid almost 6 years
    The tutorilal link is died
  • Wim Deblauwe
    Wim Deblauwe almost 6 years
    Updated link to archive from wayback machine as the link is indeed dead.