簡體   English   中英

如何讓h:inputText的必需屬性依賴於h:selectOneMenu中的某些值集?

[英]How to let the required attribute of h:inputText depend on certain set of values from h:selectOneMenu?

我正在使用JSF 2.0。

我的頁面上有一個h:selectOneMenu ,它包含一個值列表,並且在同一頁面上有一個h:inputText ,其required應取決於當前所選的h:selectOneMenu值。 只有一組特定值才能觸發required檢查,其他值則不會。

這是我嘗試過的:

<h:inputText ... required="#{(selectedPaymentType.value == 'some value') || (selectedPaymentType.value == 'other value')}" />

在上面的代碼中, #{selectedPaymentType}定義在h:selectOneMenu binding

還有3個這樣的值,它們應該將required屬性觸發為true 這看起來有點笨拙。 有沒有更好的方法呢?

如果在您的設計中可以使用enum作為您的PaymentType,其中包含一些類似的界面

public interface RequireSomeMoreStuff {
   public boolean required();
}

public enum PaymentType implements RequireSomeMoreStuff {
   FOO { boolean required() { return true; } },
   BAR { boolean required() { return false; } }
}

Fant已經給出了正確方向的暗示,你應該使用具有required屬性的枚舉,但似乎你並不完全確定如何正確實現它。 不可否認,Fant的答案不夠詳細。 所以這里有一個更精細的答案。

基本上,您需要用枚舉替換所有下拉值,如下所示:

public enum PaymentType {
    FOO("Some label for foo", true),
    BAR("Some label for bar", false),
    BAZ("Some label for baz", true);

    private String label;
    private boolean required;

    private PaymentType(String label, boolean required) {
        this.label = label; 
        this.required = required;
    }

    public String getLabel() { 
        return label;
    }

    public boolean isRequired() {
        return required;
    }
}

並按如下方式使用它

<h:selectOneMenu binding="#{selectedPaymentType}" value="#{bean.selectedPaymentType}">
    <f:selectItems value="#{bean.availablePaymentTypes}" var="paymentType"
        itemValue="#{paymentType}" itemLabel="#{paymentType.label}" />
</h:selectOneMenu>
<h:inputText ... required="#{selectedPaymentType.value.required}" />

private PaymentType selectedPaymentType; // +getter+setter

public PaymentType[] getAvailablePaymentTypes() { 
    return PaymentType.values();
}

(或者如果您使用的是OmniFaces,請使用<o:importConstants> ,那么<f:selectItems>就不需要這樣的getter;不,在任何情況下都不需要轉換器,JSF / EL有已經內置的枚舉轉換)

請參閱, required屬性現在更加簡化,因為它已在與所選值關聯的模型中定義。

只有在選擇了selectOneMenu某些值時,我才會使用a4j來渲染inputText:

<h:selectOneMenu value="#{MyBean.selectedValue}">
    <f:selectItems value="#{MyBean.listOfValues}" />
    <a4j:support event="onchange" reRender="requiredText" ajaxSingle="true"/>
</h:selectOneMenu>

<a4j:outputPanel id="requiredText">
    <h:inputText value="#{MyBean.inputValue}" rendered="#{(MyBean.selectedValue == value_1) || (MyBean.selectedValue == value_2) || ...}" />
</a4j:outputPanel>

為了避免在inputText的rendered參數上使用大字符串,我建議您創建一個執行以下條件的布爾函數: rendered="#{MyBean.inputTextIsRendered}

客戶端解決方案

還有一個基於Validators的解決方案。 這是我的方法:

  • JSF代碼

上面的selectOneMenu代碼使用binding標記將selectOneMenu選擇的值與將在為inputText組件聲明的validator方法中檢索的屬性綁定。

  • 驗證器類

然后,您需要創建CheckInputValue驗證器類。

public class CheckInputValue implements Validator {
    public CheckInputValue() {}

    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {

        //We retrieve thei binded value:
        UIInput confirmComponent = (UIInput) component.getAttributes().get("selectedValue");
        String theValue = confirmComponent.getSubmittedValue();

        //Here you check the retrieved value with the list of values that makes your inputText required.
        //In these cases you will chech the inputText is not empty and, if so, you return a ValidatorException:
        if(isEmpty){
            FacesMessage message = new FacesMessage();
            message.setDetail("inputText cannot be empty");
            message.setSummary("inputText cannot be empty");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }
    }
}
  • faces-config.xml中

最后,您必須在faces-config.xml聲明您的驗證器類:

<validator>
    <validator-id>CheckInputValue</validator-id>
    <validator-class>myPackage.myValidations.CheckInputValue</validator-class>
</validator>

暫無
暫無

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

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