Class NumberField
- All Implemented Interfaces:
AttachNotifier
,BlurNotifier<NumberField>
,CompositionNotifier
,DetachNotifier
,Focusable<NumberField>
,FocusNotifier<NumberField>
,HasAriaLabel
,HasElement
,HasEnabled
,HasHelper
,HasLabel
,HasPlaceholder
,HasSize
,HasStyle
,HasTheme
,HasValidation
,HasValue<AbstractField.ComponentValueChangeEvent<NumberField,
,Double>, Double> HasValueAndElement<AbstractField.ComponentValueChangeEvent<NumberField,
,Double>, Double> InputNotifier
,KeyNotifier
,HasAllowedCharPattern
,HasClearButton
,HasClientValidation
,HasPrefix
,HasSuffix
,HasThemeVariant<TextFieldVariant>
,HasTooltip
,HasValidationProperties
,InputField<AbstractField.ComponentValueChangeEvent<NumberField,
,Double>, Double> HasAutocapitalize
,HasAutocomplete
,HasAutocorrect
,HasPrefixAndSuffix
,HasValidator<Double>
,HasValueChangeMode
,Serializable
Validation
Number Field comes with a built-in validation mechanism based on constraints. Validation is triggered whenever the user applies an input change, for example by pressing Enter or blurring the field. Programmatic value changes trigger validation as well. In eager and lazy value change modes, validation is also triggered on every character press with a delay according to the selected mode.
Validation verifies that the value is parsable into Double
and
satisfies the specified constraints. If validation fails, the component is
marked as invalid and an error message is displayed below the input.
The following constraints are supported:
Error messages for unparsable input and constraints can be configured with
the NumberField.NumberFieldI18n
object, using the respective properties. If you
want to provide a single catch-all error message, you can also use the
setErrorMessage(String)
method. Note that such an error message will
take priority over i18n error messages if both are set.
For more advanced validation that requires custom rules, you can use
Binder
. By default, before running custom validators, Binder will
also check if the value is parsable and satisfies the component constraints,
displaying error messages from the NumberField.NumberFieldI18n
object. The
exception is the required constraint, for which Binder provides its own API,
see asRequired()
.
However, if Binder doesn't fit your needs and you want to implement fully
custom validation logic, you can disable the constraint validation by setting
AbstractNumberField.setManualValidation(boolean)
to true. This will allow you to control
the invalid state and the error message manually using
HasValidationProperties.setInvalid(boolean)
and setErrorMessage(String)
API.
- Author:
- Vaadin Ltd.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
The internationalization properties forNumberField
.Nested classes/interfaces inherited from class com.vaadin.flow.component.AbstractField
AbstractField.ComponentValueChangeEvent<C extends Component,
V> Nested classes/interfaces inherited from interface com.vaadin.flow.component.BlurNotifier
BlurNotifier.BlurEvent<C extends Component>
Nested classes/interfaces inherited from interface com.vaadin.flow.component.FocusNotifier
FocusNotifier.FocusEvent<C extends Component>
Nested classes/interfaces inherited from interface com.vaadin.flow.component.shared.HasClientValidation
HasClientValidation.ClientValidatedEvent
Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasValue
HasValue.ValueChangeEvent<V>, HasValue.ValueChangeListener<E extends HasValue.ValueChangeEvent<?>>
-
Field Summary
Fields inherited from interface com.vaadin.flow.component.textfield.HasAutocapitalize
AUTOCAPITALIZE_ATTRIBUTE
Fields inherited from interface com.vaadin.flow.component.textfield.HasAutocomplete
AUTOCOMPLETE_ATTRIBUTE
Fields inherited from interface com.vaadin.flow.component.textfield.HasAutocorrect
AUTOCORRECT_ATTRIBUTE
Fields inherited from interface com.vaadin.flow.data.value.HasValueChangeMode
DEFAULT_CHANGE_TIMEOUT
-
Constructor Summary
ConstructorDescriptionConstructs an emptyNumberField
.NumberField
(HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<NumberField, Double>> listener) Constructs an emptyNumberField
with a value change listener.NumberField
(String label) Constructs an emptyNumberField
with the given label.NumberField
(String label, HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<NumberField, Double>> listener) Constructs an emptyNumberField
with a value change listener and a label.NumberField
(String label, Double initialValue, HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<NumberField, Double>> listener) Constructs aNumberField
with a value change listener, a label and an initial value.NumberField
(String label, String placeholder) Constructs an emptyNumberField
with the given label and placeholder text. -
Method Summary
Modifier and TypeMethodDescriptiongetI18n()
Gets the internationalization object previously set for this component.double
getMax()
Gets the maximum value for this field.double
getMin()
Gets the minimum value for this field.double
getStep()
Gets the allowed number intervals for this field.void
setErrorMessage
(String errorMessage) Sets a single error message to display for all constraint violations.void
Sets the internationalization object for this component.void
setMax
(double max) Sets the maximum value for this field.void
setMin
(double min) Sets the minimum value for this field.void
setRequiredIndicatorVisible
(boolean required) Sets whether the user is required to provide a value.void
setStep
(double step) Sets the allowed number intervals for this field.Methods inherited from class com.vaadin.flow.component.textfield.AbstractNumberField
addValidationStatusChangeListener, getDefaultValidator, getEmptyValue, getMaxDouble, getMinDouble, getStepDouble, getValue, isStepButtonsVisible, onAttach, setI18n, setManualValidation, setModelValue, setStepButtonsVisible, setValue, setValueChangeMode, validate
Methods inherited from class com.vaadin.flow.component.textfield.TextFieldBase
addToInput, addToPrefix, addToSuffix, getAriaLabel, getAriaLabelledBy, getTitle, getValueChangeMode, getValueChangeTimeout, isAutofocus, isAutoselect, isRequired, isRequiredIndicatorVisible, setAriaLabel, setAriaLabelledBy, setAutofocus, setAutoselect, setLabel, setRequired, setTitle, setValueChangeTimeout
Methods inherited from class com.vaadin.flow.component.AbstractSinglePropertyField
getSynchronizationRegistration, hasValidValue, setPresentationValue, setSynchronizedEvent
Methods inherited from class com.vaadin.flow.component.AbstractField
addValueChangeListener, isEmpty, valueEquals
Methods inherited from class com.vaadin.flow.component.Component
addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisible
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListener
Methods inherited from interface com.vaadin.flow.component.BlurNotifier
addBlurListener
Methods inherited from interface com.vaadin.flow.component.CompositionNotifier
addCompositionEndListener, addCompositionStartListener, addCompositionUpdateListener
Methods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListener
Methods inherited from interface com.vaadin.flow.component.Focusable
addFocusShortcut, blur, focus, getTabIndex, setTabIndex
Methods inherited from interface com.vaadin.flow.component.FocusNotifier
addFocusListener
Methods inherited from interface com.vaadin.flow.component.shared.HasAllowedCharPattern
getAllowedCharPattern, setAllowedCharPattern
Methods inherited from interface com.vaadin.flow.component.textfield.HasAutocapitalize
getAutocapitalize, setAutocapitalize
Methods inherited from interface com.vaadin.flow.component.textfield.HasAutocomplete
getAutocomplete, setAutocomplete
Methods inherited from interface com.vaadin.flow.component.textfield.HasAutocorrect
isAutocorrect, setAutocorrect
Methods inherited from interface com.vaadin.flow.component.shared.HasClearButton
isClearButtonVisible, setClearButtonVisible
Methods inherited from interface com.vaadin.flow.component.shared.HasClientValidation
addClientValidatedEventListener
Methods inherited from interface com.vaadin.flow.component.HasElement
getElement
Methods inherited from interface com.vaadin.flow.component.HasEnabled
isEnabled, setEnabled
Methods inherited from interface com.vaadin.flow.component.HasHelper
getHelperComponent, getHelperText, setHelperComponent, setHelperText
Methods inherited from interface com.vaadin.flow.component.HasPlaceholder
getPlaceholder, setPlaceholder
Methods inherited from interface com.vaadin.flow.component.shared.HasPrefix
getPrefixComponent, setPrefixComponent
Methods inherited from interface com.vaadin.flow.component.HasSize
getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFull
Methods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
Methods inherited from interface com.vaadin.flow.component.shared.HasSuffix
getSuffixComponent, setSuffixComponent
Methods inherited from interface com.vaadin.flow.component.HasTheme
addThemeName, addThemeNames, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeName
Methods inherited from interface com.vaadin.flow.component.shared.HasThemeVariant
addThemeVariants, removeThemeVariants
Methods inherited from interface com.vaadin.flow.component.shared.HasTooltip
getTooltip, setTooltipText
Methods inherited from interface com.vaadin.flow.component.shared.HasValidationProperties
getErrorMessage, isInvalid, setInvalid
Methods inherited from interface com.vaadin.flow.component.HasValue
addValueChangeListener, clear, getOptionalValue, isEmpty
Methods inherited from interface com.vaadin.flow.component.HasValueAndElement
isReadOnly, setReadOnly
Methods inherited from interface com.vaadin.flow.component.InputNotifier
addInputListener
Methods inherited from interface com.vaadin.flow.component.KeyNotifier
addKeyDownListener, addKeyDownListener, addKeyPressListener, addKeyPressListener, addKeyUpListener, addKeyUpListener
-
Constructor Details
-
NumberField
public NumberField()Constructs an emptyNumberField
. -
NumberField
Constructs an emptyNumberField
with the given label.- Parameters:
label
- the text to set as the label
-
NumberField
Constructs an emptyNumberField
with the given label and placeholder text.- Parameters:
label
- the text to set as the labelplaceholder
- the placeholder text to set
-
NumberField
public NumberField(HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<NumberField, Double>> listener) Constructs an emptyNumberField
with a value change listener.- Parameters:
listener
- the value change listener- See Also:
-
NumberField
public NumberField(String label, HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<NumberField, Double>> listener) Constructs an emptyNumberField
with a value change listener and a label.- Parameters:
label
- the text to set as the labellistener
- the value change listener- See Also:
-
NumberField
public NumberField(String label, Double initialValue, HasValue.ValueChangeListener<? super AbstractField.ComponentValueChangeEvent<NumberField, Double>> listener) Constructs aNumberField
with a value change listener, a label and an initial value.- Parameters:
label
- the text to set as the labelinitialValue
- the initial valuelistener
- the value change listener- See Also:
-
-
Method Details
-
setErrorMessage
Sets a single error message to display for all constraint violations. The error message will only appear when the component is flagged as invalid, either as a result of constraint validation or by the developer throughHasValidationProperties.setInvalid(boolean)
if manual validation mode is enabled.Distinct error messages for unparsable input and different constraints can be configured with the
NumberField.NumberFieldI18n
object, using the respective properties. However, note that the error message set withsetErrorMessage(String)
will take priority and override any i18n error messages if both are set.- Specified by:
setErrorMessage
in interfaceHasValidation
- Specified by:
setErrorMessage
in interfaceHasValidationProperties
- Parameters:
errorMessage
- the error message to set, ornull
to clear
-
setRequiredIndicatorVisible
public void setRequiredIndicatorVisible(boolean required) Description copied from class:TextFieldBase
Sets whether the user is required to provide a value. When required, an indicator appears next to the label and the field invalidates if the value is cleared.NOTE: The required indicator is only visible when the field has a label, see
TextFieldBase.setLabel(String)
.- Specified by:
setRequiredIndicatorVisible
in interfaceHasValue<AbstractField.ComponentValueChangeEvent<NumberField,
Double>, Double> - Specified by:
setRequiredIndicatorVisible
in interfaceHasValueAndElement<AbstractField.ComponentValueChangeEvent<NumberField,
Double>, Double> - Overrides:
setRequiredIndicatorVisible
in classTextFieldBase<NumberField,
Double> - Parameters:
required
-true
to make the field required,false
otherwise- See Also:
-
setMin
public void setMin(double min) Sets the minimum value for this field. This will configure the field to invalidate if the entered value is below the minimum. It will also limit the decrementing of the value when step buttons are enabled.The minimum value is inclusive.
- Overrides:
setMin
in classAbstractNumberField<NumberField,
Double> - Parameters:
min
- the minimum double value to set- See Also:
-
getMin
public double getMin()Gets the minimum value for this field. The constraint activates only if the value was explicitly set withsetMin(int)
.- Returns:
- the minimum double value
- See Also:
-
setMax
public void setMax(double max) Sets the maximum value for this field. This will configure the field to invalidate if the entered value is above the maximum. It will also limit the incrementing of the value when step buttons are enabled.The maximum value is inclusive.
- Overrides:
setMax
in classAbstractNumberField<NumberField,
Double> - Parameters:
max
- the maximum double value to set- See Also:
-
getMax
public double getMax()Gets the maximum value for this field. The constraint activates only if the value was explicitly set withsetMax(double)
.- Returns:
- the maximum double value
- See Also:
-
setStep
public void setStep(double step) Sets the allowed number intervals for this field. This specifies how much the value will be increased/decreased. It is also used to invalidate the field, if the value doesn't align with the specified step andmin
(if explicitly specified by the developer).- Overrides:
setStep
in classAbstractNumberField<NumberField,
Double> - Parameters:
step
- the new step to set- Throws:
IllegalArgumentException
- if the argument is less or equal to zero.- See Also:
-
getStep
public double getStep()Gets the allowed number intervals for this field.- Returns:
- the
step
property from the webcomponent - See Also:
-
getI18n
Gets the internationalization object previously set for this component.NOTE: Updating the instance that is returned from this method will not update the component if not set again using
setI18n(NumberFieldI18n)
- Overrides:
getI18n
in classAbstractNumberField<NumberField,
Double> - Returns:
- the i18n object or
null
if no i18n object has been set
-
setI18n
Sets the internationalization object for this component.- Parameters:
i18n
- the i18n object, notnull
-