How can I show the field label in a JSF error message when bean validation is used?
<h:messages/>
<h:inputText label="Username" value="#{myBean.username}" />
...
public class MyBean {
@NotNull
private String username;
...
}
If no user name is submitted only "Must not be null" is shown and there is no field reference.
To display the concerned component's label in the error message define a new value for the property javax.faces.validator.BeanValidator.MESSAGE
in a <message-bundle>
of the application like this:
javax.faces.validator.BeanValidator.MESSAGE={1}: {0}
The placeholder {0}
refers to the error message as created by the Bean Validation runtime, {1}
refers to the component label. More information can be found in the JSF 2.0 spec section 3.5.6.3.
When using Faces 4.0 or newer, then of course use jakarta.*
instead of javax.*
, see also Faces 4.0 spec section 3.5.6.4:
jakarta.faces.validator.BeanValidator.MESSAGE={1}: {0}