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
Fields -
Constructor Summary
ConstructorsConstructorDescriptionNullableField(AbstractField<?, T> innerField, HasValue<?, Boolean> enabledField) Create an instance using the givenAbstractFieldand the given enablement field.NullableField(AbstractField<?, T> innerField, String checkboxLabel) Create an instance using the givenAbstractFieldand a newCheckboxwith the given label.Create an instance using the given field, component, and enablement field. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidBuild the layout for this field.protected ValidationResultBuild aValidationResultto be returned byvalidate()in the situation where the checkbox is checked, but the inner field is returning a value of null.protected Tprotected voidhandleEnabledFieldChange(boolean enabled) booleanDetermine whether to display error messages.booleanDetermine whether the inner field forgets its value when the checkbox is unchecked.protected voidsetComponentEnabled(boolean enabled) Set whether the inner component is enabled, if possible.voidsetDisplayErrorMessages(boolean displayErrorMessages) voidsetErrorMessage(String errorMessage) Sets an error message to the component.protected voidsetPresentationValue(T value) voidsetResetOnDisable(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, updateValueMethods inherited from class com.vaadin.flow.component.AbstractField
addValueChangeListener, getEmptyValue, getValue, isEmpty, setModelValue, setValue, valueEqualsMethods 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, setVisibleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.BlurNotifier
addBlurListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.Focusable
addFocusShortcut, blur, focus, getTabIndex, setTabIndexMethods inherited from interface com.vaadin.flow.component.FocusNotifier
addFocusListenerMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasEnabled
isEnabled, setEnabledMethods inherited from interface com.vaadin.flow.component.HasHelper
getHelperComponent, getHelperText, setHelperComponent, setHelperTextMethods 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, setWidthFullMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassNameMethods inherited from interface com.vaadin.flow.component.HasTheme
addThemeName, addThemeNames, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeNameMethods inherited from interface com.vaadin.flow.component.shared.HasTooltip
getTooltip, setTooltipTextMethods inherited from interface com.vaadin.flow.component.HasValidation
setManualValidationMethods inherited from interface com.vaadin.flow.component.shared.HasValidationProperties
getErrorMessage, isInvalid, setInvalidMethods inherited from interface com.vaadin.flow.component.HasValue
addValueChangeListener, clear, getEmptyValue, getOptionalValue, getValue, isEmpty, setValueMethods inherited from interface com.vaadin.flow.component.HasValueAndElement
isReadOnly, isRequiredIndicatorVisible, setReadOnly, setRequiredIndicatorVisibleMethods 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 givenAbstractFieldand a newCheckboxwith the given label.- Parameters:
innerField- the inner fieldcheckboxLabel- label for the checkbox, or null for none
-
NullableField
Create an instance using the givenAbstractFieldand the given enablement field.- Parameters:
innerField- the inner fieldenabledField- field that controls nullability- Throws:
IllegalArgumentException- ifinnerFieldis nullIllegalArgumentException- ifenabledFieldis 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
NullableFielddelegates to the overridden superclass method only ifisDisplayErrorMessages()returns true.- Specified by:
setErrorMessagein interfaceHasValidation- Specified by:
setErrorMessagein interfaceHasValidationProperties- See Also:
-
validate
Description copied from interface:ValidatingFieldValidate this instance.- Specified by:
validatein 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 aValidationResultto 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
NullableFieldreturns a "Required value" error.- Returns:
- validation error
- See Also:
-
buildLayout
protected void buildLayout()Build the layout for this field.The implementation in
NullableFieldreturns aHorizontalLayoutwith 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:
generateModelValuein classCustomField<T>
-
setPresentationValue
- Specified by:
setPresentationValuein classCustomField<T>
-