Class NullableField<T>
- Type Parameters:
T
- field value type
- All Implemented Interfaces:
AttachNotifier
,BlurNotifier<CustomField<T>>
,DetachNotifier
,Focusable<CustomField<T>>
,FocusNotifier<CustomField<T>>
,HasElement
,HasEnabled
,HasHelper
,HasLabel
,HasSize
,HasStyle
,HasTheme
,HasValidation
,HasValue<AbstractField.ComponentValueChangeEvent<CustomField<T>,
,T>, T> HasValueAndElement<AbstractField.ComponentValueChangeEvent<CustomField<T>,
,T>, T> HasTooltip
,HasValidationProperties
,InputField<AbstractField.ComponentValueChangeEvent<CustomField<T>,
,T>, T> Serializable
,ValidatingField<AbstractField.ComponentValueChangeEvent<CustomField<T>,
T>, T>
CustomField
that wraps an inner field and prepends a checkbox (or other boolean field) in front of it
that controls whether the value is null or not.
When the checkbox is checked, the inner field functions normally and provides this field's value. When the checkbox is unchecked, the inner field is completely disabled and this field's value is null.
Resetting On Disable
When the checkbox is unchecked, by default the inner (wrapped) field is automatically reset to its
empty value. To have the inner field retain its value instead,
so the value reappears if the checkbox is unchecked, use setResetOnDisable(false)
.
Validation
This class implements ValidatingField
, delegating to the inner field (for non-null values only) if it also
implements ValidatingField
.
-
Nested Class Summary
Nested classes/interfaces inherited from class com.vaadin.flow.component.AbstractField
AbstractField.ComponentValueChangeEvent<C extends Component,
V extends Object> 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.HasValue
HasValue.ValueChangeEvent<V extends Object>, HasValue.ValueChangeListener<E extends HasValue.ValueChangeEvent<?>>
-
Field Summary
-
Constructor Summary
ConstructorDescriptionNullableField
(AbstractField<?, T> innerField, HasValue<?, Boolean> enabledField) Create an instance using the givenAbstractField
and the given enablement field.NullableField
(AbstractField<?, T> innerField, String checkboxLabel) Create an instance using the givenAbstractField
and a newCheckbox
with the given label.Create an instance using the given field, component, and enablement field. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Build the layout for this field.protected ValidationResult
Build aValidationResult
to be returned byvalidate()
in the situation where the checkbox is checked, but the inner field is returning a value of null.protected T
protected void
handleEnabledFieldChange
(boolean enabled) boolean
Determine whether to display error messages.boolean
Determine whether the inner field forgets its value when the checkbox is unchecked.protected void
setComponentEnabled
(boolean enabled) Set whether the inner component is enabled, if possible.void
setDisplayErrorMessages
(boolean displayErrorMessages) void
setErrorMessage
(String errorMessage) Sets an error message to the component.protected void
setPresentationValue
(T value) void
setResetOnDisable
(boolean resetOnDisable) validate
(T value, ValueContext ctx) Validate this instance.Methods inherited from class com.vaadin.flow.component.customfield.CustomField
add, addThemeVariants, getLabel, onAttach, remove, removeThemeVariants, setLabel, updateValue
Methods inherited from class com.vaadin.flow.component.AbstractField
addValueChangeListener, getEmptyValue, getValue, isEmpty, setModelValue, setValue, 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.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.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.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.HasTheme
addThemeName, addThemeNames, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeName
Methods inherited from interface com.vaadin.flow.component.shared.HasTooltip
getTooltip, setTooltipText
Methods inherited from interface com.vaadin.flow.component.HasValidation
setManualValidation
Methods inherited from interface com.vaadin.flow.component.shared.HasValidationProperties
getErrorMessage, isInvalid, setInvalid
Methods inherited from interface com.vaadin.flow.component.HasValue
addValueChangeListener, clear, getEmptyValue, getOptionalValue, getValue, isEmpty, setValue
Methods inherited from interface com.vaadin.flow.component.HasValueAndElement
isReadOnly, isRequiredIndicatorVisible, setReadOnly, setRequiredIndicatorVisible
Methods inherited from interface org.dellroad.stuff.vaadin24.field.ValidatingField
addValidationTo
-
Field Details
-
DEFAULT_ENABLED_BUT_NULL_ERROR
- See Also:
-
innerField
The inner field. -
enabledField
The checkbox (or other boolean field). -
component
The inner field component.
-
-
Constructor Details
-
NullableField
Create an instance using the givenAbstractField
and a newCheckbox
with the given label.- Parameters:
innerField
- the inner fieldcheckboxLabel
- label for the checkbox, or null for none
-
NullableField
Create an instance using the givenAbstractField
and the given enablement field.- Parameters:
innerField
- the inner fieldenabledField
- field that controls nullability- Throws:
IllegalArgumentException
- ifinnerField
is nullIllegalArgumentException
- ifenabledField
is null
-
NullableField
public NullableField(HasValue<?, T> innerField, Component component, HasValue<?, Boolean> enabledField) Create an instance using the given field, component, and enablement field.- Parameters:
innerField
- the inner fieldcomponent
- the component corresponding toinnerField
(typically this is same object)enabledField
- field that controls nullability- Throws:
IllegalArgumentException
- if any parameter is null
-
-
Method Details
-
getInnerField
-
getEnabledField
-
getComponent
-
isResetOnDisable
public boolean isResetOnDisable()Determine whether the inner field forgets its value when the checkbox is unchecked.Default is true.
- Returns:
- true if the inner field is reset when the checkbox is unchecked
-
setResetOnDisable
public void setResetOnDisable(boolean resetOnDisable) -
isDisplayErrorMessages
public boolean isDisplayErrorMessages()Determine whether to display error messages.This method is invoked by
setErrorMessage()
.Default is true.
- Returns:
- true to display error messages, false to not display
-
setDisplayErrorMessages
public void setDisplayErrorMessages(boolean displayErrorMessages) -
setErrorMessage
Sets an error message to the component.The implementation in
NullableField
delegates to the overridden superclass method only ifisDisplayErrorMessages()
returns true.- Specified by:
setErrorMessage
in interfaceHasValidation
- Specified by:
setErrorMessage
in interfaceHasValidationProperties
- See Also:
-
validate
Description copied from interface:ValidatingField
Validate this instance.- Specified by:
validate
in interfaceValidatingField<AbstractField.ComponentValueChangeEvent<CustomField<T>,
T>, T> - Parameters:
value
- field value to validatectx
- the value context for validation- Returns:
- the validation result
-
handleEnabledFieldChange
protected void handleEnabledFieldChange(boolean enabled) -
enabledButNullValidationResult
Build aValidationResult
to be returned byvalidate()
in the situation where the checkbox is checked, but the inner field is returning a value of null.This would typically indicate that the inner field is incompletely filled-in or otherwise invalid.
In this situation,
validate()
should return a validation error, because the user has explicitly said they want a non-null value by checking the checkbox.The implementation in
NullableField
returns a "Required value" error.- Returns:
- validation error
- See Also:
-
buildLayout
protected void buildLayout()Build the layout for this field.The implementation in
NullableField
returns aHorizontalLayout
with the field that controls nullability (if it's aComponent
) followed by the inner component. -
setComponentEnabled
protected void setComponentEnabled(boolean enabled) Set whether the inner component is enabled, if possible.- Parameters:
enabled
- true to enable, false to disable
-
generateModelValue
- Specified by:
generateModelValue
in classCustomField<T>
-
setPresentationValue
- Specified by:
setPresentationValue
in classCustomField<T>
-