【发布时间】:2012-07-16 10:29:09
【问题描述】:
我正在开发的应用程序中的要求是,在执行搜索时,用户不应该在不输入州的情况下搜索城市,反之亦然,他们不应该在不输入城市的情况下搜索州。
search.xhtml
<h:inputText id="city" binding="#{city}" value="#{search.city}" validator="#{search.validateCity}">
<f:attribute name="state" value="#{state}"/>
</h:inputText>
<h:inputText id="state" binding="#{state}" value="#{search.state}" validator="#{search.validateState}">
<f:attribute name="city" value="#{city}"/>
</h:inputText>
Search.java
public void validateCity(FacesContext context, UIComponent component, Object convertedValue) {
UIInput stateComponent = (UIInput) component.getAttributes().get("state");
String state = (String) stateComponent.getValue();
if(convertedValue.toString().length() > 0) {
if(state.length() < 1) {
throw new ValidatorException(new FacesMessage("Please enter State."));
}
}
}
public void validateState(FacesContext context, UIComponent component, Object convertedValue) {
UIInput cityComponent = (UIInput) component.getAttributes().get("city");
String city = (String) cityComponent.getValue();
if(convertedValue.toString().length() > 0) {
if(city.length() < 1) {
throw new ValidatorException(new FacesMessage("Please enter City."));
}
}
}
我已经简化了我的代码,以展示我使用标准跨字段验证方法所做的尝试。但是,我遇到的问题是,在验证阶段,City 和 State 都显示验证错误,我猜是因为这两个验证器相互妨碍,因此造成了失败循环。
有什么办法可以解决这个问题吗?
谢谢。
【问题讨论】:
标签: validation jakarta-ee jsf-2