Thymeleaf Could not bind form errors using expression "*"

Dmitry Oleinik

I use Spring-boot and Thymeleaf template engine and I try use th:classappend attribute for adding optional "has-error" class for < div > html tag using #fields.hasErrors('*') expression

<form method="POST" action="/registration" class="form-signin">
        <h2 class="form-signin-heading">Create your account</h2>

        <div class="form-group" th:classappend="${#fields.hasErrors('*')} ? 'has-error' : ''">
            <input name="username" type="text" class="form-control" placeholder="Username" autofocus="true"/>
            <p class="alert alert-danger" th:if="${#fields.hasErrors('username')}" th:errors="*{username}"></p>
        </div>

        <div class="form-group" th:classappend="${#fields.hasErrors('*')} ? 'has-error' : ''">
            <input name="password" type="text" class="form-control" placeholder="Password" autofocus="true"/>
            <p class="alert alert-danger" th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></p>
        </div>

        <div class="form-group" th:classappend="${#fields.hasErrors('*')} ? 'has-error' : ''">
            <input name="passwordConfirm" type="text" class="form-control" placeholder="Confirm your password" autofocus="true"/>
            <p class="alert alert-danger" th:if="${#fields.hasErrors('passwordConfirm')}" th:errors="*{passwordConfirm}"></p>
        </div>

        <button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>


    </form>

but I have this error

Could not bind form errors using expression "*". Please check this expression is being executed inside the adequate context (e.g. a with a th:object attribute)

my controller methods

@RequestMapping(value = "/registration", method = RequestMethod.GET)
    public String registration(Model model) {
        model.addAttribute("userForm", new User());

        return "registration";
    }

    @RequestMapping(value = "/registration", method = RequestMethod.POST)
    public String registration(@ModelAttribute("userForm") User userForm, BindingResult bindingResult, Model model) {
        userValidator.validate(userForm, bindingResult);

        if (bindingResult.hasErrors()) {
            return "registration";
        }

        userService.save(userForm);

        securityService.autologin(userForm.getUsername(), userForm.getPasswordConfirm());

        return "redirect:/welcome";
    }

what am I doing wrong?

Dmitry Oleinik

I just add th:object="${userForm} attribute to my form element. And now it work fine!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Thymeleaf: Could not parse as expression

Thymeleaf: could not parse as expression for links

Thymeleaf: Concatenation - Could not parse as expression

Spring Boot Thymeleaf Could not parse as expression with an url

Could not parse as expression defining a DataTable in Thymeleaf

Thymeleaf could not parse as expression th:select option

thymeleaf not allowing using `&&` in form

TinyMapper: bind using expression

how to get rid of org.thymeleaf.exceptions.TemplateInputException: while using thymeleaf expression to print data in form of bootstrap cards?

What does this expression mean in thymeleaf form validation?

using v-bind:href to bind an expression

Spring Boot opening a modal if there are errors using Thymeleaf

Spring Boot + Thymeleaf: Bind empty form input to NULL-string

Why is Thymeleaf failing to bind form inputs to object fields?

Handling form validation with Thymeleaf 3.x - Stylizing errors

Compile errors using std::bind in gcc 4.7

Using a Thymeleaf form to construct POST request

Using an expression in a numeric form control

How to bind an Object's attribute to a hidden field using ThymeLeaf?

how we can bind a list of a list of object using thymeleaf

Problem with displaying errors on login form using sessions

checking form control input for errors by using it as a variable

How to Display Django form errors using AngularJS?

Show base errors using simple_form

Handle form errors using components Angular - TypeScript

Show errors on form without using $_GET

thymeleaf bind file to object

How could I bind `save` method using `Q` with mongoose

Not able to bind values from Thymeleaf based UI form to a controller in Spring Boot