st-tu-dresden / videoshop

SalesPoint sample application
Apache License 2.0
87 stars 246 forks source link

CustomerController empty String handling #138

Closed edtha closed 3 years ago

edtha commented 3 years ago

If you remove the required="required" attribute in the registration form and therefore disabling the front end check for empty strings an error occurs. Can anyone confirm this? My suggestion (which works for me) would be changing line 45 to 50 from being like

        @PostMapping("/register")
    String registerNew(@Valid RegistrationForm form, Errors result) {

        if (result.hasErrors()) {
            return "register";
        }

to being like

        @PostMapping("/register")
    String registerNew(@Valid @ModelAttribute("form") RegistrationForm form, Errors result, Model model) {

        if (result.hasErrors()) {
            return register(model, form);
        }

as it's being done in the guestbook form.

martinmo commented 3 years ago

You are right, in this case the server-side validation fails with a TemplateInputException, which is caused by

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'form' available as request attribute

That's because, by default, the model attribute name is derived from the form class name, unless it is set explicitly using @ModelAttribute(…) as you have done it. The other variant without an explicit @ModelAttribute involves changing th:object to use ${registrationForm} instead of ${form} in the <form> in register.html.

Why did you change

return "register";

to

return register(model, form);

? This shouldn't be necessary.