jsf正则表达式的多个自定义验证器消息 [英] jsf multiple custom validator messages for regex
问题描述
我需要验证表单中的两个字段:电子邮件和姓名.电子邮件应与模式a.a@x.x相匹配,并且名称应仅包含字母.
I need to validate two fields in a form: email and name. The email should match the patern a.a@x.x and the name should only contain letters.
我有custom-faces-messages.properties,其中包含当模式不匹配时需要显示的消息.
I have the custom-faces-messages.properties with the message I need to display when the pattern is not matching.
javax.faces.validator.RegexValidator.NOT_MATCHED = {1}:验证 错误:值不符合模式"{0}"
javax.faces.validator.RegexValidator.NOT_MATCHED = {1}: Validation Error: Value not according to pattern ''{0}''
但是,我不想用{0}来以丑陋的方式显示模式,而是希望为每次验证显示一个自定义文本.例如,名称应为名称只能包含字母",电子邮件为电子邮件不匹配该模式.示例a.a@x.x'
However, instead of {0} which displays the pattern in a pretty ugly way, I would like to display a custom text for every validation. Ex for name it should be 'Name can only contain letters' and for email 'Email does not match the pattern. Example a.a@x.x'
是否可以使用属性文件来执行此操作?
Is there a way to do this using the properties file?
谢谢.
推荐答案
如果您的电子邮件和姓名字段实现为h:inputText
,则可以为此目的使用validator
和validatorMessage
属性.参见 JSF VDL文档.
If your email and name fields are implemented as h:inputText
, then you can use validator
and validatorMessage
attributes for that purpose. See JSF VDL document.
第二种(更复杂的)方式:创建自己的验证器.我有自己的电子邮件验证程序,并且在应用程序中这样称呼它:
2nd (more complicated) way: create your own validator. I have own email validator and I call it in application like this:
<h:inputText id="email" label="#{msg.email}"
value="#{registrationForm.email}"
size="40" maxlength="80" required="true">
<f:converter converterId="lowerCase" />
<f:validator validatorId="email-validator" />
</h:inputText>
第三种方式:在备用bean中检查它并显示所需的消息.
3rd way: check it in backing bean and display message which you want.
这篇关于jsf正则表达式的多个自定义验证器消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!