JSF 2.1 条件字段验证
我有以下 JSF 2.1 页面
<h:selectOneRadio value="#{userBean.newUser}">
<f:selectItem itemValue="0" itemLabel="new User" />
<f:selectItem itemValue="1" itemLabel="existing User" />
</h:selectOneRadio>
<br />
<h:inputText value="#{userBean.customerId}" id="customerId" />
<h:message for="customerid" />
<br />
<h:inputText value="#{userBean.firstName}" id="firstName" />
<h:message for="fisrtName" />
<br />
<h:inputText value="#{userBean.lastName}" id="lastName" />
<h:message for="lastName" />
<br />
<h:commandButton value="Submit" action="#{userBean.login}" />
,这是我的 bean:
public class UserBean {
private String customerId;
private String newUser= "0";
private String firstName;
private String lastName;
// Getters and seeters ommited.
}
我需要通过以下方式验证此表单:
如果选择了“新用户”单选按钮,则应验证所有表单输入。如果选择“现有用户”,我只需要验证客户 ID。
我尝试了 Hibernate Validation,还通过实现 javax.faces.validator.Validator 接口尝试了自定义验证器。
我可以以某种方式实现这样的功能吗?
I have the following JSF 2.1 page
<h:selectOneRadio value="#{userBean.newUser}">
<f:selectItem itemValue="0" itemLabel="new User" />
<f:selectItem itemValue="1" itemLabel="existing User" />
</h:selectOneRadio>
<br />
<h:inputText value="#{userBean.customerId}" id="customerId" />
<h:message for="customerid" />
<br />
<h:inputText value="#{userBean.firstName}" id="firstName" />
<h:message for="fisrtName" />
<br />
<h:inputText value="#{userBean.lastName}" id="lastName" />
<h:message for="lastName" />
<br />
<h:commandButton value="Submit" action="#{userBean.login}" />
and this is my bean:
public class UserBean {
private String customerId;
private String newUser= "0";
private String firstName;
private String lastName;
// Getters and seeters ommited.
}
I need to validate this form in the following way:
If the "new User" radio button selected, all form inputs should be validated. If "existing User" is selected I need to validate only the Customer Id.
I tried Hibernate Validation and I also tried a custom validator by implementing javax.faces.validator.Validator
interface.
Can I achieve such functionality somehow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以编写一个自定义验证器来检查父级UI 组件并仅在某些条件下执行验证。这相当复杂。也许看看 ExtVal 及其
@SkipValidation
注释?You can write a custom validator which examines the parent UI component and performs validations only under certain conditions. It's quite complex. Maybe look at ExtVal and its
@SkipValidation
annotation?