5

i am having a code similar to this:

<h:inputText id="email" value="#{managePasswordBean.forgotPasswordEmail}"
        validatorMessage="#{validate['constraints.email.notValidMessage']}"
        requiredMessage="#{validate['constraints.email.emptyMessage']}"
        validator="#{managePasswordBean.validateForgotPasswordEmail}"
        required="true">
    <f:validateRegex pattern="^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$" />
</h:inputText>

The validator in the backing bean has its own validation message generated. but it is overwritten by the validatorMessage of the inputText tag.

My Question is: how can i define a custom validator message for the validateRegex tag? I don't want to remove the validatorMessage cause then JSF is displaying an own error message containing the regex pattern and so on -> which i dont find very pretty.

Thanks for the help :)

IhlieDaily
  • 51
  • 1
  • 1
  • 4
  • 1
    You can change all the messages, but you can not have specific message for each component in your application. Take a look at this : http://www.mkyong.com/jsf2/customize-validation-error-message-in-jsf-2-0/ – Hossein May 07 '12 at 04:16

2 Answers2

13

You can't define a separate validatorMessage for each individual validator. Best what you can do is to do the regex validation in your custom validator as well, so that you can remove the validatorMessage.


Update: since version 1.3, the <o:validator> component of the JSF utility library OmniFaces allows you to set the validator message on a per-validator basis. Your particular case can then be solved as follows:

<h:inputText id="email" value="#{managePasswordBean.forgotPasswordEmail}"
        required="true" requiredMessage="#{validate['constraints.email.emptyMessage']}">
    <o:validator binding="#{managePasswordBean.validateForgotPasswordEmail}" message="#{validate['constraints.email.notValidMessage']}" />
    <o:validator validatorId="javax.faces.RegularExpression" pattern="^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$" message="Your new custom message here" />
</h:inputText>

Unrelated to the concrete problem: these days you would be not ready for world domination as long as you still validate email addresses based on Latin characters. See also Email validation using regular expression in JSF 2 / PrimeFaces.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Wow, Not being able to set a custom error message makes jsf validations less than useless! Code that is just Java spam, not worth the 2cents of hard drive space it occupies on my harddrive. – jeremyjjbrown Dec 02 '12 at 00:13
  • Omnifaces solution didn't work for me. Follow the showcase, but there are still the JSF validation message. Not a hint to my custom messages... – Felix Feb 08 '13 at 13:12
  • It's new since OmniFaces 1.3. Make sure that you've at least OmniFaces 1.3. – BalusC Feb 08 '13 at 13:13
0

This worked for me.
You can write your custom messages in "validatorMessage"

 <h:form id="form">
        <h:outputLabel for="name">Name :</h:outputLabel>  
        <h:inputText id="name" value="#{editStock.name}" required="true" requiredMessage="Name field must not be empty" validatorMessage="Your name can have only Alphabets">  
        <f:validateRegex pattern="^[a-zA-Z]*$" />  
        </h:inputText><br/>  
        <h:message for="name" style="color:red" />
        <h:commandButton value="Update" action="#{stock.update(editStock)}" style="width: 80px;"></h:commandButton> 
 </h:form> 
Ajay
  • 570
  • 8
  • 17