Interface HasValidation
- All Superinterfaces:
Serializable
- All Known Subinterfaces:
HasValidationProperties
- All Known Implementing Classes:
AbstractNumberField,BigDecimalField,Checkbox,CheckboxGroup,ComboBox,ComboBoxBase,CustomField,DatePicker,DateTimePicker,EmailField,IntegerField,MultiSelectComboBox,NumberField,PasswordField,RadioButtonGroup,RangeSlider,Select,Slider,TextArea,TextField,TextFieldBase,TimePicker
HasValidation is implemented by component when used with a Binder and
input is validated with a Binder.
- Since:
- 1.0.
- Author:
- Vaadin Ltd
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidbindErrorMessage(Signal<String> signal) Binds the component's error message to the provided signal so that the error message is kept in sync with the signal's current value.default voidbindInvalid(Signal<Boolean> signal) Binds the component's invalid state to the provided signal so that the invalid flag is kept in sync with the signal's current value.Gets the current error message from the component.booleanReturnstrueif component input is invalid,falseotherwise.voidsetErrorMessage(String errorMessage) Sets an error message to the component.voidsetInvalid(boolean invalid) Sets the validity of the component input.default voidsetManualValidation(boolean enabled) Sets whether manual validation mode is enabled for the component.
-
Method Details
-
setManualValidation
default void setManualValidation(boolean enabled) Sets whether manual validation mode is enabled for the component.When enabled, the component doesn't perform its built-in constraint validation on value change, blur, and other events. This allows manually controlling the invalid state and error messages using the
setInvalid(boolean)andsetErrorMessage(String)methods. Manual mode is helpful when there is a need for a totally custom validation logic that cannot be achieved with Binder.Example:
Field field = new Field(); field.setManualValidation(true); field.addValueChangeListener(event -> { if (Objects.equal(event.getValue(), "")) { field.setInvalid(true); field.setErrorMessage("The field is required."); } else { field.setInvalid(false); } });For components that don't have built-in validation, the method has no effect.
- Parameters:
enabled- whether to enable manual validation mode.
-
setErrorMessage
Sets an error message to the component.The Web Component is responsible for deciding when to show the error message to the user, and this is usually triggered by triggering the invalid state for the Web Component. Which means that there is no need to clean up the message when component becomes valid (otherwise it may lead to undesired visual effects).
While a signal binding for the error message is active, calls to this method throw a
com.vaadin.flow.signals.BindingActiveException.- Parameters:
errorMessage- a new error message- Throws:
BindingActiveException- thrown when there is already an existing signal binding
-
getErrorMessage
String getErrorMessage()Gets the current error message from the component.- Returns:
- current error message
-
bindErrorMessage
Binds the component's error message to the provided signal so that the error message is kept in sync with the signal's current value.Passing
nullas thesignalremoves any existing binding. When unbinding, the current error message is left unchanged.While a binding is active, manual calls to
setErrorMessage(String)throw acom.vaadin.flow.signals.BindingActiveException. Bindings are lifecycle-aware and only active while the owningComponentis in attached state; they are deactivated while the component is in detached state.- Parameters:
signal- the signal providing error messages, notnull- Throws:
BindingActiveException- thrown when there is already an existing binding- Since:
- 25.1
-
setInvalid
void setInvalid(boolean invalid) Sets the validity of the component input.When component becomes valid it hides the error message by itself, so there is no need to clean up the error message via the
setErrorMessage(String)call.NOTE: If you need to manually control the invalid state, consider enabling manual validation mode with
setManualValidation(boolean)to avoid potential conflicts between your custom validation and the component's built-in validation.While a signal binding for the invalid state is active, calls to this method throw a
com.vaadin.flow.signals.BindingActiveException.- Parameters:
invalid- new value for component input validity- Throws:
BindingActiveException- thrown when there is already an existing signal binding
-
isInvalid
boolean isInvalid()Returnstrueif component input is invalid,falseotherwise.- Returns:
- whether the component input is valid
-
bindInvalid
Binds the component's invalid state to the provided signal so that the invalid flag is kept in sync with the signal's current value.Passing
nullas thesignalremoves any existing binding. When unbinding, the current invalid state is left unchanged.While a binding is active, manual calls to
setInvalid(boolean)throw acom.vaadin.flow.signals.BindingActiveException. Bindings are lifecycle-aware and only active while the owning component is in the attached state; they are deactivated while the component is in the detached state.- Parameters:
signal- the signal providing invalid state flags, ornullto unbind- Throws:
BindingActiveException- thrown when there is already an existing binding- Since:
- 25.1
-