DCestLaVie
DCestLaVie

Reputation: 61

Spring class level validation and Thymeleaf

I am learning Spring Framework and Thymeleaf. I have known how to display field error by using something like ${#fields.errors("xx")}. However, I get stuck about how to display object error message in Thymeleaf.

Here is my UserForm class:

@PasswordMatches
public class UserForm {
    @NotNull
    @NotEmpty
    private String username;
    @NotNull
    @NotEmpty
    private String password;
    @NotNull
    @NotEmpty
    private String matchingPassword;
    @NotNull
    @NotEmpty
    @ValidEmail
    private String email;

    /* setter and getter methods */

Here is my PasswordMatches annotation:

@Target({ElementType.TYPE, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) 
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = PasswordMatchesValidator.class)
@Documented
public @interface PasswordMatches { 
    String message() default "Passwords don't match";
    Class<?>[] groups() default {}; 
    Class<? extends Payload>[] payload() default {};
}

class PasswordMatchesValidator implements ConstraintValidator<PasswordMatches, Object> {  
    @Override
    public void initialize(PasswordMatches constraintAnnotation) {       
    }

    @Override
    public boolean isValid(Object obj, ConstraintValidatorContext context){   
        UserDto user = (UserDto) obj;
        return user.getPassword().equals(user.getMatchingPassword());    
  }     
}

Here is my Controller method:

@RequestMapping(value="/registration", method=RequestMethod.POST)
public ModelAndView registerUserAccount(@ModelAttribute("user") @Valid UserForm userForm,
          BindingResult result, WebRequest request, Errors errors) {
    if (!result.hasErrors()) {
        return new ModelAndView("registerSuccess");
    }
    else {
        return new ModelAndView("registration", "user", userForm);
    }
}

Now here is my problem: If the password field and confirmPass field doesn't match, how can I get the default error message returned by the class level annotation in Thymeleaf?

Upvotes: 6

Views: 3907

Answers (3)

user13779384
user13779384

Reputation: 1

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

Upvotes: 0

a4dev92
a4dev92

Reputation: 561

I know this is old post but I also encountered this problem and here is the soulution (maybe it will also help someone else): Modify PasswordMatchesValidator to this:

class PasswordMatchesValidator implements ConstraintValidator<PasswordMatches, Object> {  
@Override
public void initialize(PasswordMatches constraintAnnotation) {       
}

@Override
public boolean isValid(Object obj, ConstraintValidatorContext context){   
    UserDto user = (UserDto) obj;
    boolean isValid = user.getPassword().equals(user.getMatchingPassword());
    if(!isValid){
         context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate())
                .addPropertyNode( "matchingPassword" ).addConstraintViolation();
    }
    return isValid;

}

it will bind the validation result to your 'matchingPassword' attribute. So in your thymeleaf template us it like this:

${#fields.errors("matchingPassword")}

Upvotes: 10

holmis83
holmis83

Reputation: 16644

Add this inside the form tag:

<p data-th-each="err : ${#fields.allErrors()}" data-th-text="${err}" class="error">
  Invalid input.
</p>

Upvotes: 1

Related Questions