1.编写验证类,类需要实现Validator接口并实现validate方法
public class CustomValidator implements Validator {
/*
* @param object 需要验证的对象
* @param propertyType 对象属性类型
* @param propertyValue 对象属性值
* @param rule 使用的规则
*/
@SuppressWarnings("rawtypes")
@Override
public boolean validate(Object object, Class propertyType,
Object propertyValue, Rule rule) {
if(propertyValue != null){
//编写业务逻辑
return false;//验证不通过
}
return true;//验证通过
}
}
2.在validation-rules.xml中配置
<validators>
<validator name="custom" class="com.feiynn.validation.test.CustomValidator" message="自定义的验证器message"/>
</validators>
3.在validation-rules.xml中使用此规则
<field name="myproperty">
<rule name="custom" />
</field>