8

I am trying to implement a custom password match validation in spring boot. But I am getting an error as follows:-

PasswordMatch contains Constraint annotation, but does not contain a message parameter

I am following this link https://www.baeldung.com/registration-with-spring-mvc-and-spring-security for custom validation. The problem is I am getting error as this.

javax.validation.ConstraintDefinitionException: HV000074: com.bikram.booking.validation.PasswordMatch contains Constraint annotation, but does not contain a message parameter. at org.hibernate.validator.internal.metadata.core.ConstraintHelper.assertMessageParameterExists(ConstraintHelper.java:915)

I have searched solutions on web but couldn't find the decent soultion.

My Modal is

package com.bikram.booking.dto;

import com.bikram.booking.validation.PasswordMatch;
import com.bikram.booking.validation.ValidEmail;

import javax.validation.constraints.*;

@PasswordMatch
public class UserDto {
 @NotNull
    @Size(min = 6, message = "Password should be more than 6 characters")
    @NotEmpty(message = "Please provide a password")
    private String password;


    @NotNull
    @Size(min = 6, message = "Password should be more than 6 characters")
    private String confirmPassword;
}

My Interface is

package com.bikram.booking.validation;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target({ TYPE, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = PasswordMatchValidator.class)
@Documented
public @interface PasswordMatch {
    String messages() default "Sorry, passwords does not match";

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

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

And Implementation is

package com.bikram.booking.validation;

import com.bikram.booking.dto.UserDto;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class PasswordMatchValidator implements ConstraintValidator<PasswordMatch, Object> {
    @Override
    public void initialize(PasswordMatch constraintAnnotation) {

    }

    @Override
    public boolean isValid(Object obj, ConstraintValidatorContext constraintValidatorContext) {
        UserDto userDto = (UserDto) obj;
        return true;
    }
}

Any hints will be higly appreciable.

Jivan Bhandari
  • 860
  • 1
  • 10
  • 32

1 Answers1

8

Rename messages() to message() in PasswordMatch :

public @interface PasswordMatch {
    String message() default "Sorry, passwords does not match";
    ....
}
S.K.
  • 3,597
  • 2
  • 16
  • 31
  • Nice catch. The error is gone now. But I am not getting any password mismatch error on client side. Any idea on that. – Jivan Bhandari Sep 16 '18 at 10:48
  • Try to debug your code and see where the message is getting lost. If the answer helped...please upvote/accept it. Thanks! – S.K. Sep 16 '18 at 10:52
  • @S.K. why did you rename it to message? – java dev May 21 '22 at 13:27
  • 1
    @devloper152 because that's just what Javax requires. Java's typesystem is limited so all of this stuff is done through reflection, but "message" is the field that it expects to find, and it throws exceptions telling you about it if you don't cooperate. – mirichan Jun 09 '22 at 16:55