0

i have a dropdown field named ContactTypeName which have the values like phone, email etc and an input field named Contact i want to validate the input field for valid email address if the dropdown value Email is selected...


i have the following bean
@FieldMatch.List({    
    @FieldMatch(first = "contactDetail", second = "contectTypeName", message = "Please Enter a valid email address")
})
public class Contact {
    private int contactId = 0;
    @NotNull(message="Please Select a Contact Type")
    private Integer contectTypeId;
    private String contectTypeName;
    @NotNull(message="Please Specify Contact Details")
    private String contactDetail;   
}

im trying to create a custom constraint which will validate the contactDetail field against an email regex if the contactTypeName field will have the value Email

i have the following code for the custom constraint

@Target({TYPE, ANNOTATION_TYPE,METHOD, FIELD})
@Retention(RUNTIME)
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
public @interface FieldMatch
{
    String message() default "{constraints.fieldmatch}";

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

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

    /**
     * @return The first field
     */
    String first();

    /**
     * @return The second field
     */
    String second();

    /**
     * Defines several <code>@FieldMatch</code> annotations on the same element
     *
     * @see FieldMatch
     */
    @Target({TYPE, ANNOTATION_TYPE})
    @Retention(RUNTIME)
    @Documented
            @interface List
    {
        FieldMatch[] value();
    }
}

and the validator like

public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object>
{
    private String firstFieldName;
    private String secondFieldName;

    private Pattern pattern;
    private Matcher matcher;


    private static final String EMAIL_PATTERN = 
            "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";



    @Override
    public void initialize(final FieldMatch constraintAnnotation)
    {
        firstFieldName = constraintAnnotation.first();
        secondFieldName = constraintAnnotation.second();
        pattern = Pattern.compile(EMAIL_PATTERN);

    }

    @Override
    public boolean isValid(final Object value, final ConstraintValidatorContext context)
    {
        try
        {           
            final Object firstObj = BeanUtils.getProperty(value, firstFieldName);//email address
            final Object secondObj = BeanUtils.getProperty(value, secondFieldName);//value of the contactType

            System.out.println((String) firstObj);
            System.out.println((String) secondObj);

            if(((String) firstObj == "" || (String) firstObj == null ) && (String) secondObj != "Email"){
                return true;
            }else{
                matcher = pattern.matcher((String) firstObj);
                return matcher.matches();

            }

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

the problem is that its not firing on the form post... im using Prime Faces model dialog in which the bean to be validated is rendered

REF: Cross field validation with Hibernate Validator (JSR 303)

looking forward to your suggestions and guidance

Community
  • 1
  • 1
Dakait
  • 2,531
  • 1
  • 25
  • 43

1 Answers1

0

JSF 2.0 does not support cross field validation yet https://hibernate.onjira.com/browse/BVAL-214

to make it work either use a library like seamfaces or call the validation manually like

    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    Set<ConstraintViolation<Contact>> violations = validator.validate(contact);
Dakait
  • 2,531
  • 1
  • 25
  • 43