2010-06-02 73 views
4

我有一个h:selectOneMenu这是填充枚举值,它工作正常。唯一的问题是,我不知道如何覆盖标准的JSF错误消息,当没有选择有效的值。错误消息始终为bv:title: 'Choose' must be convertible to an enum from the enum that contains the constant 'Choose'. 虽然我已指定requiredMessage和validatormessage(它对InputText起作用),但只显示标准JSF消息。JSF为h:selectOneMenu(如果没有选择)自定义验证消息

的片段:

<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中再次定义所有错误)?

回答

10

这不是“必需的”错误消息。这是一个“转换器”错误消息。当当前选择的项目与期望的类型或列表中的任何选项不匹配时,可能会出现这种情况。只有在当前选择的项目是null时才会显示所需的信息。

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

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

好,完美的作品 - 谢谢! 我将selectItem替换为您的选择项,并在selectOneMenu中定义converterMessage =“请选择一个值”。 – ifischer 2010-06-02 12:22:22

+0

不客气。 – BalusC 2010-06-02 12:24:05