JSR303:尝试自定义约束违规以与类级别关系约束验证器中的子路径相关联

Kin*_*ode 6 java constraints bean-validation

我正在使用JSR303并创建了一个类级别约束,用于比较表单中的密码及其确认,我将在此处命名为@SameAs约束.理想情况下,我希望将约束与预期目标(confirmPassword)相关联,但显然封闭bean不可用于提取密码prop. - 因此是类级约束.

我有兴趣阅读其他帖子,演示如何利用类级别约束来验证关系,但找不到任何解释如何定制约束违规以与子路径相关联的内容,在这种情况下是两个字段之一在关系中.

我的问题如下:如何将约束违规的消息与'confirmPassword'字段而不是顶级对象相关联?我试图使用javax.Validator.validate(target,context)的上下文参数,但在@SameAs的验证器中添加一个Node会导致级联中的下一个约束的异常(尝试提取confirmPassword - > orderNumber属性)而不是order - > orderNumber)作为结果.

目前,我已经通过创建一个存储约束消息的额外属性来使用丑陋的kludge,该消息被拔出以供在web层上的confirmPassword输入字段附近使用.

当然我在这里遗漏了一些东西....请看下面的例子

谢谢你的评论

@Constraint( validatedBy = { SamePwdAsValidator.class})
public interface SamePwdAs {//...
}

//Using passwords in an order doesn't make sense - only for demo purpose
@SamePwdAs( message = "Password and confirmation must match" ...)
public class Order {

    @NotNull
    @Size(....)
    String pwd;

    //where I would really like to use @SameAs, and associate a violation 
    String pwdConfirm;

    @NotNull (...)
    @Pattern (....)
    String orderNumber;

    //...getters/setters
}

public class SamePwdAsValidator implements javax.validation.Validator {
//...
  public boolean isValid( Object target, ValidationContext ctx) {
   String tgt = target.getPwd(), other = target.getPwdConfirm()
   boolean isValid = tgt.equals( other);

   if ( !isValid) {
      //try to configure the context subpath for pwdConfirm to associate this constraint violation with: I tried 
      //ctx.addNode( 'pwdConfirm').addConstraintViolation() which doesn't work, as the next validator will
      //bump into trying to extract Order.pwdConfirm.orderNumber and throw a NoPropertyFoundException or the like
   }

   return isValid;
}
Run Code Online (Sandbox Code Playgroud)

opy*_*ate 5

我要给出2个答案.

答案1,努力回答您的问题:

我使用像这样的实用方法:

public static final void recreateConstraintViolation(ConstraintValidatorContext constraintValidatorContext, String errorCode, String fieldName) {
        constraintValidatorContext.disableDefaultConstraintViolation();
        constraintValidatorContext.buildConstraintViolationWithTemplate(errorCode).
        addNode(fieldName).addConstraintViolation();

}
Run Code Online (Sandbox Code Playgroud)

然后,在验证器中比较两个密码:

if ( !isValid) {
    for (String fieldName : fieldNames) {
        CustomConstraintUtils.recreateConstraintViolation(constraintValidatorContext, "password.password2.mismatch", fieldName);
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

答案2

我建议您使用像Spring Security这样的安全框架来处理密码匹配,因为我认为您的用例正在处理用户登录.