在validatorMessage中显示值

时间:2015-11-09 13:01:26

标签: primefaces

我正在使用validatorMessage作为电子邮件输入的正则表达式,但我想在消息的输入中显示当前值{0}。

但是如果我使用bean中声明的变量它不起作用,因为它没有处理变量,因为存在验证错误。如何在validatorMessage的输入中显示当前值?

注意:{0}在PrimeFaces语言环境中用于验证当前值。

<p:inputText id="email" value="#{registerBean.email}" type="email"  label="#{bundle.register_email}" required="true" style="width: 100%;" validatorMessage="#{bundle.register_email}: '{0}' #{bundle.register_email_error}">
    <f:validateRegex pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$" />
</p:inputText>

1 个答案:

答案 0 :(得分:2)

我建议的解决方案是使用自定义的“messages.properties”文件并覆盖JSF API的默认消息。

有关此文件的示例,请重定向到:jsf-api-2.x.jar,“javax \ faces \ Messages.properties”。 以文件messages.properties

中的两个值为例

enter image description here

javax.faces.validator.LengthValidator.MAXIMUM={1}: Validation Error: Length is greater than allowable maximum of ''{0}''

javax.faces.validator.LengthValidator.MINIMUM={1}: Validation Error: Length is less than allowable minimum of ''{0}'

例如, 如果最大长度验证失败,JSF将获得“javax.faces.validator.LengthValidator.MAXIMUM”。 如果最小长度验证失败,JSF将获得“javax.faces.validator.LengthValidator.MINIMUM”。

个性化消息

为例子创建一个文件(带有任何名称):myMessages.properties并将它们放在源文件夹中的特定包中。

将您在messages.properties jsf-api-api 2.x.jar文件中找到的所有消息放入。 然后根据需要更改消息,例如:

javax.faces.validator.LengthValidator.MAXIMUM=My custom message 1
javax.faces.validator.LengthValidator.MINIMUM=My custom message 2

注册消息包

在“faces-config.xml”中注册自定义属性文件,将其作为应用程序级别。

面-config.xml中

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
 <application>
  <message-bundle>
    package.name.MyMessage
  </message-bundle>
 </application>

<强>演示

<h:form>

    <h:panelGrid columns="3">

        Enter your username :

        <h:inputText id="username" value="#{user.username}" 
            size="20" required="true" label="Username">
            <f:validateLength minimum="5" maximum="10" />
        </h:inputText>

        <h:message for="username" style="color:red" />

        Enter your DOB :

        <h:inputText id="dob" value="#{user.dob}" 
            size="20" required="true" label="Date of Birth">
            <f:convertDateTime />
        </h:inputText>

        <h:message for="dob" style="color:red" />

    </h:panelGrid>

    <h:commandButton value="Submit" action="result" />

</h:form>

来源:click here

相关问题