3
votes

BalusC provided a good answer to a question about how to set messages per composite component. Specifying different error messages for different composite components The answer involved using per component resource bundles.

Ive applied this approach but when I use {0} in the message to specify the current component label as part of the message, the {0} is rendered as is in my error, rather than being resolved. Is this resolution of labels available when I use this approach? Or do I have to manually build up the error message in the composite component? e.g.

requiredMessage="#{cc.resourceBundleMap.requiredMessage} #{cc.attrs.prefix} #{cc.attrs.label}"

I guess the deeper question is how can I have parameterised message strings in my resource bundle? Will I need to build some kind of custom message resolver?

1

1 Answers

2
votes

Unfortunately, the requiredMessage (and validatorMessage and converterMessage) attribues are not parameterizable by default. I think it would make little sense anyway as they are tied to a specific component in particular. A composite component does usually not contain a multiple of the same components. You would need to specify separate messages for every individual component.

It's in theory however possible to create a custom EL function which does the message formatting job. The method would look something like this:

public static String format(String message, Object argument1, Object argument2) {
    return MessageFormat.format(message, argument1, argument2);
}

which is to be used something like this:

requiredMessage="#{util:format(cc.resourceBundleMap.requiredMessage, cc.attrs.prefix, cc.attrs.label)}"

The only disadvantage is that you can't create EL functions which can take varargs. This is a limitation of the EL specification. So if you intend to be able to pass variable arguments, you'd need to create a separate EL function and a Java method for every possible amount of arguments needed.

As a completely different alternative, you could create a custom Validator and attach it to the particular input component. You can even put the validator method straight in the "backing component" which is associated with the composite component by <cc:interface componentType>. If you remove the required attribute, then this validator will immediately be invoked where you've the freedom to compose messages the way you want.

E.g.

<h:inputText id="foo" validator="#{cc.validateFoo}" requiredMessage="#{cc.resourceBundleMap.requiredMessage}" />

with

public void validateFoo(FacesContext context, UIComponent component, Object value) {
    if (value == null || value.toString().trim().isEmpty()) {
        String requiredMessage = (String) component.getAttributes().get("requiredMessage");
        String prefix = (String) getAttributes().get("prefix");
        String label = (String) getAttributes().get("label");
        throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
            MessageFormat.format(requiredMessage, prefix, label), null));
    }
}