【问题标题】:Thymeleaf form validationThymeleaf 表单验证
【发布时间】:2020-06-09 08:11:41
【问题描述】:

我正在尝试通过双重检查来验证密码,如果密码不匹配,则会显示 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)

所以我不确定我应该在 thymeleaf 模板中注释什么以及在哪个字段中使用它才能工作?

【问题讨论】:

  • 我相信你有一个标记类型。第一个输入应该有 th:field="*{password}">(不是 passwordRepeat)
  • 对了,上面有个“密码”输入,没提,在编辑中修复了

标签: java spring spring-boot thymeleaf


【解决方案1】:

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

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

【讨论】:

  • 表单本身无法加载,即使没有字段值
【解决方案2】:

验证拦截器尝试验证所有添加的约束并将它们全部返回。您的验证方法抛出 NullPointerException 并停止它。

在您的密码字段上添加 NotEmpty 约束。

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

捕获 NullPointerException 并返回 false。

@AssertTrue(message = "Passwords should match")
public boolean isPasswordsEqual() {
    try {
        return password.equals(passwordRepeat);
    } catch(NullPointerException e) {
        return false;
    }
}

【讨论】:

    猜你喜欢
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 2020-09-18
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多