繁体   English   中英

Thymeleaf 表单验证

[英]Thymeleaf form validation

我正在尝试通过仔细检查来验证密码,如果密码不匹配,则 thymeleaf 会显示错误

输入表单看起来像这样

package com.foxminded.university.domain;

import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

public class UserForm {

    @NotNull(message = "Can't be empty")
    @Size(min = 2, max = 30, message = "Must be more than 2 and less than 30 symbols")
    private String firstName;

    @NotNull(message = "Can't be empty")
    @Size(min = 2, max = 30, message = "Must be more than 2 and less than 30 symbols")
    private String lastName;

    @Email(message = "Enter valid e-mail" )
    private String email;

    @Pattern(regexp = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})",
    message = "Enter valid password")
    private String password;

    private String passwordRepeat;

    private boolean passwordsEqual;

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }

    public String getPasswordRepeat() {
        return passwordRepeat;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setEmail(String email) {
        this.email = email;
    }

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

    public void setPasswordRepeat(String passwordRepeat) {
        this.passwordRepeat = passwordRepeat;
    }

    public void setPasswordsEqual(boolean passwordsEqual) {
        this.passwordsEqual = passwordsEqual;
    }
    @AssertTrue(message = "Passwords should match")
    public boolean isPasswordsEqual() {
        return password.equals(passwordRepeat);
    }
}

和百里香形式行:

<div class="form-group">
            <label>Password</label>
            <input type="text" th:unless="${#fields.hasErrors('password')}" class="form-control" placeholder="Password"
                   th:field="*{password}">
            <input type="text" th:if="${#fields.hasErrors('password')}" class="form-control alert-danger"
                   placeholder="Password" th:field="*{password}">
            <span th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></span>
        </div>
        <div class="form-group">
            <label> Repeat password</label>
            <input type="text" th:unless="${#fields.hasErrors('passwordsEqual')}" class="form-control"
                   placeholder="Password" th:field="*{passwordRepeat}">
            <input type="text" th:if="${#fields.hasErrors('passwordsEqual')}" class="form-control alert-danger"
                   placeholder="Password" th:field="*{passwordRepeat}">
            <span class="error" th:if="${#fields.hasErrors('passwordsEqual')}" th:errors="*{passwordsEqual}"></span>
        </div><div class="form-group">
            <label> Repeat password</label>
            <input type="text" th:unless="${#fields.hasErrors('passwordsEqual')}" class="form-control"
                   placeholder="Password" th:field="*{passwordRepeat}">
            <input type="text" th:if="${#fields.hasErrors('passwordsEqual')}" class="form-control alert-danger"
                   placeholder="Password" th:field="*{passwordRepeat}">
            <span class="error" th:if="${#fields.hasErrors('passwordsEqual')}" th:errors="*{passwordsEqual}"></span>
        </div>

但是使用此设置,它不会加载异常Caused by: java.lang.NullPointerException at com.foxminded.university.domain.UserForm.isPasswordsEqual(UserForm.java:74)

所以我不确定我应该在 from 和哪个字段中使用 thymeleaf 模板进行注释才能使其工作?

可能在任何运行时密码为空,尝试添加验证以避免空指针。

 @AssertTrue(message = "PasswisPasswordsEqualords should match")
    public boolean isPasswordsEqual() {
        return (password == null) ? false : password.equals(passwordRepeat);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM