jsf inputText and validateRegexPatter

10,664

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>
Share:
10,664
Roberto de Santis
Author by

Roberto de Santis

Updated on June 04, 2022

Comments

  • Roberto de Santis
    Roberto de Santis almost 2 years

    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?