簡體   English   中英

JSF 2.1 條件字段驗證

[英]JSF 2.1 conditional field validation

我有以下 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}" />

這是我的豆子:

public class UserBean {

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

    // Getters and seeters ommited.
}

我需要通過以下方式驗證此表單:

如果選擇了“新用戶”單選按鈕,則應驗證所有表單輸入。 如果選擇“現有用戶”,我只需要驗證客戶 ID。

我嘗試了 Hibernate 驗證,還通過實現javax.faces.validator.Validator接口嘗試了自定義驗證器。

我能以某種方式實現這樣的功能嗎?

您可以編寫一個自定義驗證器來檢查父 UI 組件並僅在特定條件下執行驗證。 這很復雜。 也許看看ExtVal和它的@SkipValidation注釋?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM