简体   繁体   English

如何在列表字段中进行bean的条件验证

[英]How to do conditional validation of bean in a list field

Conditional validation of bean in a list field. 在列表字段中对bean进行条件验证。

I have a little problem with bean validation. 我对bean验证有一点问题。 I would like to do a conditional validation, but the validated class have as a field a list of bean which must be also validated, and some of these bean's fields must be conditionnaly validated. 我想进行条件验证,但是validated类具有必须进行验证的Bean列表作为字段,并且必须对这些Bean的某些字段进行条件验证。 Here an example code : 这里是一个示例代码:

public class ParentBean {

    @Valid
    private List<ChildBean> childBeans;

}

public class ChildBean {

    boolean flag;

    @NotNull(condition="flag")
    String mustNotBeNullFlagTrue;

    String cannotBeNull();
}

I could do a loop on the child beans and validate each child separitively but the path in the errors would be wrong 我可以在子bean上做一个循环,并分别验证每个孩子,但是错误中的路径是错误的

Addittionnaly, I could use a solution like this one : Cross field validation with Hibernate Validator (JSR 303) but it seems to mess up with the path associated to the error... 另外,我可以使用这样的解决方案: 使用Hibernate Validator(JSR 303)进行跨字段验证,但似乎弄乱了与错误相关的路径...

I Find a workaround : I first validate the parent Bean then I validate each ChildBean separatively and for each error found, I add it to the set of errors of the ParentBean, updating the path in the process 我找到一种解决方法:首先验证父Bean,然后分别验证每个ChildBean,针对发现的每个错误,将其添加到ParentBean的错误集中,从而更新流程中的路径

public Errors validateParentBean(ParentBean parentBean) {
    //We create the errors list for the parent bean. 
    DefaultMessageContext msgContext = new DefaultMessageContext();
    private MessageCodesResolver messageCodesResolver = new DefaultMessageCodesResolver();
    MessageContextErrors parentBeanErrors = new MessageContextErrors(msgContext, "parentBean", null, null, messageCodesResolver, null);

    List<ChildBean> childBeanList = parentBean.getChildBeans();


    //for each childBean
    for (int childBeanIndex = 0; childBeanIndex < childBeanList.size(); childBeanIndex++) {
        ChildBean ChildBean = childBeanList.get(childBeanIndex);
        //we validate the bean. In this method we do the conditionnal validation itself
        MessageContextErrors childBeanErrors = validateChildBean(ChildBean);
        //for each error
        for (int errorIndex = 0; errorIndex < childBeanErrors.getFieldErrors().size(); errorIndex++) {
            //we "pull" it up, to the parentBean, updating the path in the way
            FieldError fieldError = (FieldError) childBeanErrors.getFieldErrors().get(errorIndex);
            parentBeanErrors.rejectValue("childBeans[" + childBeanIndex + "]."+fieldError.getField(), fieldError.getCode());
        }
    }
}

I didn't provide the code for the method validateChildBean as it is quite trivial, but don't hesitate to ask it in comments, I will be happy to oblige :) 我没有提供方法validateChildBean的代码,因为它很琐碎,但请在注释中毫不犹豫地询问,我很乐意提供:)

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

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