代码之家  ›  专栏  ›  技术社区  ›  Wolkenarchitekt

jsf为h:selectonemenu自定义验证消息(如果未选择任何内容)

  •  5
  • Wolkenarchitekt  · 技术社区  · 15 年前

    我有一个h:selectonemenu,它充满了枚举值,可以正常工作。唯一的问题是,当没有选择有效值时,我不知道如何覆盖标准的JSF错误消息。错误消息总是 bv:title: 'Choose' must be convertible to an enum from the enum that contains the constant 'Choose'. 虽然我已经指定了RequiredMessage和ValidatorMessage(在inputText上工作),但是只显示标准的JSF消息。

    片段:

    <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中的所有错误)?

    1 回复  |  直到 15 年前
        1
  •  11
  •   BalusC    15 年前

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

    您正在使用字符串值作为第一项。这不能转换为枚举。您需要将其设置为带有 无效的 项目价值。

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