具有自己的验证器消息的JSF 2.0 validateRegex [英] JSF 2.0 validateRegex with own validator message

查看:116
本文介绍了具有自己的验证器消息的JSF 2.0 validateRegex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码与此相似:

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

支持Bean中的验证器生成了自己的验证消息.但是它会被inputText标签的validateatorMessage覆盖.

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

我的问题是:如何为validateRegex标记定义自定义验证器消息?我不想删除validatorMessage原因,然后JSF会显示自己的包含正则表达式模式的错误消息,等等->我觉得不是很漂亮.

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.

感谢您的帮助:)

推荐答案

您不能为每个单独的验证器定义单独的validatorMessage.最好的办法就是在自定义验证器中也进行正则表达式验证,以便删除validatorMessage.

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.

更新:从1.3版开始, <o:validator> JSF实用程序库的 OmniFaces 组件允许您在每个验证者的基础上设置验证者消息.您的特殊情况可以通过以下方式解决:

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>


无关与具体问题无关:如今,只要您仍然根据拉丁字符验证电子邮件地址,您就不会为世界统治做好准备.另请参见在JSF 2中使用正则表达式进行电子邮件验证/PrimeFaces .


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.

这篇关于具有自己的验证器消息的JSF 2.0 validateRegex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆