【问题标题】:Validator is called but error message is not displayed调用验证器但不显示错误消息
【发布时间】:2013-09-06 06:15:22
【问题描述】:

当我点击命令按钮时。正在调用 validate 方法,但未显示错误消息..

这是我的代码..

<h:form id="form">
    <h:body>
        <p:panel style="width:500px">
            <h:outputLabel for="year" value="Select Year: *" style="font-weight:bold" />
            <p:selectOneMenu id="year" value="#{leaveBean.year}">
                <f:selectItem itemLabel="Select One" itemValue="null" />
                <f:selectItems value="#{leaveBean.yearDTO}" var="currentUser" itemValue="#{currentUser.name}" itemLabel="#{currentUser.name}" />
                <f:validator validatorId="LeaveCardValidator" />
            </p:selectOneMenu>
        </p:panel>

        <p:commandButton value="Submit" action="#{leaveController.leaveCard}" update="updateList,updateDetails" id="button"/>
       <h:message for="year" style="color:red"/>

【问题讨论】:

  • 您确定您的输入无效吗?如果输入有效,则不会有任何消息。
  • 我确定我的输入无效......即使验证失败,我的操作也会被调用......
  • 不要忘记表单,作为一个单独的命名容器,所以如果您将消息 id 命名为 msg 并尝试更新它,它将是 form:msg
  • 我试过 .. 没用..

标签: jsf message validation


【解决方案1】:

不确定updateListupdateDetails 的位置在哪里,但在上面给出的示例中,您应该使用 update="@form" 代替或像这样添加:

update="updateList updateDetails @form" 

这样表单将再次呈现...

【讨论】:

  • 不,不是,您需要确保使用您的命令按钮渲染/更新h:message
  • @erencan,尝试使用简单的 h:commandButton 和简单的空 f:ajax ,因为 p:commandButton 被默认更新 @form 或类似的东西提升
  • @erencan,我不知道你有什么尝试,基于你所说的这些,但请阅读:balusc.blogspot.co.il/2006/09/…,即使验证失败,也会调用 RENDER_RESPONSE。
  • 是的@Daniel 你是对的。 p:commandButton 需要更新。
  • 在 jsf 给出的示例中显示错误消息不需要渲染表单。
【解决方案2】:

只需使用其中之一: 更新整个表格以更新内容 &lt;h:message /&gt;

<p:commandButton value="Submit" action="#{leaveController.leaveCard}" update="@form" id="button"/>

或者给&lt;h:message /&gt;一个id和id这个id给&lt;p:commandButton/&gt;
&lt;h:message id="msg" for="year" style="color:red"/&gt;

<p:commandButton value="Submit" action="#{leaveController.leaveCard}" update="updateList,updateDetails,msg" id="button"/>

【讨论】:

  • 在 firefox 中使用 firebug 并获取正确的 clientId 而不是 year
  • clientId 总是随机生成的,如果你给每个元素一个 id 它总是相同的。
【解决方案3】:

您似乎期望 JSF 在每个 ajax 请求上自动更新 &lt;h:message&gt;。这是不真实的。也许您对 PrimeFaces &lt;p:messages&gt;&lt;p:growl&gt; 感到困惑,它们每个都有一个 autoUpdate 属性,可以让您告诉他们在每个 ajax 请求时自动更新自己。

您确实需要确保 &lt;h:message&gt; 包含在 ajax 更新中。给它一个ID

<h:message id="yearMessage" ... />

并将其包含在 ajax 更新的客户端 ID 集合中

<p:commandButton ... update="updateList updateDetails yearMessage" />

另一种方法是将&lt;h:message&gt; 替换为&lt;p:messages autoUpdate="true"&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 2023-01-14
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多