Specifying different error messages for different

2019-02-19 18:49发布

问题:

Is there a way to have REQUIRED messages for different kinds of composite components?

I'd like to be able to say something like the following in a message bundle

javax.faces.component.UIInput.REQUIRED=Please enter a value for {0}
com.anon.sandbox.jsf.component.NameField.REQUIRED=ENTER A {} OR DIE
com.anon.sandbox.jsf.component.EmailField.REQUIRED=You forgot to enter a {0} address

I could do this by implementing custom validators for each kind of input field, but given that the only behaviour I want to customise is the error message this seems like overkill.

回答1:

Use the requiredMessage attribute to specify them.

<h:inputText ... requiredMessage="#{cc.resourceBundleMap.requiredMessage}" />

For the case you didn't knew that, the #{cc.resourceBundleMap} refers the composite component specific resource bundle which is identified by a .properties file with the same filename as the composite component in the same folder.

So, imagine that you've a composite component file foo.xhtml, then you can specify the localized messages in foo.properties (and consequently foo_en.properties, foo_es.properties, etc) which reside in the very same folder. Those localized messages are then available by the aforementioned #{cc.resourceBundleMap}.