2015-11-05 22 views
1

我在我的應用程序中遇到了h:inputText問題。有些字段可能需要一些不需要,我想爲所有字段創建一個自定義驗證器,並且無法找到信息如何檢入驗證器類是UIComponent是否需要。如果字段是必需的,JSF如何獲取驗證器信息?

這是輸入域代碼的樣子:

<h:inputText styleClass="form-control" 
     disabled="#{cc.attrs.bean.disableAnswerPosibility(cc.attrs.answer)}" 
     value="#{cc.attrs.freeAnswer}" 
     requiredMessage="#{msg.fieldRequired}" 
     a:requiredForValidator="#{cc.attrs.question.optional}" 
     required="#{cc.attrs.question.optional}"> 
    <f:validator validatorId="stringValidator"/> 
</h:inputText> 

正如你可以看到我試圖通過這個信息作爲直通參數。它在網頁上呈現爲true/false,但是當我嘗試將其置於驗證程序類中時,它是某種類型的對象,沒有顯示在網頁源代碼中的值,但是代碼來自.xhtml文件。

這裏是代碼:

Object temp = uiComponent.getPassThroughAttributes().get("requiredForValidator"); 

,這裏是我得到了什麼:

enter image description here

我只需要值 - 布爾或字符串。有什麼建議麼?

+0

您是否嘗試評估表達式? – Kukeltje

+1

這個問題很混亂。你爲什麼不檢查'required'屬性?爲什麼要將它複製到passthrough屬性? – BalusC

回答

4

在驗證器的驗證方法中,您可以將組件轉換爲javax.faces.component.UIInput並評估其屬性如required

@Override 
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { 
     UIInput inputComponent = (UIInput) component; 
     if(inputComponent.isRequired()){ 

     } 
    } 

這個驗證器顯然只適用於UIInput組件。

+0

聖潔的廢話 - 有時我很驚訝自己......謝謝:) –

相關問題