Specifying different error messages for different

2019-02-19 18:28发布

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条回答
我想做一个坏孩纸
2楼-- · 2019-02-19 19:08

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}.

查看更多
登录 后发表回答