我有一个填充了枚举值的 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 中定义所有错误)?
最佳答案
这不是“必需的”错误消息。这是“转换器”错误消息。当当前选定的项目与预期的类型或列表中的任何选项都不匹配时,可能会出现这种情况。仅当当前选择的项目为 null
时才会出现所需的消息.
您正在使用字符串值作为第一项。这不能转换为枚举。您需要将其设置为带有 null
的项目标签元素值(value)。
<f:selectItem itemLabel="Choose" itemValue="null" />
关于JSF 为 h :selectOneMenu (if nothing selected) 自定义验证消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2957182/