jsf一次验证两个字段 [英] jsf validate two fields in one time
问题描述
我可以使用一个验证器来验证两个相互依赖的字段吗?
can I validate two interdependent fields in with one validator?
<h:form>
<h:inputText value="#{logRegBean.person.name}" >
<f:validator validatorId="loginCorrectValidator" />
</h:inputText>
<h:inputSecret value="#{logRegBean.person.password}" />
<h:commandButton action="#{logRegBean.login}" />
</h:form>
我想在数据库中搜索该用户,如果有该用户,我将测试密码(在db中和输入的密码)是否匹配.但是,如何才能甚至在一个验证器中访问密码字段呢?我试图通过createValueExpression()
在另一个字段中评估值,但是由于我总是得到空字符串,因此我似乎无法访问该时间值.
I want to search for the user in the DB and if there is the user, I'll test if the passwords(in db and inputted) match. But how can I access even the password field in one validator? I tried to evaluate the value int the other field via createValueExpression()
, but it looks like I can't access the value in that time since I always get empty strings.
推荐答案
最好的方法是抓住另一个validate()方法中的UIComponentBase.html#findComponent-java.lang.String-"rel =" noreferrer> UIViewRoot#findComponent()
,然后通过 UIInput#getValue()
(当它出现在当前组件的之前且已被验证).
Best what you can do is to grab the other UIInput
component by UIViewRoot#findComponent()
inside the validate()
method and then determine the submitted value by either UIInput#getSubmittedValue()
(when it occurs after the currently validated component in the component tree) or UIInput#getValue()
(when it occurs before the current component and thus is already validated).
例如
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
UIInput otherInput = (UIInput) context.getViewRoot().findComponent("clientId");
String otherValue = (String) otherInput.getSubmittedValue();
// ...
}
另请参见:
- JSF不支持跨域验证,有解决方法吗?
- JSF doesn't support cross-field validation, is there a workaround?
See also:
这篇关于jsf一次验证两个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!