-
Available since OmniFaces 1.8
The <o:massAttribute> sets an attribute of the given name and value on all nested components, if they don't already have an attribute set. On boolean attributes like disabled
, readonly
and rendered
, any literal (static) attribute value will be ignored and overridden. Only if they have already a value expression #{...}
as attribute value, then it won't be overridden. This is a technical limitation specifically for boolean attributes as they don't default to null
.
Usage
For example, the following setup
<o:massAttribute name="disabled" value="true">
<h:inputText id="input1" />
<h:inputText id="input2" disabled="true" />
<h:inputText id="input3" disabled="false" />
<h:inputText id="input4" disabled="#{true}" />
<h:inputText id="input5" disabled="#{false}" />
</o:massAttribute>
will set the disabled="true"
attribute in input1
, input2
and input3
as those are the only components without a value expression on the boolean attribute.
As another general example without booleans, the following setup
<o:massAttribute name="styleClass" value="#{component.valid ? '' : 'error'}">
<h:inputText id="input1" />
<h:inputText id="input2" styleClass="some" />
<h:inputText id="input3" styleClass="#{'some'}" />
<h:inputText id="input4" styleClass="#{null}" />
</o:massAttribute>
will only set the styleClass="#{component.valid ? '' : 'error'}"
attribute in input1
as that's the only component on which the attribute is absent. Do note that the specified EL expression will actually be evaluated on a per-component basis.
To target a specific component (super)class, use the target
attribute. The example below skips labels (as that would otherwise fail in the example below because they don't have the valid
property):
<o:massAttribute name="styleClass" value="#{component.valid ? '' : 'error'}" target="jakarta.faces.component.UIInput">
<h:outputLabel for="input1" />
<h:inputText id="input1" />
<h:outputLabel for="input2" />
<h:inputText id="input2" />
<h:outputLabel for="input3" />
<h:inputText id="input3" />
</o:massAttribute>
Since OmniFaces 3.10, the target
attribute supports a commaseparated string.
Set disabled="true"
on all inputs and submit button after successful submit
Set style="background: #{facesContext.postback ? (component.valid ? 'palegreen' : 'pink') : ''}"
on all inputs
<h3>Set <code>disabled="true"</code> on all inputs and submit button after successful submit</h3>
<h:form>
<h:panelGrid columns="3">
<o:massAttribute name="disabled"
value="#{facesContext.postback and facesContext.renderResponse and not facesContext.validationFailed}">
<o:outputLabel for="foo" value="Foo" />
<h:inputText id="foo" required="true" />
<h:message for="foo" />
<o:outputLabel for="bar" value="Bar" />
<h:inputText id="bar" required="true" />
<h:message for="bar" />
<o:outputLabel for="baz" value="Baz" />
<h:inputText id="baz" required="true" />
<h:message for="baz" />
<h:panelGroup />
<h:commandButton value="submit">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
<h:outputText value="OK!" rendered="#{facesContext.postback and not facesContext.validationFailed}" />
</o:massAttribute>
</h:panelGrid>
</h:form>
<h3>Set <code>style="background: \#{facesContext.postback ? (component.valid ? 'palegreen' : 'pink') : ''}"</code> on all inputs</h3>
<h:form>
<h:panelGrid columns="3">
<o:massAttribute target="jakarta.faces.component.UIInput" name="style"
value="background: #{facesContext.postback ? (component.valid ? 'palegreen' : 'pink') : ''}">
<o:outputLabel for="foo" value="Foo" />
<h:inputText id="foo" required="true" />
<h:message for="foo" />
<o:outputLabel for="bar" value="Bar" />
<h:inputText id="bar" required="true" />
<h:message for="bar" />
<o:outputLabel for="baz" value="Baz" />
<h:inputText id="baz" required="true" />
<h:message for="baz" />
<h:panelGroup />
<h:commandButton value="submit">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
<h:outputText value="OK!" rendered="#{facesContext.postback and not facesContext.validationFailed}" />
</o:massAttribute>
</h:panelGrid>
</h:form>