0
我的xhtml包含輸入字段,用戶可以在其中輸入數字或字母值。使用ajax調用我正在驗證輸入值並在其上引發驗證錯誤。但即使輸入無效字符和錯誤信息後,用戶仍可能點擊「保存」按鈕。一旦用戶點擊保存按鈕,它將在Controller中觸發一個方法。有沒有一種方法可以使用FacesContext.getCurrentInstance()在控制器內的p:messages上檢查驗證錯誤。如何使用FacesContext獲取驗證錯誤消息?
XHTML代碼
<!-- Messages -->
<p:messages id="errorMessages" globalOnly="false" showDetail="true" showSummary="false" closable="true" />
<!-- Column input field-->
<p:column>
<f:facet name="header">
<h:outputText value="Input Amount" escape="false" />
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{row.amount}" escape="false">
<f:convertNumber maxFractionDigits="3" minFractionDigits="3"
maxIntegerDigits="5" />
</h:outputText>
</f:facet>
<f:facet name="input">
<p:inputText id="input" value="#{row.amount}" maxlength="10"
size="10">
<f:convertNumber maxFractionDigits="3" minFractionDigits="3"
maxIntegerDigits="5" />
<f:validator validatorId="hourlyValueValidator" for="input" />
<p:ajax event="change" partialSubmit="true" process="input"
update=":#{p:component('errorMessages')}" />
</p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
<!-- Save Button -->
<p:commandButton id="btnSubmit" value="Save"
update=":#{p:component('tblScrollPnl')} :#{p:component('errorMessages')}"
action="#{controller.saveValues()}" ajax="true" />
控制器在ViewScope
public void saveValues() throws Exception {
FacesContext context = FacesContext.getCurrentInstance();
List<FacesMessage> errorMsgList = context.getMessageList("globalMessages");
......
......
......
}
感謝您的建議。 使它們發生變化。仍然頁面提交無效字符。在我的數據表列中,我使用cellEditor。當用戶點擊提交按鈕頁面時,可能會阻止驗證輸入字段。 – Sabarish
更新的答案,應該可以工作! –
@Saba現在能工作嗎? –