如何自定义JSF验证错误消息

时间:2022-12-16 20:02:41

How can I customize the validation message that appears when validation fails?

如何自定义验证失败时出现的验证消息?

Here is the code I have:

这是我的代码:

<h:form>
    <p><h:inputText
           id="userNo"
           title="Type a number from 0 to 10:">
       <f:validateLongRange
           minimum="3"
           maximum="6"/>
       </h:inputText>

       <h:commandButton id="submit" value="Submit"
           action="response"/>
    </p>
    <h:message showSummary="true" showDetail="false"
        id="errors1"
        for="userNo"/>
</h:form>

Currently the message looks like this:

目前消息如下所示:

j_idt10:userNo: Validation Error: Specified attribute is not between the expected values of 3 and 6. 

Which is not particularly user-friendly.

哪个不是特别用户友好。

3 个解决方案

#1


38  

The simplest way would be to set the validatorMessage="my custom message" attribute in the <h:inputText> tag.

最简单的方法是在 标记中设置validatorMessage =“我的自定义消息”属性。 :inputtext>

For a more advanced way read this article Customize validation error message in JSF 2.0

有关更高级的方法,请阅读本文自定义JSF 2.0中的验证错误消息

And here a complete Reference to all available message that you can override in JSF 2.0.x

这里是对JSF 2.0.x中可以覆盖的所有可用消息的完整引用

#2


10  

In addition to Daniel's answer you could always use the label attribute for your input components to remove the client-id (j_idt10:userNo:) from the error message.

除了Daniel的答案之外,你总是可以使用输入组件的label属性从错误消息中删除client-id(j_idt10:userNo :)。

E.g. with

例如。同

<h:inputText id="userNo" title="Type a number from 0 to 10:"
             label="User number">
  <f:validateLongRange
           minimum="3"
           maximum="6"/>
</h:inputText>

will produce:

将产生:

User number: Validation Error: Specified attribute is not between the expected values of 3 and 6.

用户编号:验证错误:指定的属性不在预期值3和6之间。

The label attribute can be an el expression as well to change this part of the error message dynamically.

label属性也可以是el表达式,以动态更改错误消息的这一部分。

#3


4  

You can use validatorMessage property of input text. Use requiredMessage property for required message, it is different from validator message.

您可以使用输入文本的validatorMessage属性。对所需消息使用requiredMessage属性,它与验证器消息不同。

<h:input text required ="true" validatorMessage="Enter user friendly message">
    <f:validateLongRange
        minimum="3"
        maximum="6"/>
</h:inputText>

#1


38  

The simplest way would be to set the validatorMessage="my custom message" attribute in the <h:inputText> tag.

最简单的方法是在 标记中设置validatorMessage =“我的自定义消息”属性。 :inputtext>

For a more advanced way read this article Customize validation error message in JSF 2.0

有关更高级的方法,请阅读本文自定义JSF 2.0中的验证错误消息

And here a complete Reference to all available message that you can override in JSF 2.0.x

这里是对JSF 2.0.x中可以覆盖的所有可用消息的完整引用

#2


10  

In addition to Daniel's answer you could always use the label attribute for your input components to remove the client-id (j_idt10:userNo:) from the error message.

除了Daniel的答案之外,你总是可以使用输入组件的label属性从错误消息中删除client-id(j_idt10:userNo :)。

E.g. with

例如。同

<h:inputText id="userNo" title="Type a number from 0 to 10:"
             label="User number">
  <f:validateLongRange
           minimum="3"
           maximum="6"/>
</h:inputText>

will produce:

将产生:

User number: Validation Error: Specified attribute is not between the expected values of 3 and 6.

用户编号:验证错误:指定的属性不在预期值3和6之间。

The label attribute can be an el expression as well to change this part of the error message dynamically.

label属性也可以是el表达式,以动态更改错误消息的这一部分。

#3


4  

You can use validatorMessage property of input text. Use requiredMessage property for required message, it is different from validator message.

您可以使用输入文本的validatorMessage属性。对所需消息使用requiredMessage属性,它与验证器消息不同。

<h:input text required ="true" validatorMessage="Enter user friendly message">
    <f:validateLongRange
        minimum="3"
        maximum="6"/>
</h:inputText>