开发者

jsf inputText and validateRegexPatter

i want to display the label attribute in the message:

    <h:inputText id="email" label开发者_StackOverflow中文版="#{sW.email}" value="#{contattiBean.contatto.email}"
                                 required="true">
              <f:param value="#{sW.email}" />
              <f:validateRegex pattern="[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]"/>
    </h:inputText>

i have setted the f:param because in the

`javax.faces.validator.RegexValidator.NOT_MATCHED={0}: Valore non valido`

the {0} is substituted with the regex pattern. While, i what to display the label value. My solution doesn't work how can i do it?


I suppose you are using Mojarra, because checking the source of javax.faces.validator.RegexValidator I notice the param of the validation message is only the pattern, the label is never passed to the message formatter and you can't use it in your own custom messages.

//From javax.faces.validator.RegexValidator source
if (!matcher.matches()) {
   Object[] params = { regex }; 
   fmsg = MessageFactory.getMessage(locale, NOT_MATCHED_MESSAGE_ID, params);
   throw new ValidatorException(fmsg);
}

In MyFaces sources, appears that they do pass both pattern and label.

There are at least two simple options: Use MyFaces or better use the validatorMessage attribute of your input component.

validatorMessage description is A ValueExpression enabled attribute that, if present, will be used as the text of the validator message, replacing any message that comes from the validator.

 <h:inputText id="email" label="#{sW.email}" 
         value="#{contattiBean.contatto.email}"
         required="true" validatorMessage="#{sW.email} is not valid">
          <f:validateRegex pattern="[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]"/>
 </h:inputText>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜