【问题标题】:Primefaces validation not workingPrimefaces验证不起作用
【发布时间】:2015-05-27 01:31:23
【问题描述】:

我正在处理一个表单,我正在使用 primefaces。当我验证输入文本时,ajax 消息仅适用于字段而不适用于其他字段,请检查下面的代码。

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
   xmlns:h="http://java.sun.com/jsf/html"
   xmlns:f="http://java.sun.com/jsf/core"
   xmlns:p="http://primefaces.org/ui">
   <h:head>

   </h:head>
   <h:body> 
   <h2>h:validateLength Example</h2>
   <h:form>
    <p:panel id="panel" header="New User">
   <label for="name" >Name*</label>
      <p:inputText id="name" value="#{userData.name}" 
         label="name" >
         <f:validateLength minimum="7" />
         <p:ajax execute="currentInput" update="msgLastname" event="blur" process="@this"/>

      <p:message for="name" id = "msgLastname" display="icon" style="color:red" />
      </p:inputText>



      <p:inputText id="lastname" value="#{userData.lastname}">
      <f:validateLength minimum="5"/>
      <p:ajax execute="currentInput" update="msg" event="blur" process="@this"/>
        <p:message for="lastname" id="msg" display="icon"/>
      </p:inputText>


      <p:commandButton value="submit" action="result" />
    </p:panel>
   </h:form>   
</h:body>
</html>

【问题讨论】:

  • 您要验证哪些字段?
  • 嗨 - 我正在尝试验证姓氏和姓名,但 ajax 仅显示第一个字段的 msg,而不显示第二个字段

标签: jsf jsf-2 primefaces


【解决方案1】:

您需要将&lt;p:message&gt; 标记移出 &lt;p:inputText&gt; 标记,如下所示:

<p:inputText id="name" value="#{userData.name}" label="name" >
    <f:validateLength minimum="7" />
    <p:ajax execute="currentInput" update="msgLastname" event="blur" process="@this"/>
</p:inputText>

<p:message for="name" id="msgLastname" display="icon" style="color:red" />

<p:inputText id="lastname" value="#{userData.lastname}">
    <f:validateLength minimum="5"/>
    <p:ajax execute="currentInput" update="msg" event="blur" process="@this"/>
</p:inputText>

<p:message for="lastname" id="msg" display="icon"/>

如果您想在验证消息中同时显示文本图标,则删除display="icon" 参数,例如:

<p:message for="name" id="msgLastname" style="color:red" />

【讨论】:

  • 嗨,蒂姆,您能告诉我,如何添加我自己的错误消息以进行客户端验证吗?
  • FacesContext.getCurrentInstance().addMessage("name", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Input error!", "Name must be a minimum of 7 characters"));
猜你喜欢
  • 2013-11-25
  • 2011-09-04
  • 2017-09-30
  • 1970-01-01
  • 2012-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-30
相关资源
最近更新 更多