2

I have a dto like this

@FieldMatch(first = "email", second = "emailConfirm", message = "E-posta adresleri eslesmeli")
public class EmailDTO {


    private String email;


    private String emailConfirm;

this is validator

public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object> {


    private String firstFieldName;
    private String secondFieldName;
    private String message;

    @Override
    public void initialize(final FieldMatch constraintAnnotation) {
        firstFieldName = constraintAnnotation.first();
        secondFieldName = constraintAnnotation.second();
        message = constraintAnnotation.message();
    }

    @Override
    public boolean isValid(final Object value, final ConstraintValidatorContext context) {
        boolean valid = true;
        try
        {
            final Object firstObj = BeanUtils.getProperty(value, firstFieldName);
            final Object secondObj = BeanUtils.getProperty(value, secondFieldName);

            valid =  firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj);
        }
        catch (final Exception ignore)
        {
            // ignore
        }

        if (!valid){
            context.buildConstraintViolationWithTemplate(message)
                    .addPropertyNode(firstFieldName)
                    .addConstraintViolation()
                    .disableDefaultConstraintViolation();
        }

        return valid;
    }
}

this is interface

arget({TYPE, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
public @interface FieldMatch {


    String message() default "The fields must match";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    String first();
    String second();

    @Target({TYPE, ANNOTATION_TYPE})
    @Retention(RUNTIME)
    @Documented
    @interface List
    {
        FieldMatch[] value();
    }
}

but i also want to change the class to this

@FieldMatch(first = "email", second = "emailConfirm", message = "E-posta adresleri eslesmeli")
public class EmailDTO {



     @Max(value = 150, message = "E-posta karakter sayisi fazla!")
        @Email(message = "Email should be valid")
        @NotNull(message = "Name cannot be null")
        @NotEmpty(message = "Name cannot be null")
        private String email;

        @Max(value = 150, message = "E-posta karakter sayisi fazla!")
        @Email(message = "Email should be valid")
        @NotNull(message = "Name cannot be null")
        @NotEmpty(message = "Name cannot be null")
        private String emailConfirm;

Should I use another generic constraint or a lot of annotations like shown above? I also have other entities like password, etc and those entities will also have same validations.

Some fields can be nullable, so not every field has to be checked by null constraint. I want to do a very generic validation. I will send to front end (thymeleaf), so I need to see which constraint is violated.

Email has @email annotation that wont be in the password validation. Others are common. like

notnull, notempty, matching, notblank

I was not able to find good examples. I found below posts on the topic, but I could not find an example of custom + for example @email validation together.

Rov
  • 133
  • 1
  • 11
Caner Aydın
  • 195
  • 2
  • 6
  • 18

0 Answers0