- cdi
- components
- contexts
- converters
- el
- eventlisteners
- exceptionhandlers
- facesviews
- filters
- functions
- managedbeans
- push
- resourcehandlers
- search
- servlets
- taghandlers
- utils
- validators
- viewhandlers
- - JsfLabelMessageInterpolator
- RequiredCheckboxValidator
- ValueChangeValidator
- validateAll
- validateAllOrNone
- validateBean
- validateEqual
- validateMultiple
- validateOne
- validateOneOrMore
- validateOneOrNone
- validateOrder
- validateUnique
- validateUniqueColumn
- cdi
- components
- contexts
- converters
- el
- eventlisteners
- exceptionhandlers
- facesviews
- filters
- functions
- managedbeans
- push
- resourcehandlers
- search
- servlets
- taghandlers
- utils
- validators
- viewhandlers
- JsfLabelMessageInterpolator
- RequiredCheckboxValidator
- ValueChangeValidator
- validateAll
- validateAllOrNone
- validateBean
- validateEqual
- validateMultiple
- validateOne
- validateOneOrMore
- validateOneOrNone
- validateOrder
- validateUnique
- validateUniqueColumn
Unlike native JSF validation error messages, in a bean validation message by default the label of the component where a validation constraint violation originated from can not be displayed in the middle of a message. Using the javax.faces.validator.BeanValidator.MESSAGE
bundle key such label can be put in front or behind the message, but that's it. With this JsfLabelMessageInterpolator
a label can appear in the middle of a message, by using the special placeholder {jsf.label}
in bean validation messages.
Note that Bean Validation is not only called from within JSF, and as such JSF might not be available. If JSF is not available occurrences of {jsf.label}
will be replaced by an empty string. The user should take care that messages are compatible with both situations if needed.
This message interpolator is not needed for putting a component label before or after a bean validation message. That functionality is already provided by JSF itself via the javax.faces.validator.BeanValidator.MESSAGE
key in any resource bundle known to JSF.
Installation
Create a /META-INF/validation.xml
file in WAR with the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<validation-config
xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd"
>
<message-interpolator>org.omnifaces.validator.JsfLabelMessageInterpolator</message-interpolator>
</validation-config>
Usage
As an example, the customization of @Size
in ValidationMessages.properties
:
javax.validation.constraints.Size.message = The size of {jsf.label} must be between {min} and {max} characters
Enter more than 2 resp. 3 characters in the input fields below and press submit to trigger
a validation error. Note how the field's label appears in the message, which originates from
ValidationMessages.properties
.
<p>
Enter more than 2 resp. 3 characters in the input fields below and press submit to trigger
a validation error. Note how the field's label appears in the message, which originates from
<code>ValidationMessages.properties</code>.
</p>
<h:form>
<h:messages />
<h:panelGrid columns="2">
<o:outputLabel for="foo" value="Foo" />
<h:inputText id="foo" value="#{validateConstraintsBean.constrainedEntity.foo}" />
<o:outputLabel for="bar" value="Bar" />
<h:inputText id="bar" value="#{validateConstraintsBean.constrainedEntity.bar}" />
<h:commandButton value="submit">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
</h:panelGrid>
</h:form>
package org.omnifaces.showcase.validators;
import javax.inject.Named;
import org.omnifaces.showcase.model.ConstrainedEntity;
@Named
public class ValidateConstraintsBean {
private ConstrainedEntity constrainedEntity = new ConstrainedEntity();
public ConstrainedEntity getConstrainedEntity() {
return constrainedEntity;
}
}
package org.omnifaces.showcase.model;
import javax.validation.constraints.Size;
public class ConstrainedEntity {
@Size(min = 0, max = 2)
public String foo;
@Size(min = 0, max = 3)
public String bar;
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
}
javax.validation.constraints.Size.message = The size of {jsf.label} must be between {min} and {max} characters.