Spring + Thymeleaf自定义验证显示

我一直在尝试使自定义javax验证正常工作(Spring Boot和Thymeleaf),但是我不知道如何显示错误消息。问题似乎是“正常”错误(例如@

Size,@

NotNull等)似乎将FieldError添加到绑定结果中。我的自定义验证器虽然提供了ObjectError。我不知道如何让Thymeleaf来显示我的自定义错误(由于th:errors="*{*}"显示了此错误,显然已经通过了)。

任何帮助是极大的赞赏。

更新:我现在可以通过显示错误消息

<p th:if="${#fields.hasErrors('${user}')}" th:errors="${user}"></p>

但是,如果我需要多个验证者(例如,确认密码和确认电子邮件),则该解决方案将无法正常工作(如果一个不合适,则显示两个错误消息。如果有任何建议,请不要犹豫)。

以下是我为此使用的代码:

模板:

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

<!-- works just fine -->

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

<!-- works and displays all errors (for every field with an error,

including my custom validator) -->

<p th:if="${#fields.hasErrors('confirmPassword')}" th:errors="*{*}"></p>

<!-- does not work -->

<p th:if="${#fields.hasErrors('*')}" th:errors="*{confirmPassword}"></p>

<!-- does not work -->

验证器实现:

public class PasswordsEqualConstraintValidator implements

ConstraintValidator<PasswordsEqualConstraint, Object> {

@Override

public void initialize(PasswordsEqualConstraint arg0) {

}

@Override

public boolean isValid(Object candidate, ConstraintValidatorContext arg1) {

User user = (User) candidate;

return user.getPassword().equals(user.getConfirmPassword());

}

}

验证器界面:

@Target({ ElementType.TYPE })

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Constraint(validatedBy = PasswordsEqualConstraintValidator.class)

public @interface PasswordsEqualConstraint {

String message();

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

}

User.java:

@PasswordsEqualConstraint(message = "passwords are not equal")

public class User implements java.io.Serializable {

...

@Size(min=2, max=40)

private String username;

...

private String confirmPassword;

...

控制器:

@RequestMapping(value = "/signup", method = RequestMethod.POST)

public String signup(@Valid User user, BindingResult bindingResult) {

if (bindingResult.hasErrors()) {

return "signup";

}

... do db stuff .. return "success";

}

回答:

这可能是因为您的@PasswordsEqualConstraint被分配给整个bean(类型)而不是字段“

confirmPassword”。要将可能违反约束的条件添加到具体字段,您可以像以下示例一样进行操作。

如果两个字段不相等,则FieldMatch比较两个字段,然后将验证错误分配给第二个字段。

顺便说一句。这是您正在做的事情的更通用的解决方案。密码示例,您可以像这样使用它

@FieldMatch(first = "password", second = "confirmPassword", message = "Passowords are not equal.")

验证器:

public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object> {

private String firstFieldName;

private String secondFieldName;

@Override

public void initialize(final FieldMatch constraintAnnotation) {

firstFieldName = constraintAnnotation.first();

secondFieldName = constraintAnnotation.second();

}

@Override

public boolean isValid(final Object value, final ConstraintValidatorContext context) {

try {

final Object firstObj = BeanUtils.getProperty(value, firstFieldName);

final Object secondObj = BeanUtils.getProperty(value, secondFieldName);

boolean isValid = firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj);

if (!isValid) {

context.disableDefaultConstraintViolation();

context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()).addNode(secondFieldName).addConstraintViolation();

}

return isValid;

}

catch (final Exception ignore) {

// ignore

}

return true;

}

}

以上是 Spring + Thymeleaf自定义验证显示 的全部内容, 来源链接: utcz.com/qa/426490.html

回到顶部