SoFunction
Updated on 2025-03-02

Use ConstraintValidatorContext in springboot to verify that the content of two fields is the same

Scene

I am developing the password modification function, and I use the original password, new password and confirm the new password. I hope to verify the new password and confirm the new password through the ConstraintValidator method. The rule is that these two passwords need to be the same.

Reference Documents

  • /micronaut-projects/micronaut-core/issues/3243
  • /questions/37750656/how-to-access-a-field-which-is-described-in-annotation-property
  • /t/how-can-i-retrieve-current-validation-contexts-groups-in-a-validator/414/4

accomplish

Define Matches annotation

@Constraint(validatedBy = )
@Target({  })
@Retention()
public @interface SameContentMatches {
	String message() default "Inconsistent content";
	Class<?>[] groups() default {};
	Class<? extends Payload>[] payload() default {};
	String field(); // Add a new attribute to specify the field to compare}

Define DTO objects

@Data
public class UserModifyPasswordDTO implements UserDTO {
	@NotNull
	private String userName;
	@NotNull
	private String password;
	private String newPassword;
	@SameContentMatches(field = "newPassword")
	private String confirmPassword;
}

Define the MatchesValidator object to implement the code logic for verification

public class SameContentMatchesValidator implements ConstraintValidator<SameContentMatches, String> {
    private String field;
    @Override
    public void initialize(SameContentMatches constraintAnnotation) {
         = ();
    }
    @Override
    public boolean isValid(String object, final ConstraintValidatorContext context) {
        return true;
    }
}

Problems encountered

  • In the MatchesValidator class, the current object cannot be obtained unless the SameContentMatches annotation is applied to the current class, not the field.
  • This problem should not be solved by the main one, because you are intercepting the field, and the ConstraintValidatorContext handles information related to the current field.

Apply to the class, adjust the code, and solve the problem

@Constraint(validatedBy = )
@Target({  })
@Retention()
public @interface SameContentMatches {
	String message() default "Inconsistent content";
	Class&lt;?&gt;[] groups() default {};
	Class&lt;? extends Payload&gt;[] payload() default {};
	/**
	  * Source field name
	  * @return
	  */
	String sourceField();
	/**
	  * Target field name
	  * @return
	  */
	String destinationField();
}
public class SameContentMatchesValidator implements ConstraintValidator&lt;SameContentMatches, Object&gt; {
	private String sourceField;
	private String destinationField;
	@Override
	public void initialize(SameContentMatches constraintAnnotation) {
		 = ();
		 = ();
	}
	@Override
	public boolean isValid(Object o, final ConstraintValidatorContext context) {
		final Object sourceFieldVal = (o, );
		final Object destinationFieldVal = (o, );
		return (destinationFieldVal);
	}
}
@Data
@SameContentMatches(sourceField = "confirmPassword", destinationField = "newPassword")
public class UserModifyPasswordDTO implements UserDTO {
	@NotNull
	private String userName;
	@NotNull
	private String password;
	private String newPassword;
	private String confirmPassword;
}

The above code SameContentMatches annotation has weak encoding, and this area needs to be optimized.

This is the article about using ConstraintValidatorContext to verify the same content of two fields in springboot. For more related springboot verification fields, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!