0

我有一个名为的下拉字段ContactTypeName,其中包含电话、电子邮件等值和一个名为Contact的输入字段,如果选择了下拉值,我想验证有效电子邮件地址的输入字段Email...


我有以下豆

@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;   
}

我试图创建一个自定义约束,如果该字段将具有值,它将contactDetail根据电子邮件正则表达式验证该字段contactTypeNameEmail

我有自定义约束的以下代码

@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();
    }
}

和验证器之类的

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;
    }
}

问题是它没有在表单上触发...我正在使用Prime Faces模型对话框,在该对话框中呈现要验证的 bean

REF:使用 Hibernate Validator 进行跨字段验证 (JSR 303)

期待您的建议和指导

4

1 回答 1

0

JSF 2.0 还不支持跨字段验证https://hibernate.onjira.com/browse/BVAL-214

要使其正常工作,可以使用seamfaces之类的库,也可以手动调用验证,例如

    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    Set<ConstraintViolation<Contact>> violations = validator.validate(contact);
于 2013-02-06T06:25:24.540 回答