开发者

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


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.


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> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜