简体   繁体   English

条件Bean验证

[英]Conditional Bean Validation

I am using Bean Validation in my JSF project. 我在我的JSF项目中使用Bean验证。 Now I have come across a situation in which I would like to validate a method ONLY when a preceding method is validated. 现在,我遇到了一种情况,在这种情况下,仅当验证先前的方法时,我才想验证方法。

I will give an example: 我举一个例子:

@AssertTrue(message="{invalidCode}")
private boolean isValidActivationCode() { ... }

if(isValidActivationCode()) {
    @AssertTrue(message="{alreadyActivated}")
    private boolean isAlreadyActivated() { ... }
}

Since I will receive the Activation Code per parameter, I would like to validate it first. 由于我将收到每个参数的激活码,因此我想先对其进行验证。 If it is not valid, it will cause a violation. 如果无效,将导致违规。 If so, I cannot even check whether it is already activated (since the code is invalid). 如果是这样,我什至无法检查它是否已经激活(因为代码无效)。 So, is it possible to achieve anything like above mentioned (the function of the if-statement, I do know this would no way work, but it shows what I am trying to accomplish). 因此,是否有可能实现上述任何内容(if语句的功能,我确实知道这是行不通的,但是它表明了我要完成的工作)。

Thanks in advance 提前致谢

Update 更新资料

Workaround like Ravi K mentioned: 像Ravi K这样的解决方法提到:

@AssertTrue(message="{invalidCode}")
private boolean isValidActivationCode() { ... }

@AssertTrue(message="{alreadyActivated}")
private boolean isAlreadyActivated() { return isValidActivationCode() ? ... : true; }

Though I wonder, is there a clean way to solve this? 虽然我想知道,有没有一种干净的方法来解决这个问题? If nobody provides an answer soon, I will assume there is no clean solution for this and I will accept the workaround from Ravi K as the answer to this problem. 如果没有人很快提供答案,我将假定没有解决方案,并且我将接受Ravi K的解决方法作为此问题的答案。

As you said, if you feel workaround is not good one, then there are 2 options. 如您所说,如果您认为解决方法不好,那么有两种选择。

1) Go with same above workaround but make it bit more logical. 1)采取与上述相同的解决方法,但使其更具逻辑性。 Change return type of isAlreadyActivated() to Boolean instead of boolean. 将isAlreadyActivated()的返回类型更改为Boolean而不是boolean。 In isAlreadyActivated() method if isValidActivationCode() is false then return null. 在isAlreadyActivated()方法中,如果isValidActivationCode()为false,则返回null。

http://docs.oracle.com/javaee/6/api/index.html?javax/validation/constraints/package-summary.html http://docs.oracle.com/javaee/6/api/index.html?javax/validation/constraints/package-summary.html

As per above API, null are considered as valid. 根据上述API,null被视为有效。 So your logic gets more clearer. 因此,您的逻辑变得更加清晰。 true = valid, false = invalid, null = Not Applicable. true =有效,false =无效,null =不适用。 You can put the same in javadoc for that method also. 您也可以在javadoc中为该方法添加相同的内容。

 @AssertTrue(message="{invalidCode}")
    private boolean isValidActivationCode() { ... }


    @AssertTrue(message="{alreadyActivated}")
    private Boolean isAlreadyActivated() {

         if(isValidActivationCode()) {
               <Logic for isAlreadyActivated>
          } else {
             return null;
           }

    }

2) Go for custom constraint. 2)进行自定义约束。 @AssertTrue is built in constraint & jsf guys know that they are not enough. @AssertTrue是内置于约束中的,jsf的人知道这还不够。 So they have given privilege to create your own. 因此,他们拥有创建自己的特权。 So go for that. 所以去吧。 Refer below links for the same. 请参考以下链接。

http://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html/validator-customconstraints.html http://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html/validator-customconstraints.html

JSR 303 Validation, If one field equals "something", then these other fields should not be null JSR 303验证,如果一个字段等于“ something”,则这些其他字段不应为null

I think thats all we got, choice is yours :) 我认为那就是我们所拥有的,选择就是您的:)

Well I couldn't get into much depth in JSF annotation side, but I am curious why can't you simply call isValidActivationCode() in isAlreadyActivated() also ie something like below, 好吧,我无法在JSF注释方面深入了解,但是我很好奇为什么您不能简单地在isAlreadyActivated()中调用isValidActivationCode(),例如下面的内容,

@AssertTrue(message="{invalidCode}")
private boolean isValidActivationCode() { ... }


@AssertTrue(message="{alreadyActivated}")
private boolean isAlreadyActivated() {

     if(isValidActivationCode()) {
           <Logic for isAlreadyActivated>
      )

}

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

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