Hibernate 跨字段自定义约束注释不起作用
我有一个名为Hibernate 跨字段自定义约束注释不起作用,hibernate,jsf-2,validation,bean-validation,Hibernate,Jsf 2,Validation,Bean Validation,我有一个名为ContactTypeName的下拉字段,其中包含电话、电子邮件等值,还有一个名为Contact的输入字段。如果选择了下拉值email,我想验证输入字段的有效电子邮件地址 我有下面的豆子 @FieldMatch.List({ @FieldMatch(first = "contactDetail", second = "contectTypeName", message = "Please Enter a valid email address") }) public
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;
}
我正在尝试创建一个自定义约束,如果contactTypeName
字段的值为email
我有以下自定义约束的代码
@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;
}
}
公共类FieldMatchValidator实现ConstraintValidator
{
私有字符串firstFieldName;
私有字符串secondFieldName;
私有模式;
私人匹配器匹配器;
私有静态最终字符串电子邮件\u模式=
“^[\u A-Za-z0-9-\\+]+(\\.[u A-Za-z0-9-]+)*@”
+“[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$”;
@凌驾
公共无效初始化(最终字段匹配约束旋转)
{
firstFieldName=ConstraintAnotation.first();
secondFieldName=ConstraintAnotation.second();
pattern=pattern.compile(电子邮件模式);
}
@凌驾
公共布尔值有效(最终对象值、最终约束验证或上下文上下文)
{
尝试
{
最终对象firstObj=BeanUtils.getProperty(值,firstFieldName);//电子邮件地址
最终对象secondObj=BeanUtils.getProperty(值,secondFieldName);//contactType的值
System.out.println((字符串)firstObj);
System.out.println((字符串)secondObj);
如果(((字符串)firstObj==“”| |(字符串)firstObj==null)和(&(字符串)secondObj!=“电子邮件”){
返回true;
}否则{
matcher=pattern.matcher((字符串)firstObj);
返回matcher.matches();
}
//返回firstObj==null&&secondObj==null | | firstObj!=null&&firstObj.equals(secondObj);
}
捕获(最终异常忽略)
{
//忽略
}
返回true;
}
}
问题是它没有在表单上开火。。。im使用Prime Faces
model对话框,在该对话框中呈现要验证的bean
参考:
期待您的建议和指导JSF2.0还不支持跨领域验证 要使其正常工作,请使用库(如)或手动调用验证(如)
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Contact>> violations = validator.validate(contact);
Validator Validator=Validation.buildDefaultValidatorFactory().getValidator();
设置冲突=validator.validate(联系人);