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
,Select
,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 TypeMethodDescriptionGets current error message from the component.boolean
Returnstrue
if component input is invalid,false
otherwise.void
setErrorMessage
(String errorMessage) Sets an error message to the component.void
setInvalid
(boolean invalid) Sets the validity of the component input.default void
setManualValidation
(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).
- Parameters:
errorMessage
- a new error message
-
getErrorMessage
String getErrorMessage()Gets current error message from the component.- Returns:
- current error message
-
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.- Parameters:
invalid
- new value for component input validity
-
isInvalid
boolean isInvalid()Returnstrue
if component input is invalid,false
otherwise.- Returns:
- whether the component input is valid
-