[Spring MVC - Thymeleaf] - Form Submission and Error Messages

I am trying to create a form and validate its data using @Valid in a command object. Validation is doing well, but an error is thrown back to the Internet.

This is what I have:

Html

<div id="content" layout:fragment="contenido">
    <div sec:authorize="isAnonymous()">
        <form class="form-horizontal" action="#" th:action="@{register}" th:object="${userForm}" method="post">
            <input type="hidden" name="_csrf" th:value="${_csrf.token}"/>
            <fieldset>
                <label for="alias" th:text="#{form.register.alias}">Alias</label>
                <input id="alias" type="text" th:field="*{alias}" placeholder="Su alias" required="required" autofocus="autofocus"/>

                <label for="pass" th:text="#{form.register.password}">Contraseรฑa</label>
                <input id="pass" type="password" th:field="*{password}" pattern="[\w\d-_]{5,15}" required="required" th:title="#{form.error.password}"/>
                <p th:if="${#fields.hasErrors('password')}" th:errors="*{password}">Error en el dato ingresado</p>

                <button type="submit" name="save" class="btn btn-primary" th:text="#{control.register}">Registrarme</button>
            </fieldset>
        </form>
    </div>
</div>

      

controller

@RequestMapping(value = "/register", params = {"save"}, method = RequestMethod.POST) 
public String register (final ModelMap model, @Valid final UsuarioForm userForm, final BindingResult result) { 
    if (result.hasErrors()) { 
        return "register"; 
    } else { 
        return "redirect:/" + HomeController.PAGE_NAME; 
    } 
} 

      

When submit is clicked, the register method is called, result.hasErrors () is true, so the same page should be displayed, but this error occurs.

Stack

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'userForm' available as request attribute 
        org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) 
        org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:396) 
        org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:323) 
        org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:289) 
        org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.processAttribute(AbstractSpringFieldAttrProcessor.java:98) 
        org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87) 
        org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212) 
        org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017) 
        org.thymeleaf.dom.Node.processNode(Node.java:972) 

      

If I add "userForm" to the model in the controller like this:

Modified controller

@RequestMapping(value = "/register", params = {"save"}, method = RequestMethod.POST) 
public String register (final ModelMap model, @Valid final UsuarioForm userForm, final BindingResult result) { 
    if (result.hasErrors()) { 
        model.addAttribute("userForm", userForm); //THIS LINE IS ADDED 
        return "register"; 
    } else { 
        return "redirect:/" + HomeController.PAGE_NAME; 
    } 
} 

      

The error disappears BUT ... the expression in HTML ${#fields.hasErrors('password')}

returns false, so I cannot show the error messages to the user.

Any idea why this behavior is happening? Thanks in advance!

PS: I am using Spring MVC 4.1.2 with Thymeleaf 2.1.4

+3


source to share


1 answer


it

public String register(final ModelMap model,
        @Valid final UsuarioForm userForm,
        final BindingResult result)

      

it should be:



public String register(final ModelMap model,
        @ModelAttribute("userForm") @Valid final UsuarioForm userForm,
        final BindingResult result)

      

Pay attention to the annotation @ModelAttribute

.

+6


source







All Articles