JSF 2.0 validateRegex with own validator message

做~自己de王妃 提交于 2020-01-23 07:05:56

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/7415157/jsf-2-0-validateregex-with-own-validator-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!