在验证失败的对话框中自动显示验证消息 [英] Automatically show validation messages in p:dialog on validation failure
问题描述
我想在< p:dialog>
组件中提供支持bean的验证消息和消息。在我的JSF页面中,我有以下对话框定义:
I want to present validation messages and messages from backing bean in <p:dialog>
component. In my JSF page I have following dialog defined:
<p:dialog widgetVar="messageDialog" id="msgDialog" modal="true" appendToBody="true">
<h:form id="messageForm">
<p:messages id="messagesInDialog" />
<p:commandButton value="OK" onclick="messageDialog.hide()" />
</h:form>
</p:dialog>
在后台bean附加一些消息后,我执行以下代码:
I execute the following code after appending some message in backing bean:
RequestContext.getCurrentInstance().execute("messageDialog.show()");
可以正常运行。
,我也想在此对话框中显示bean验证消息。消息附加到< p:message>
组件afer验证,但我不知道如何在验证失败后显示此对话框。
However, I also want to display bean validation messages in this dialog. Messages are appended to <p:message>
component afer validation, but I have no idea how to display this dialog after validation failure.
如何实现?
推荐答案
您可以使用 visible
属性
< p:dialog>
指定对话框是否默认显示。您可以通过 FacesContext# isValidationFailed()
如果有验证失败。
You can use the visible
attribute of <p:dialog>
to specify whether the dialog should show up by default or not. You can check by FacesContext#isValidationFailed()
if there's a validation failure or not.
简而言之,
<p:dialog id="msgDialog" widgetVar="messageDialog" modal="true" appendToBody="true"
visible="#{facesContext.validationFailed}">
<p:messages id="messagesInDialog" />
<p:button value="OK" onclick="messageDialog.hide()" />
</p:dialog>
(请注意,我简化了不必要的 h:form
和
p:commandButton
通过 p:button
)
然后由...更新:
<p:commandButton value="submit" update=":msgDialog" />
或者将它放在< p:outputPanel autoUpdate = true>
,以便自动更新每个ajax请求,而无需在每个更新
属性中指定它:
Or by just placing it inside a <p:outputPanel autoUpdate="true">
so that it auto-updates itself on every ajax request without the need to specify it in every update
attribute:
<p:outputPanel autoUpdate="true">
<p:dialog id="msgDialog" widgetVar="messageDialog" modal="true" appendToBody="true"
visible="#{facesContext.validationFailed}">
<p:messages id="messagesInDialog" />
<p:button value="OK" onclick="messageDialog.hide()" />
</p:dialog>
</p:outputPanel>
另请参见:
- 呈现和可见属性之间的差异; p:dialog>
- Difference between rendered and visible attributes of <p:dialog>
See also:
无关 FacesContext#getMessageList()
不为空。
Unrelated to the concrete problem, to cover non-validation messages, such as those global messages which you add in the action method, rather check instead if FacesContext#getMessageList()
is not empty.
<p:dialog ... visible="#{not empty facesContext.messageList}">
当有任何消息时,这将显示对话框。这样, RequestContext#execute()
调用是不必要的。
This will then show the dialog when there is any message. This way that RequestContext#execute()
call is unnecessary.
这篇关于在验证失败的对话框中自动显示验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!