【问题标题】:JSF Customize validation message for h:selectOneMenu (if nothing selected)JSF 为 h:selectOneMenu 自定义验证消息(如果没有选择)
【发布时间】:2010-06-02 11:27:41
【问题描述】:

我有一个充满枚举值的 h:selectOneMenu,效果很好。唯一的问题是,当没有选择有效值时,我不知道如何覆盖标准 JSF 错误消息。错误信息总是bv:title: 'Choose' must be convertible to an enum from the enum that contains the constant 'Choose'. 虽然我指定了 requiredmessage 和 validatormessage(适用于 InputText),但只显示标准 JSF 消息。

sn-p:

<h:selectOneMenu id="title" value="#{personBean.person.title}" required="true"
                  requiredMessage="ERROR"
                  validatorMessage="ERROR">
  <f:selectItem itemValue="Choose" />
  <f:selectItems value="#{personBean.titleOptions}" />
  <f:ajax event="blur" render="titleError" />
  <f:validateRequired/> 
</h:selectOneMenu> 
<h:message for="title" errorClass="invalid" id="titleError" />

如何覆盖标准验证器消息? 或者更好 - 我可以使用自定义错误消息创建 JSF messages.properties 的副本(不想再次在我自己的 messages.properties 中定义所有错误)?

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:

    这不是“必需的”错误消息。这是“转换器”错误消息。当当前选定的项目与预期类型或列表中的任何选项不匹配时,可能会出现此问题。仅当当前选择的项目为null 时才会出现所需消息。

    您将字符串值用作第一项。这不能转换为枚举。您需要将其设置为带有 null 项目值的项目标签。

    <f:selectItem itemLabel="Choose" itemValue="null" />
    

    【讨论】:

    • 太好了,完美运行-谢谢!!!我用你的替换了 selectItem 并在 selectOneMenu 中定义了 converterMessage="请选择一个值"'。
    猜你喜欢
    • 1970-01-01
    • 2013-06-05
    • 2012-05-11
    • 2013-04-08
    • 2013-06-13
    • 1970-01-01
    • 2011-09-09
    • 2012-12-25
    • 1970-01-01
    相关资源
    最近更新 更多