JSF 2.1条件字段验证

dan*_*nik 5 java jsf richfaces java-ee jsf-2

我有以下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}" />
Run Code Online (Sandbox Code Playgroud)

这是我的豆子:

public class UserBean {

    private String customerId;
    private String newUser= "0";
    private String firstName;
    private String lastName;

    // Getters and seeters ommited.
}
Run Code Online (Sandbox Code Playgroud)

我需要通过以下方式验证此表单:

如果选择"新用户"单选按钮,则应验证所有表单输入.如果选择"现有用户",我只需要验证客户ID.

我尝试了Hibernate验证,我还通过实现javax.faces.validator.Validator接口尝试了自定义验证器.

我能以某种方式实现这样的功能吗?

zac*_*usz 3

您可以编写一个自定义验证器来检查父 UI 组件并仅在某些条件下执行验证。这相当复杂。也许看看ExtVal及其@SkipValidation注释?

@SkipValidation("#{person.role eq 'admin'}")
@Required
@Equals("person.password")
@NotEquals("password")
private String oldPassword;
Run Code Online (Sandbox Code Playgroud)