条件Bean验证 [英] Conditional Bean Validation
问题描述
我在JSF项目中使用Bean验证. 现在,我遇到了一种情况,在这种情况下,我只想在验证前面的方法时才验证方法.
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.
我举一个例子:
@AssertTrue(message="{invalidCode}")
private boolean isValidActivationCode() { ... }
if(isValidActivationCode()) {
@AssertTrue(message="{alreadyActivated}")
private boolean isAlreadyActivated() { ... }
}
由于我将收到每个参数的激活码,因此我想先对其进行验证.如果无效,将导致违规.如果是这样,我什至无法检查它是否已经激活(因为代码无效). 因此,是否有可能实现上面提到的任何功能(if语句的功能,我确实知道这是行不通的,但是它显示了我要完成的工作).
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).
预先感谢
更新
提到了类似Ravi K的解决方法:
Workaround like Ravi K mentioned:
@AssertTrue(message="{invalidCode}")
private boolean isValidActivationCode() { ... }
@AssertTrue(message="{alreadyActivated}")
private boolean isAlreadyActivated() { return isValidActivationCode() ? ... : true; }
我想知道,有没有一种干净的方法来解决这个问题? 如果没有人很快提供答案,我将假定没有解决方案,并且我将接受Ravi K的解决方法作为此问题的答案.
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.
推荐答案
好吧,我无法深入JSF注释方面,但是我很好奇为什么不能在isAlreadyActivated()中简单地调用isValidActivationCode()例如下面的内容,
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>
)
}
这篇关于条件Bean验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!