org.omnifaces.component.validator
Class ValidateMultipleFields

java.lang.Object
  extended by javax.faces.component.UIComponent
      extended by javax.faces.component.UIComponentBase
          extended by org.omnifaces.component.validator.ValidatorFamily
              extended by org.omnifaces.component.validator.ValidateMultipleFields
All Implemented Interfaces:
java.util.EventListener, javax.faces.component.PartialStateHolder, javax.faces.component.StateHolder, javax.faces.event.ComponentSystemEventListener, javax.faces.event.FacesListener, javax.faces.event.SystemEventListenerHolder
Direct Known Subclasses:
ValidateAll, ValidateAllOrNone, ValidateEqual, ValidateOne, ValidateOneOrMore, ValidateOneOrNone, ValidateOrder, ValidateUnique

public abstract class ValidateMultipleFields
extends ValidatorFamily

Base class which is to be shared between all multi field validators. The implementors have to call the super constructor with the default message. The implementors have to override the validateValues(FacesContext, List, List) method to perform the actual validation.


General usage of all multiple field validators

This validator must be placed inside the same UIForm as the UIInput components in question. The UIInput components must be referenced by a space separated collection of their client IDs in the components attribute. This validator can be placed anywhere in the form, but keep in mind that the components will be validated in the order as they appear in the form. So if this validator is been placed before all of the components, then it will be executed before any of the component's own validators. If this validator fails, then the component's own validators will not be fired. If this validator is been placed after all of the components, then it will be executed after any of the component's own validators. If any of them fails, then this validator will not be exeucted. It is not recommended to put this validator somewhere in between the referenced components as the resulting behaviour may be confusing. Put this validator either before or after all of the components, depending on how you would like to prioritize the validation.

 <o:validateMultipleFields id="myId" components="foo bar baz" />
 <h:message for="myId" />
 <h:inputText id="foo" />
 <h:inputText id="bar" />
 <h:inputText id="baz" />
 

In an invalidating case, all of the referenced components will be marked invalid and a faces message will be added on the client ID of this validator component. The default message can be changed by the message attribute. Any "{0}" placeholder in the message will be substituted with a comma separated string of labels of the referenced input components.

 <o:validateMultipleFields components="foo bar baz" message="{0} are wrong!" />
 

The faces message can also be shown for all of the referenced components using showMessageFor="@all".

 <o:validateMultipleFields components="foo bar baz" message="This is wrong!" showMessageFor="@all" />
 <h:inputText id="foo" />
 <h:message for="foo" />
 <h:inputText id="bar" />
 <h:message for="bar" />
 <h:inputText id="baz" />
 <h:message for="baz" />
 

The showMessageFor attribute defaults to @this. Other values than @this or @all are not allowed.

The validator can be disabled by the disabled attribute. It accepts a request based EL expression.

 <o:validateMultipleFields components="foo bar baz" disabled="#{param.validationDisabled}" />
 

There is a read-only validationFailed attribute which can be used to determine if the validation by this component has failed.

 <o:validateMultipleFields id="myId" binding="#{myId}" components="foo bar baz" />
 <h:panelGroup rendered="#{myId.validationFailed}">
     Validation has failed! <h:message for="myId" />
 </h:panelGroup>
 

TODO: support for immediate="true".

Author:
Bauke Scholtz

Field Summary
 
Fields inherited from class org.omnifaces.component.validator.ValidatorFamily
COMPONENT_FAMILY
 
Fields inherited from class javax.faces.component.UIComponent
BEANINFO_KEY, bindings, COMPOSITE_COMPONENT_TYPE_KEY, COMPOSITE_FACET_NAME, CURRENT_COMPONENT, CURRENT_COMPOSITE_COMPONENT, FACETS_KEY, VIEW_LOCATION_KEY
 
Constructor Summary
ValidateMultipleFields(java.lang.String defaultMessage)
          The default constructor sets the default message and sets the renderer type to null.
 
Method Summary
protected  java.util.List<javax.faces.component.UIInput> collectComponents()
          Collect the input components.
protected  java.util.List<java.lang.Object> collectValues(java.util.List<javax.faces.component.UIInput> inputs)
          Collect the values of the given input components.
 java.lang.String getComponents()
          Returns the client identifiers of components which are to be validated.
 java.lang.String getMessage()
          Returns the validation message to be shown.
 java.lang.String getShowMessageFor()
          Returns the client identifier to show the validation message for.
 java.lang.Boolean isDisabled()
          Returns whether the validation should be disabled or not.
 boolean isValidationFailed()
          Returns whether the validation has failed or not.
 void setComponents(java.lang.String components)
          Sets the client identifiers of components which are to be validated.
 void setDisabled(java.lang.Boolean disabled)
          Sets whether the validation should be disabled or not.
 void setMessage(java.lang.String message)
          Sets the validation message to be shown.
 void setShowMessageFor(java.lang.String showMessageFor)
          Sets the client identifier to show the validation message for.
protected  void showMessage(javax.faces.context.FacesContext context, java.util.List<javax.faces.component.UIInput> inputs)
          Show the message at the desired place(s) depending on the value of the showMessageFor attribute.
protected  void validateComponents(javax.faces.context.FacesContext context)
          If the validation is not disabled, collect the components, if it is not empty, then collect their values and delegate to validateValues(FacesContext, List, List).
protected  void validateHierarchy()
          Validate our component hierarchy.
protected abstract  boolean validateValues(javax.faces.context.FacesContext context, java.util.List<javax.faces.component.UIInput> components, java.util.List<java.lang.Object> values)
          Perform the validation on the collected values of the input components and returns whether the validation is successful.
 
Methods inherited from class org.omnifaces.component.validator.ValidatorFamily
encodeChildren, getFamily, getRendersChildren, processDecodes, processUpdates, processValidators
 
Methods inherited from class javax.faces.component.UIComponentBase
addClientBehavior, addFacesListener, broadcast, clearInitialState, decode, encodeBegin, encodeEnd, findComponent, getAttributes, getChildCount, getChildren, getClientBehaviors, getClientId, getDefaultEventName, getEventNames, getFacesContext, getFacesListeners, getFacet, getFacetCount, getFacets, getFacetsAndChildren, getId, getParent, getRenderer, getRendererType, getValueBinding, invokeOnComponent, isRendered, isTransient, markInitialState, processRestoreState, processSaveState, queueEvent, removeFacesListener, restoreAttachedState, restoreState, saveAttachedState, saveState, setId, setParent, setRendered, setRendererType, setTransient, setValueBinding
 
Methods inherited from class javax.faces.component.UIComponent
encodeAll, getClientId, getCompositeComponentParent, getContainerClientId, getCurrentComponent, getCurrentCompositeComponent, getListenersForEventClass, getNamingContainer, getResourceBundleMap, getStateHelper, getStateHelper, getValueExpression, initialStateMarked, isCompositeComponent, isInView, isVisitable, popComponentFromEL, processEvent, pushComponentToEL, setInView, setValueExpression, subscribeToEvent, unsubscribeFromEvent, visitTree
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ValidateMultipleFields

public ValidateMultipleFields(java.lang.String defaultMessage)
The default constructor sets the default message and sets the renderer type to null.

Method Detail

validateHierarchy

protected void validateHierarchy()
                          throws java.lang.IllegalArgumentException
Validate our component hierarchy.

Specified by:
validateHierarchy in class ValidatorFamily
Throws:
java.lang.IllegalArgumentException - When there is no parent of type UIForm, or when there are any children.

validateComponents

protected void validateComponents(javax.faces.context.FacesContext context)
If the validation is not disabled, collect the components, if it is not empty, then collect their values and delegate to validateValues(FacesContext, List, List). If it returns false, then mark all inputs and the faces context invalid and finally delegate to showMessage(FacesContext, List) to show the message.

Specified by:
validateComponents in class ValidatorFamily
Parameters:
context - The faces context to work with.

collectComponents

protected java.util.List<javax.faces.component.UIInput> collectComponents()
Collect the input components. Only those which are an instance of UIInput, are rendered, not disabled nor readonly will be returned. If at least one of them has already been validated and is been marked invalid, then an empty collection will be returned.

Returns:
The input components.
Throws:
java.lang.IllegalArgumentException - When the components attribute is missing, or when it references an non-existing component, or when it references a non-input component.

collectValues

protected java.util.List<java.lang.Object> collectValues(java.util.List<javax.faces.component.UIInput> inputs)
Collect the values of the given input components.

Parameters:
inputs - The input components to collect values from.
Returns:
The values of the given input components.

validateValues

protected abstract boolean validateValues(javax.faces.context.FacesContext context,
                                          java.util.List<javax.faces.component.UIInput> components,
                                          java.util.List<java.lang.Object> values)
Perform the validation on the collected values of the input components and returns whether the validation is successful.

Parameters:
context - The faces context to work with.
components - The input components whose values are to be validated.
values - The values of the input components to be validated.
Returns:
true if validation is successful, otherwise false (and thus show the message).

showMessage

protected void showMessage(javax.faces.context.FacesContext context,
                           java.util.List<javax.faces.component.UIInput> inputs)
Show the message at the desired place(s) depending on the value of the showMessageFor attribute.

Parameters:
context - The faces context to work with.
inputs - The validated input components.

getComponents

public java.lang.String getComponents()
Returns the client identifiers of components which are to be validated.

Returns:
The client identifiers of components which are to be validated.

setComponents

public void setComponents(java.lang.String components)
Sets the client identifiers of components which are to be validated.

Parameters:
components - The client identifiers of components which are to be validated.

getMessage

public java.lang.String getMessage()
Returns the validation message to be shown.

Returns:
The validation message to be shown.

setMessage

public void setMessage(java.lang.String message)
Sets the validation message to be shown.

Parameters:
message - The validation message to be shown.

getShowMessageFor

public java.lang.String getShowMessageFor()
Returns the client identifier to show the validation message for.

Returns:
The client identifier to show the validation message for.

setShowMessageFor

public void setShowMessageFor(java.lang.String showMessageFor)
Sets the client identifier to show the validation message for.

Parameters:
showMessageFor - The client identifier to show the validation message for.

isDisabled

public java.lang.Boolean isDisabled()
Returns whether the validation should be disabled or not.

Returns:
Whether the validation should be disabled or not.

setDisabled

public void setDisabled(java.lang.Boolean disabled)
Sets whether the validation should be disabled or not.

Parameters:
disabled - Whether the validation should be disabled or not.

isValidationFailed

public boolean isValidationFailed()
Returns whether the validation has failed or not.

Returns:
Whether the validation has failed or not.
Since:
1.3