JSR-303 的顺序和可验证的验证在起作用
Order of JSR-303 and Validatable validation in play
在 Play(现在是 2.6)项目中,使用了基于注释的验证(例如,@Constraints.Required
)以及通过 Validatable
(加上 @Validate
)的验证。到目前为止这一切都很好,但是从 play 2.6 开始,两者现在默认同时执行。
这会导致不幸的结果,即 validate
方法(来自 Validatable
)现在无法再确定所有其他验证是否已成功完成,因此我们必须添加各种 null -在 validate
中对每个注释已标记为非空的字段进行检查等。
在 Play 2.6 中有没有办法获得这种行为,即 validate()
仅在所有基于注释的验证规则成功完成后才被调用?
来自https://www.playframework.com/documentation/2.6.x/Migration26#form-changes:
Be aware: The “old” validate method was invoked only after all other constraints were successful before. By default class-level constraints however are called simultaneously with any other constraint annotations - no matter if they passed or failed. To (also) define an order between the constraints you can now use constraint groups.
看起来是这样的:
import javax.validation.GroupSequence;
import javax.validation.groups.Default;
@GroupSequence({ Default.class, SignUpCheck.class, LoginCheck.class })
public interface OrderedChecks { }
详情见https://www.playframework.com/documentation/2.6.x/JavaForms#advanced-validation。
在 Play(现在是 2.6)项目中,使用了基于注释的验证(例如,@Constraints.Required
)以及通过 Validatable
(加上 @Validate
)的验证。到目前为止这一切都很好,但是从 play 2.6 开始,两者现在默认同时执行。
这会导致不幸的结果,即 validate
方法(来自 Validatable
)现在无法再确定所有其他验证是否已成功完成,因此我们必须添加各种 null -在 validate
中对每个注释已标记为非空的字段进行检查等。
在 Play 2.6 中有没有办法获得这种行为,即 validate()
仅在所有基于注释的验证规则成功完成后才被调用?
来自https://www.playframework.com/documentation/2.6.x/Migration26#form-changes:
Be aware: The “old” validate method was invoked only after all other constraints were successful before. By default class-level constraints however are called simultaneously with any other constraint annotations - no matter if they passed or failed. To (also) define an order between the constraints you can now use constraint groups.
看起来是这样的:
import javax.validation.GroupSequence;
import javax.validation.groups.Default;
@GroupSequence({ Default.class, SignUpCheck.class, LoginCheck.class })
public interface OrderedChecks { }
详情见https://www.playframework.com/documentation/2.6.x/JavaForms#advanced-validation。