Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF Customize validation message for h:selectOneMenu (if nothing selected)

Tags:

jsf

jsf-2

I have a h:selectOneMenu thats filled with enum values, which works fine. The only problem is that i don't know how to overwrite the standard JSF error message, when no valid value is selected. The error message is always bv:title: 'Choose' must be convertible to an enum from the enum that contains the constant 'Choose'. Although i have specified requiredmessage and validatormessage (which works on InputText), only the standard JSF message is shown.

The snippet:

<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" />

How can i overwrite the standard validator message? Or better - can i create a copy of the JSF messages.properties with customized error messages (don't want to define all errors in my own messages.properties again)?

like image 606
Wolkenarchitekt Avatar asked Jun 02 '10 11:06

Wolkenarchitekt


1 Answers

This is not a "required" error message. This is a "converter" error message. This may appear when the currently selected item does not match the expected type nor any of the options in the list. The required message will only appear when the currently selected item is null.

You're using a string value as first item. This is not convertible to an enum. You need to set it as item label with a null item value.

<f:selectItem itemLabel="Choose" itemValue="null" />
like image 74
BalusC Avatar answered Sep 28 '22 09:09

BalusC