Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsf inputText and validateRegexPatter

i want to display the label attribute in the message:

    <h:inputText id="email" label="#{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?

like image 872
Roberto de Santis Avatar asked Jan 26 '11 11:01

Roberto de Santis


1 Answers

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>
like image 51
victor herrera Avatar answered Sep 21 '22 21:09

victor herrera