Class SetAttributeAction<T>
- Type Parameters:
T- the runtime type of the value to assign — the DOM coerces it to a string when setting the attribute
- All Implemented Interfaces:
Serializable
Attributes differ from JS properties (see SetPropertyAction):
attributes live in the markup-facing namespace (id, class,
data-*, ARIA attributes such as aria-hidden) and their values
are always strings in the DOM. A null value at fire time removes the
attribute via removeAttribute, matching the way the DOM models
"attribute is absent".
The value to assign can be either a literal (constant, serialised at build
time) or an Action.Input that produces the value on the client when
the trigger fires.
Common idioms:
- Set an ARIA flag:
new SetAttributeAction<>(panel, "aria-hidden", "true") - Remove an attribute:
new SetAttributeAction<>(input, "disabled", (String) null) - Mirror a value from another component:
new SetAttributeAction<>(target, "data-source", new PropertyInput<>(field, "value", String.class))
Server-state mirror (opt-in)
By default, this action runs client-only: the attribute is assigned (or removed) in the browser and the server's view of the attribute is not updated. As a result,Element.getAttribute(String) on
the target keeps returning whatever the server last set, which will drift
from what the user sees in the browser. Call mirrorToServer() to opt
into mirroring the assigned value back into the target's
ElementAttributeMap so the server-side view tracks the browser. The
server-side update produced by the mirror does not generate another
client-bound change, so there is no echo round-trip.
Security implication of mirrorToServer(): when the
mirror is enabled, the value the server stores is whatever the client sent.
An end user with the browser devtools open can substitute any value for the
one this action would normally compute. Server-side code must not rely on
the mirrored attribute value as a trusted source of truth for
authorisation, validation, or any other security-sensitive decision; treat it
the same way you would treat any other client-originated input. If you need a
trusted server-side value, derive it on the server (e.g. via a
CallbackAction that recomputes from session state) rather than
trusting the mirror. This caveat is the reason the mirror is opt-in: the safe
default never trusts a client-set value.
For internal use only. May be renamed or removed in a future release.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.vaadin.flow.component.trigger.internal.Action
Action.Input<T> -
Constructor Summary
ConstructorsConstructorDescriptionSetAttributeAction(Component target, String attributeName, @Nullable T value) Creates an action that assigns the given literal value to the given attribute ontargetwhen the trigger fires.SetAttributeAction(Component target, String attributeName, Action.Input<? extends T> source) Creates an action that assigns the value produced bysourceto the given attribute ontargetwhen the trigger fires. -
Method Summary
Modifier and TypeMethodDescriptionOpts into the server-state mirror: when the trigger fires, the assigned value (or the removal) is forwarded back to the server and stored on the target'sElementAttributeMapsoElement.getAttribute(String)returns the same value the browser sees.protected JsFunctionBuilds theJsFunctionthat runs this action when the surrounding trigger fires.Methods inherited from class com.vaadin.flow.component.trigger.internal.Action
applyTemporarily, warnIfNotVisible
-
Constructor Details
-
SetAttributeAction
Creates an action that assigns the given literal value to the given attribute ontargetwhen the trigger fires. Passingnullremoves the attribute (renderstarget.removeAttribute(name);).- Parameters:
target- the component whose root element to modify, notnullattributeName- the attribute name, notnullvalue- the value to assign — typically aString; any value is coerced to a string by the DOM.nullremoves the attribute.
-
SetAttributeAction
Creates an action that assigns the value produced bysourceto the given attribute ontargetwhen the trigger fires. Anullproduced by the source removes the attribute.- Parameters:
target- the component whose root element to modify, notnullattributeName- the attribute name, notnullsource- input that produces the value to assign, notnull
-
-
Method Details
-
mirrorToServer
Opts into the server-state mirror: when the trigger fires, the assigned value (or the removal) is forwarded back to the server and stored on the target'sElementAttributeMapsoElement.getAttribute(String)returns the same value the browser sees.See the class Javadoc for the security implication — the server stores whatever the client sent, so the mirrored value is not trustworthy for authorisation or validation decisions.
Must be called before this action is wired through
Trigger.triggers(Action...); configuration changes after wiring do not affect the already-installed client JS.- Returns:
- this action, for chaining
-
toJs
Description copied from class:ActionBuilds theJsFunctionthat runs this action when the surrounding trigger fires. The returned function takes one runtime argument namedevent(declared by the framework when it composes the trigger handler); subclasses do not declare argument names themselves.The body is one statement. To embed a value produced on the client, capture an
Action.Input'sJsFunctionas a capture and invoke it inside the body as$N(event).
-