Class Highlight
- java.lang.Object
-
- jakarta.faces.component.UIComponent
-
- jakarta.faces.component.UIComponentBase
-
- org.omnifaces.component.script.ScriptFamily
-
- org.omnifaces.component.script.OnloadScript
-
- org.omnifaces.component.script.Highlight
-
- All Implemented Interfaces:
PartialStateHolder
,StateHolder
,TransientStateHolder
,ComponentSystemEventListener
,FacesListener
,SystemEventListener
,SystemEventListenerHolder
,EventListener
public class Highlight extends OnloadScript
The
<o:highlight>
is a helper component which highlights all invalidUIInput
components and the associated labels by adding an error style class to them. Additionally, it by default focuses the first invalidUIInput
component. The<o:highlight />
component can be placed anywhere in the view, as long as there's only one of it. Preferably put it somewhere in the master template for forms.<h:form> <h:inputText value="#{bean.input1}" required="true" /> <h:inputText value="#{bean.input2}" required="true" /> <h:commandButton value="Submit" action="#{bean.submit}" /> </h:form> <o:highlight />
The default error style class name is
error
. You need to specify a CSS style associated with the class yourself. For example,label.error { color: #f00; } input.error, select.error, textarea.error { background-color: #fee; }
You can override the default error style class by the
styleClass
attribute:<o:highlight styleClass="invalid" />
You can disable the default focus on the first invalid input element setting the
focus
attribute.<o:highlight styleClass="invalid" focus="false" />
Since version 2.5, the error style class will be removed from the input element and its associated label when the enduser starts using the input element.
- Author:
- Bauke Scholtz
- See Also:
OnloadScript
,ScriptFamily
-
-
Field Summary
Fields Modifier and Type Field Description static String
COMPONENT_TYPE
The component type, which is "org.omnifaces.component.script.Highlight".-
Fields inherited from class org.omnifaces.component.script.ScriptFamily
COMPONENT_FAMILY
-
Fields inherited from class jakarta.faces.component.UIComponent
ATTRS_WITH_DECLARED_DEFAULT_VALUES, BEANINFO_KEY, bindings, COMPOSITE_COMPONENT_TYPE_KEY, COMPOSITE_FACET_NAME, CURRENT_COMPONENT, CURRENT_COMPOSITE_COMPONENT, FACETS_KEY, HONOR_CURRENT_COMPONENT_ATTRIBUTES_PARAM_NAME, VIEW_LOCATION_KEY
-
-
Constructor Summary
Constructors Constructor Description Highlight()
The constructor instructs Faces to register all scripts during the render response phase if necessary.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
encodeChildren(FacesContext context)
Visit all components of the currentUIForm
, check if they are an instance ofUIInput
and are notUIInput.isValid()
and finally append them to an array in JSON format and render the script.String
getStyleClass()
Returns the error style class which is to be applied on invalid inputs.boolean
isFocus()
Returns whether the first error element should gain focus.boolean
isRendered()
This component is per definiton only rendered when the current request is a postback request and the validation has failed.void
setFocus(boolean focus)
Sets whether the first error element should gain focus.void
setStyleClass(String styleClass)
Sets the error style class which is to be applied on invalid inputs.-
Methods inherited from class org.omnifaces.component.script.OnloadScript
encodeBegin, encodeEnd, isListenerForSource, processEvent, processEvent
-
Methods inherited from class org.omnifaces.component.script.ScriptFamily
getFamily, getRendersChildren, moveToBody
-
Methods inherited from class jakarta.faces.component.UIComponentBase
addClientBehavior, addFacesListener, broadcast, clearInitialState, decode, findComponent, getAttributes, getChildCount, getChildren, getClientBehaviors, getClientId, getDefaultEventName, getEventNames, getFacesContext, getFacesListeners, getFacet, getFacetCount, getFacets, getFacetsAndChildren, getId, getListenersForEventClass, getParent, getPassThroughAttributes, getRenderer, getRendererType, getValueBinding, invokeOnComponent, isTransient, markInitialState, processDecodes, processRestoreState, processSaveState, processUpdates, processValidators, queueEvent, removeFacesListener, restoreAttachedState, restoreState, saveAttachedState, saveState, setId, setParent, setRendered, setRendererType, setTransient, setValueBinding, subscribeToEvent, unsubscribeFromEvent
-
Methods inherited from class jakarta.faces.component.UIComponent
encodeAll, getClientId, getCompositeComponentParent, getContainerClientId, getCurrentComponent, getCurrentCompositeComponent, getNamingContainer, getPassThroughAttributes, getResourceBundleMap, getStateHelper, getStateHelper, getTransientStateHelper, getTransientStateHelper, getValueExpression, initialStateMarked, isCompositeComponent, isInView, isVisitable, popComponentFromEL, pushComponentToEL, restoreTransientState, saveTransientState, setInView, setValueExpression, visitTree
-
-
-
-
Field Detail
-
COMPONENT_TYPE
public static final String COMPONENT_TYPE
The component type, which is "org.omnifaces.component.script.Highlight".- See Also:
- Constant Field Values
-
-
Method Detail
-
encodeChildren
public void encodeChildren(FacesContext context) throws IOException
Visit all components of the currentUIForm
, check if they are an instance ofUIInput
and are notUIInput.isValid()
and finally append them to an array in JSON format and render the script.Note that the
FacesContext.getClientIdsWithMessages()
could also be consulted, but it does not indicate whether the components associated with those client IDs are actuallyUIInput
components which are notUIInput.isValid()
. Also note that the highlighting is been done by delegating the job to JavaScript instead of directly changing the component's ownstyleClass
attribute; this is chosen so because we don't want the changed style class to be saved in the server side view state as it may result in potential inconsistencies because it's supposed to be an one-time change.- Overrides:
encodeChildren
in classUIComponentBase
- Throws:
IOException
-
isRendered
public boolean isRendered()
This component is per definiton only rendered when the current request is a postback request and the validation has failed.- Overrides:
isRendered
in classUIComponentBase
-
getStyleClass
public String getStyleClass()
Returns the error style class which is to be applied on invalid inputs. Defaults toerror
.- Returns:
- The error style class which is to be applied on invalid inputs.
-
setStyleClass
public void setStyleClass(String styleClass)
Sets the error style class which is to be applied on invalid inputs.- Parameters:
styleClass
- The error style class which is to be applied on invalid inputs.
-
isFocus
public boolean isFocus()
Returns whether the first error element should gain focus. Defaults totrue
.- Returns:
- Whether the first error element should gain focus.
-
setFocus
public void setFocus(boolean focus)
Sets whether the first error element should gain focus.- Parameters:
focus
- Whether the first error element should gain focus.
-
-