5

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?

1 Answer 1

6

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.