Conditional Bean Validation

橙三吉。 提交于 2019-12-02 19:51:03

问题


I am using Bean Validation in my JSF project. 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).

Thanks in advance

Update

Workaround like Ravi K mentioned:

@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.


回答1:


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 i.e. something like below,

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


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

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

}



回答2:


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. Change return type of isAlreadyActivated() to Boolean instead of boolean. In isAlreadyActivated() method if isValidActivationCode() is false then return null.

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

As per above API, null are considered as valid. So your logic gets more clearer. true = valid, false = invalid, null = Not Applicable. You can put the same in javadoc for that method also.

 @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. @AssertTrue is built in constraint & jsf guys know that they are not enough. 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

JSR 303 Validation, If one field equals "something", then these other fields should not be null

I think thats all we got, choice is yours :)



来源:https://stackoverflow.com/questions/13011955/conditional-bean-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!