Class Switch

All Implemented Interfaces:
AttachNotifier, BlurNotifier<Switch>, ClickNotifier<Switch>, DetachNotifier, Focusable<Switch>, FocusNotifier<Switch>, HasAriaLabel, HasElement, HasEnabled, HasHelper, HasLabel, HasSize, HasStyle, HasTheme, HasValidation, HasValue<AbstractField.ComponentValueChangeEvent<Switch,Boolean>,Boolean>, HasValueAndElement<AbstractField.ComponentValueChangeEvent<Switch,Boolean>,Boolean>, HasThemeVariant<SwitchVariant>, HasTooltip, HasValidationProperties, InputField<AbstractField.ComponentValueChangeEvent<Switch,Boolean>,Boolean>, HasValidator<Boolean>, Serializable

Switch is an input field representing a binary on/off choice.

This component is experimental and needs to be enabled with the com.vaadin.experimental.switchComponent feature flag.

Switch is functionally equivalent to Checkbox, but it is presented as an on/off toggle and is intended for a single setting that takes effect immediately. Unlike Checkbox, Switch has no indeterminate state.

Validation

Switch comes with a built-in validation mechanism that verifies that the field is selected when required is enabled.

Validation is triggered whenever the user toggles the switch. Programmatic toggling triggers validation as well. If validation fails, the component is marked as invalid and an error message is displayed below the input.

The required error message can be configured using either Switch.SwitchI18n.setRequiredErrorMessage(String) or HasValidationProperties.setErrorMessage(String).

For more advanced validation that requires custom rules, you can use Binder. Please note that Binder provides its own API for the required validation, see asRequired().

However, if Binder doesn't fit your needs and you want to implement fully custom validation logic, you can disable the built-in validation by setting setManualValidation(boolean) to true. This will allow you to control the invalid state and the error message manually using HasValidationProperties.setInvalid(boolean) and HasValidationProperties.setErrorMessage(String) API.

Since:
25.3
Author:
Vaadin Ltd
See Also: