控制验证注释的顺序? [英] Control validation annotations order?
问题描述
我有一个有两个验证注释的字段
@NotEmpty
@Length(min = 3,max = 100)
字符串firstName;
我只是想知道hibernate是如何指定首先执行的验证注释的顺序,并且如果可以自定义的话?
我要问的原因是,如果我将该字段留空,则有时显示的第一个验证消息不是空的,而是其他时间
预先感谢。
如果没有指定组,则约束是Default Bean Validation组的一部分(请参阅: 创建一个界面作为你的扩展(或任何你想要的调用它)group: 现在创建一个接口,它将包含 在约束上设置验证组 将MySequence传递到您的验证器调用。 由您的 i have a field that have two validation annotations i am just curious to know how hibernate is specifying the order of which validation annotation to be executed first, and if it's possible to custom that ? the reason i am asking is that if i left that field empty sometimes the first validation message displayed is for not empty and other times if i left it empty i get the validation message for the length annotation. thanks in advance. Use JSR-303 validation groups. If no groups are specified a constraint is part of the Default Bean Validation group (see: Create an interface to be your "Extended" (or whatever you want to call it) group: Now create an interface that will have the Set the validation groups on your constraints Pass MySequence to your validator call. As specified by your 这篇关于控制验证注释的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! b
$ b
public interface Extended {}
javax.validation.GroupSequence
注释。
@GroupSequence({Default.class,Extended.class})
public interface MySequence {}
@NotEmpty //如果没有指定组,则它是默认组的一部分
@Length(min = 3,max = 100,groups = Extended.class)
String firstName;
validator.validate(object,MySequence.class);
@GroupSequence
指定,默认首先会验证约束条件,如果没有遇到违规违规行为,它将转到扩展组。@NotEmpty
@Length(min=3,max=100)
String firstName;
javax.validation.groups.Default
).public interface Extended{}
javax.validation.GroupSequence
annotation.@GroupSequence({Default.class, Extended.class})
public interface MySequence {}
@NotEmpty // If no group is specified it is part of the default group
@Length(min=3,max=100, groups = Extended.class)
String firstName;
validator.validate(object, MySequence.class);
@GroupSequence
the default constraints will be validated first and if no contraint violations are encountered it will move on to the extended group.