1

In jsf according to what I read I can replace parameters in a resource bundle string using

<h:outputFormat value="#{msg['message.param2']}"> 
   <f:param value="param0" /> 
   <f:param value="param1" /> 
</h:outputFormat> 

My problem is I am working with a primefaces tag and I need to use the attribute requiredMessage of inputText, similar to this:

<p:inputText value="#{cteYDetalleMb.cteEnCaptura.nombreComercial}" style="width: 50em" required="true" 
             requiredMessage="#{msg['validacion.datosRequeridos']}" />

My message to validacion.datosRequeridos is requiring a parameter and the example working above is different. How can i resolve this, i try to use <f:param> but it does not work.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
user1750701
  • 31
  • 3
  • 5
  • I think you can find the solution here: http://stackoverflow.com/questions/9280915/how-to-parameterize-requiredmessage-attribute-in-composite-component – Mojtaba Sep 04 '14 at 08:43

2 Answers2

1

Your best bet is creating a custom EL function so that you can end up like follows:

<p:inputText ... requiredMessage="#{my:format(msg['validacion.datosRequeridos'], 'param0', 'param1')}" />

You could also create a custom UI component which extends <h:outputFormat> and captures the output and stores it in a variable in the EL scope:

<my:outputFormat value="#{msg['message.param2']}" var="requiredMessage"> 
   <f:param value="param0" /> 
   <f:param value="param1" /> 
</my:outputFormat> 
<p:inputText ... requiredMessage="#{requiredMessage}" />

Both approaches are available in JSF utility library OmniFaces in flavor of of:format2() and <o:outputFormat> respectively.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Not the best solution but You can override all default required message with this line: javax.faces.component.UIInput.REQUIRED = new required message. label:{0} The parameter is the label attribute of the inputText.

Or you can create a backingbean method. It reads the message from the bundle.

   <p:inputText value="#{cteYDetalleMb.cteEnCaptura.nombreComercial}" style="width: 50em" required="true" requiredMessage="#{controller.getRequiredMessage(param)}"/>

You can use these helper methods to reading messages from the controllers.

public static String getMessage(String key) {
        FacesContext fc = FacesContext.getCurrentInstance();
        String mb = fc.getApplication().getMessageBundle();
        ResourceBundle resourceBundle = ResourceBundle.getBundle(mb, fc.getViewRoot().getLocale());
        return resourceBundle.getString(key);
    }

    public static String getMessage(String key, String params[]) {
        MessageFormat messageFormat = new MessageFormat(getMessage(key), FacesContext.getCurrentInstance().getViewRoot().getLocale());
        return messageFormat.format(params);
    }
alex.parej
  • 312
  • 2
  • 9