How to customize JSF validation error message

后端 未结 3 1526
小蘑菇
小蘑菇 2020-12-01 21:02

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

Here is the code I have:


    

相关标签:
3条回答
  • 2020-12-01 21:12

    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.

    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.

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

    0 讨论(0)
  • 2020-12-01 21:15

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

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

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

    0 讨论(0)
  • 2020-12-01 21:22

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

    <h:input text required ="true" validatorMessage="Enter user friendly message">
        <f:validateLongRange
            minimum="3"
            maximum="6"/>
    </h:inputText>
    
    0 讨论(0)
提交回复
热议问题