0

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.

Menno
  • 12,175
  • 14
  • 56
  • 88

2 Answers2

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 :)

Community
  • 1
  • 1
Ravi K
  • 976
  • 7
  • 9
  • Seems like there is no out-of-the-box solution for this. I appreciate your extra research, I had been working with my workaround but I will use your `null`-solution, as it just feels like a better output. Later on I will put some effort in custom validators, though I think I can invest my time in a better way in this project for now. As said, thank you Ravi. – Menno Oct 23 '12 at 08:57
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>
      )

}
Ravi K
  • 976
  • 7
  • 9
  • Because this way the method isAlreadyActivated will still be executed, and my message will still be thrown. Only way I could manipulate this is always throw true in case of an invalid code, so this will never cause a violation. But that's like a workaround instead of a nice fix, isn't it? – Menno Oct 22 '12 at 13:36
  • Yes you are right, it is just a workaround. If you want to go for perfect solution, then it will be little harder. Lemme give you 2 more options in next answer. – Ravi K Oct 23 '12 at 05:43