2012-10-09 39 views
0

我試圖將正常的bean驗證附加到以編程方式生成的Primefaces UIComponent上的模糊事件。如何將ajax驗證添加到以編程方式生成的primefaces組件

如果我手動創建使用XHTML的組件,它工作得很好,它看起來像這樣:

<p:inputText id="customerName" value="#{editCustomerBean.name}" label="Name"> 
    <p:ajax async="true" event="blur" update="customerName, customerNameMsg" /> 
</p:inputText> 

不幸的是我需要產生的,因爲這將基於填充一些動態屬性的飛這個組件運行時數據。該代碼我寫了嘗試,並準確地複製此組件:當我生成這樣,這個組件

UIInput input = new InputText(); 

AjaxBehavior ajax = new AjaxBehavior(); 
ajax.setAsync(true); 
ajax.setUpdate("customerName, customerNameMsg"); 
input.addClientBehavior("blur", ajax); 
input.setId("customerName"); 
input.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{editCustomerBean.name}", String.class)); 

,我看到發送到執行模糊事件的服務器的請求,但沒有確認發生。這是發佈的請求看起來相同的是,當我指定XHTML組件被髮送:

javax.faces.partial.ajax=true&javax.faces.source=mainForm%3AcustomerName&javax.faces.partial.execute=mainForm%3AcustomerName&javax.faces.partial.render=mainForm%3AcustomerName+mainForm%3AcustomerNameMsg&javax.faces.behavior.event=blur&javax.faces.partial.event=blur&mainForm%3AcustomerName=&javax.faces.ViewState=8176624577669857830%3A-4154840965136338204 

我已經看到張貼本網站和Primefaces論壇上類似的問題,但它通常涉及連接監聽器的方法AjaxBehavior,這不是我在這裏要做的。當沒有指定偵聽器時,我希望該行爲與標記相同,即驗證字段。

回答

1

原來我正在吠叫看着ajax組件的錯誤樹。我今天意識到,我的組件在提交時未被驗證。事實證明,當您動態創建JSF組件時,您需要手動向組件註冊BeanValidator。感謝維克多·埃雷拉他對這個問題的回答:https://stackoverflow.com/a/7055586/1535568

2

的實例添加onBlur事件動態

Message message=new Message(); 
    message.setId("msg"); 
    InputText it = new InputText(); 
    it.setId("input1"); 
    it.setRequired(true); 
    message.setFor(it.getId()); 

    //******Setting validation render at onBlur event 
    AjaxBehavior ajaxBehavior=new AjaxBehavior(); 
    ajaxBehavior.setAsync(true); 
    ajaxBehavior.setUpdate(message.getId()); 
    it.addClientBehavior("blur", ajaxBehavior); 
    //************************************************ 
相關問題