亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Hibernate Validator交叉字段驗證(JSR 303)

Hibernate Validator交叉字段驗證(JSR 303)

Hibernate Validator交叉字段驗證(JSR 303)Hibernate Validator 4.x中是否有交叉字段驗證的實現(或第三方實現)?如果不是,實現交叉字段驗證器的最干凈的方法是什么?例如,如何使用API來驗證兩個bean屬性是否相等(例如驗證密碼字段與密碼驗證字段匹配)。在注釋中,我希望類似于:public class MyBean {   @Size(min=6, max=50)   private String pass;   @Equals(property="pass")   private String passVerify;}
查看完整描述

3 回答

?
縹緲止盈

TA貢獻2041條經驗 獲得超4個贊

我建議你另一個可能的解決辦法。也許不那么優雅,但更容易!

public class MyBean {
  @Size(min=6, max=50)
  private String pass;

  private String passVerify;

  @AssertTrue(message="passVerify field should be equal than pass field")
  private boolean isValid() {
    return this.pass.equals(this.passVerify);
  }}

這個isValid方法由驗證器自動調用。


查看完整回答
反對 回復 2019-06-19
?
森欄

TA貢獻1810條經驗 獲得超5個贊

我很驚訝這個沒被打開。無論如何,這里有一個可能的解決方案。

我已經創建了一個類級驗證器,而不是原始問題中描述的字段級別。

下面是注釋代碼:

package com.moa.podium.util.constraints;import static java.lang.annotation.ElementType.*;import static java.lang.annotation.RetentionPolicy.*;
import java.lang.annotation.Documented;import java.lang.annotation.Retention;import java.lang.annotation.Target;
import javax.validation.Constraint;import javax.validation.Payload;@Target({TYPE, ANNOTATION_TYPE})@Retention(RUNTIME)
@Constraint(validatedBy = MatchesValidator.class)@Documentedpublic @interface Matches {

  String message() default "{com.moa.podium.util.constraints.matches}";

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

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

  String field();

  String verifyField();}

而驗證器本身:

package com.moa.podium.util.constraints;import org.mvel2.MVEL;import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;public class MatchesValidator implements ConstraintValidator<Matches, Object> {

  private String field;
  private String verifyField;


  public void initialize(Matches constraintAnnotation) {
    this.field = constraintAnnotation.field();
    this.verifyField = constraintAnnotation.verifyField();
  }

  public boolean isValid(Object value, ConstraintValidatorContext context) {
    Object fieldObj = MVEL.getProperty(field, value);
    Object verifyFieldObj = MVEL.getProperty(verifyField, value);

    boolean neitherSet = (fieldObj == null) && (verifyFieldObj == null);

    if (neitherSet) {
      return true;
    }

    boolean matches = (fieldObj != null) && fieldObj.equals(verifyFieldObj);

    if (!matches) {
      context.disableDefaultConstraintViolation();
      context.buildConstraintViolationWithTemplate("message")
          .addNode(verifyField)
          .addConstraintViolation();
    }

    return matches;
  }}

注意,我使用了MVEL來檢查被驗證對象的屬性。這可以用標準反射API代替,或者如果它是您正在驗證的特定類,則訪問器方法本身。

然后,可以在bean上使用@Matters注釋,如下所示:

@Matches(field="pass", verifyField="passRepeat")public class AccountCreateForm {

  @Size(min=6, max=50)
  private String pass;
  private String passRepeat;

  ...}

作為一個免責聲明,我在最后5分鐘寫了這個,所以我可能還沒有解決所有的bug。如果出了什么問題,我會更新答案的。


查看完整回答
反對 回復 2019-06-19
  • 3 回答
  • 0 關注
  • 2192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號