Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2.0 validateRegex with own validator message

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 :)

like image 742
IhlieDaily Avatar asked Sep 14 '11 10:09

IhlieDaily


Video Answer


1 Answers

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.

like image 84
BalusC Avatar answered Nov 09 '22 13:11

BalusC