简体   繁体   English

Java播放! 框架表格验证顺序

[英]Java Play! Framework form validation order

On Play! 播放中! Framework 2.6 I have implemented the following form which requires a custom validation: 框架2.6我实现了以下形式,需要自定义验证:

@Validate
public class MachineRegistrationForm implements Validatable<List<ValidationError>> {

    @Required
    private String field1;

    @Required
    private String field2;

    // other fields, getters and setters

    @Override
    public List<ValidationError> validate() {

        // validation on field1 and field2

    }
}

Looks like Play performs my custom validation before checking if the @Required fields field1 and field2 actually contains some values, forcing me to check if the values are null to avoid NullPointerExceptions. 看起来Play 检查@Required字段field1field2是否实际包含一些值之前执行了我的自定义验证,迫使我检查这些值是否为null以避免NullPointerExceptions。 Am I following the wrong approach to custom validation or is this a Play! 我是按照错误的方式进行自定义验证还是这是Play! unintended behaviour? 意外的行为?

TLDR: RTFM ;-) TLDR:RTFM ;-)

In your example the validate() method is not evaluated before the two @Required constraints: The three constraints are called simultaneously - so basically there is no guaranteed order what will run first. 在您的示例中, validate()方法没有在两个@Required约束之前求值:这三个约束被同时调用-因此,基本上没有保证先运行的约束。 This is also documented in the Play Framework documentation : Play框架文档中也对此进行了记录

Also be aware that in this example the validate method and the @Constraints.Required constraint will be called simultaneously - so the validate method will be called no matter if @Constraints.Required was successful or not (and vice versa). 还要注意,在此示例中, validate方法和@Constraints.Required约束将同时调用-因此,无论@Constraints.Required成功与否,都会调用validate方法。 You will learn how to introduce an order later on. 稍后您将学习如何引入订单。

If you carefully read the docs you will then find the section "Defining the order of constraint groups" : 如果您仔细阅读了文档,则将找到“定义约束组的顺序”部分

You can validate groups in sequences. 您可以按顺序验证组。 This means groups will be validated one after another - but the next group will only be validated if the previous group was validated successfully before. 这意味着将一个接一个地验证各个组-但是只有在之前的组之前已成功验证过后,才能验证下一个组。 (However right now it's not possible to determine the order of how constraints will be validated within a group itself...) (但是,现在无法确定在组自身中如何验证约束的顺序...)

To make that work for you example let's great the group... 为了使该示例为您所用,让我们一起努力吧……

public interface First { }

...and... ...和...

public interface Second { }

...and you would have to great following group sequence: ...并且您必须遵循以下良好的小组顺序:

import javax.validation.GroupSequence;
import javax.validation.groups.Default;

@GroupSequence({ Default.class, First.class, Second.class })
public interface OrderedChecks { }

Then you have to add the groups to your constraints: 然后,您必须将组添加到约束中:

@Validate(groups = {Second.class})
public class MachineRegistrationForm implements Validatable<List<ValidationError>> {

    @Required // Default group is Default.class
    private String field1;

    @Required(groups = {First.class})
    private String field2;

    // other fields, getters and setters

    @Override
    public List<ValidationError> validate() {

        // validation on field1 and field2

    }
}

Now you can trigger the validation with the defined order: 现在,您可以按定义的顺序触发验证:

Form<MachineRegistrationForm> form = formFactory().form(MachineRegistrationForm.class, OrderedChecks.class).bindFromRequest();

Now field1 will be checked first and only if it succeeded then field2 will be checked and only if that one succeeded as well the validate method will be checked. 现在,将首先检查field1并且只有在成功的情况下才进行检查,并且只有在该field2也成功的情况下,才会对validate方法进行检查。 You could also remove (groups = {First.class}) from field2 (so it has the Default.class group by default as well) or , the other way around, add (groups = {First.class}) to field1 - in both cases now the two fields would be evaluated simultaneously - and only if both succeed then validate would be evaluted. 您也可以删除(groups = {First.class})field2 (因此它具有Default.class默认组也一样), 或者反过来,添加(groups = {First.class})field1 -中在这两种情况下,将同时评估这两个字段-并且只有两个都成功时,才会评估validate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM