Mike3355
Mike3355

Reputation: 12061

Thymeleaf form validation with spring MVC

This has been asked a few times but all of them did not answer my question. I have been trying to get different functionalities to work with Thymeleaf for two days now and been very unsuccessful. I have only been able to get it to work with spring-boot, however right now I am using spring-MVC.

First I will show you my dependencies

1

<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Create a New Account</title>
    <link th:href="@{/resources/css/loginForm.css}" href="/resources/css/loginForm.css" rel="stylesheet"
          type="text/css"/>
</head>
<body>

<form action="#" th:action="@{/createNewAccount}" th:object="${user}" method="post">
    <table>
        <tr>
            <td>Name:</td>
            <td><input type="text" th:field="*{username}" /></td>
            <td th:if="${#fields.hasErrors('name')}" th:errors="*{username}">Name Error</td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type="text" th:field="*{password}" /></td>
            <td th:if="${#fields.hasErrors('age')}" th:errors="*{password}">Password Error</td>
        </tr>
        <tr>
            <td><button type="submit">Submit</button></td>
        </tr>
    </table>
</form>

</body>
</html>

Now so you can see the errors my intellij IDE is showing:

2

User.java

package com.practice.domain;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * Created by DrewJocham on 8/30/15.
 */

public class User {

    @NotNull
    @Size(min = 2, max = 30)
    private String username;
    @NotNull
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

Upvotes: 0

Views: 6645

Answers (1)

Andrew
Andrew

Reputation: 1279

The field name you call hasErrors with needs to match the field name in the object. Like so:

<td th:if="${#fields.hasErrors('username')}" th:errors="*{username}">Name Error</td>

Note that hasErrors('name') became hasErrors('username'), and:

<td th:if="${#fields.hasErrors('password')}" th:errors="*{password}">Password Error</td>

Note that hasErrors('age') became hasErrors('password').

As for the errors being highlighted in Intellij, I think they're misleading, and are related to this open issue: https://youtrack.jetbrains.com/issue/IDEA-132738

Upvotes: 2

Related Questions