Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC Custom validation showing error code HV000074.

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.

like image 324
Jivan Bhandari Avatar asked Sep 16 '18 09:09

Jivan Bhandari


1 Answers

Rename messages() to message() in PasswordMatch :

public @interface PasswordMatch {
    String message() default "Sorry, passwords does not match";
    ....
}
like image 152
S.K. Avatar answered Oct 17 '22 07:10

S.K.