【问题标题】:All Validation Messages are seen in Annotated driven hibernate validation所有验证消息都可以在带注释的驱动休眠验证中看到
【发布时间】:2015-07-10 02:07:27
【问题描述】:

我正在使用休眠注释来通过弹簧表单验证来验证该字段。

这是我的模型类:

     public class NewPasword {

      @NotEmpty(message="Please Provide Password")
      @Pattern(regexp = "^(?=.*?[A-Z])(?=.*?[0-9])(?=\\S+$).{8,}$", message = "{encryptedpassword.required}")
      @Size(min=8, max=19)   
      private String newPassword;

       ........

        .......
      }

这是我的 JSP;

  .....
    <div class="row">
                <form:input path="field" />
                <form:error path="field" />
    </div>
 .....

在执行时,所有验证消息都显示在前端,这是不应该的。

任何人都可以建议我如何才能从 up 之前只获得一条验证消息。

like : 如果字段为空,则消息应该只是“请输入字段”

如果字段与模式不匹配,则消息应仅为“请输入有效字段”

如果字段为 &lt 8 和 &gt 19,则消息应仅为“长度应在 8-19 之间”

如果需要更多信息,请询问。

【问题讨论】:

    标签: java spring hibernate spring-validator


    【解决方案1】:

    您可以使用 Groups 和 GroupSequence 解决此问题

    @GroupSequence({ Step1.class, Step2.class, Step3.class })
    public class NewPasword {
    
      public interface Step1 {}
      public interface Step2 {}
      public interface Step3 {}
    
      @NotEmpty(message="Please Provide Password",groups = Step1.class)
      @Pattern(regexp = "^(?=.*?[A-Z])(?=.*?[0-9])(?=\\S+$).{8,}$", message = "{encryptedpassword.required}", groups = Step2.class)
      @Size(min=8, max=19, groups = Step3.class)   
      private String newPassword;
    
      }
    

    见参考:Grouping

    【讨论】:

      猜你喜欢
      • 2011-08-08
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多