T
- the type of the items to be inserted in the combo box@JsModule(value="./flow-component-renderer.js") @JsModule(value="./comboBoxConnector-es6.js") @JavaScript(value="frontend://comboBoxConnector.js") public class ComboBox<T> extends GeneratedVaadinComboBox<ComboBox<T>,T> implements HasSize, HasValidation, HasFilterableDataProvider<T,String>, HasHelper, HasLabel, HasValidator<T>, HasClientValidation
vaadin-combo-box
webcomponent. It
contains the same features of the webcomponent, such as item filtering,
object selection and item templating.
ComboBox supports lazy loading. This means that when using large data sets,
items are requested from the server one "page" at a time when the user
scrolls down the overlay. The number of items in one page is by default 50,
and can be changed with setPageSize(int)
.
ComboBox can do filtering either in the browser or in the server. When
ComboBox has only a relatively small set of items, the filtering will happen
in the browser, allowing smooth user-experience. When the size of the data
set is larger than the pageSize
, the webcomponent doesn't necessarily
have all the data available and it will make requests to the server to handle
the filtering. Also, if you have defined custom filtering logic, with eg.
setItems(ItemFilter, Collection)
, filtering will happen in the
server. To enable client-side filtering with larger data sets, you can
override the pageSize
to be bigger than the size of your data set.
However, then the full data set will be sent to the client immediately and
you will lose the benefits of lazy loading.
Modifier and Type | Class and Description |
---|---|
static interface |
ComboBox.FetchItemsCallback<T>
A callback method for fetching items.
|
static interface |
ComboBox.ItemFilter<T>
Predicate to check
ComboBox items against user typed strings. |
GeneratedVaadinComboBox.CustomValueSetEvent<R extends GeneratedVaadinComboBox<R,?>>, GeneratedVaadinComboBox.FilterChangeEvent<R extends GeneratedVaadinComboBox<R,?>>, GeneratedVaadinComboBox.InvalidChangeEvent<R extends GeneratedVaadinComboBox<R,?>>, GeneratedVaadinComboBox.OpenedChangeEvent<R extends GeneratedVaadinComboBox<R,?>>, GeneratedVaadinComboBox.SelectedItemChangeEvent<R extends GeneratedVaadinComboBox<R,?>>
AbstractField.ComponentValueChangeEvent<C extends Component,V>
HasClientValidation.ClientValidatedEvent
BlurNotifier.BlurEvent<C extends Component>
FocusNotifier.FocusEvent<C extends Component>
HasValue.ValueChangeEvent<V>, HasValue.ValueChangeListener<E extends HasValue.ValueChangeEvent<?>>
Constructor and Description |
---|
ComboBox()
Default constructor.
|
ComboBox(int pageSize)
Creates an empty combo box with the defined page size for lazy loading.
|
ComboBox(String label)
Creates an empty combo box with the defined label.
|
ComboBox(String label,
Collection<T> items)
Creates a combo box with the defined label and populated with the items
in the collection.
|
ComboBox(String label,
T... items)
Creates a combo box with the defined label and populated with the items
in the array.
|
Modifier and Type | Method and Description |
---|---|
Registration |
addCustomValueSetListener(ComponentEventListener<GeneratedVaadinComboBox.CustomValueSetEvent<ComboBox<T>>> listener)
Adds a listener for the event which is fired when user inputs a string
value that does not match any existing items and commits it eg.
|
Registration |
addValidationStatusChangeListener(ValidationStatusChangeListener<T> listener)
Enables the implementing components to notify changes in their validation
status to the observers.
|
DataProvider<T,?> |
getDataProvider()
Gets the data provider used by this ComboBox.
|
T |
getEmptyValue()
Returns the value that represents an empty value.
|
String |
getErrorMessage()
Gets the current error message from the combobox.
|
ItemLabelGenerator<T> |
getItemLabelGenerator()
Gets the item label generator that is used to produce the strings shown
in the combo box for each item.
|
String |
getLabel()
Gets the label of the combobox.
|
int |
getPageSize()
Gets the page size, which is the number of items fetched at a time from
the data provider.
|
String |
getPattern()
Gets the valid input pattern
|
String |
getPlaceholder()
Gets the placeholder of the combobox.
|
boolean |
isAllowCustomValue()
If
true , the user can input string values that do not match to
any existing item labels, which will fire a CustomValueSetEvent . |
boolean |
isAutofocus()
Get the state for the auto-focus property of the combobox.
|
boolean |
isAutoOpen()
Gets whether dropdown will open automatically or not.
|
boolean |
isClearButtonVisible()
Gets whether this combo box displays a clear button when a value is
selected.
|
protected boolean |
isEnforcedFieldValidationEnabled() |
boolean |
isInvalid()
Gets the validity of the combobox output.
|
boolean |
isOpened()
Gets the states of the drop-down.
|
boolean |
isPreventInvalidInput()
Determines whether preventing the user from inputing invalid value.
|
boolean |
isRequired()
Determines whether the combobox is marked as input required.
|
protected void |
onAttach(AttachEvent attachEvent)
Called when the component is attached to a UI.
|
protected void |
onDetach(DetachEvent detachEvent)
Called when the component is detached from a UI.
|
void |
setAllowCustomValue(boolean allowCustomValue)
Enables or disables the component firing events for custom string input.
|
void |
setAutofocus(boolean autofocus)
Set the combobox to be input focused when the page loads.
|
void |
setAutoOpen(boolean autoOpen)
Enables or disables the dropdown opening automatically.
|
void |
setClearButtonVisible(boolean clearButtonVisible)
Allows displaying a clear button in the combo box when a value is
selected.
|
void |
setDataProvider(ComboBox.FetchItemsCallback<T> fetchItems,
SerializableFunction<String,Integer> sizeCallback)
Sets a CallbackDataProvider using the given fetch items callback and a
size callback.
|
void |
setDataProvider(ComboBox.ItemFilter<T> itemFilter,
ListDataProvider<T> listDataProvider)
Sets a list data provider with an item filter as the data provider of
this combo box.
|
<C> void |
setDataProvider(DataProvider<T,C> dataProvider,
SerializableFunction<String,C> filterConverter)
Sets the data provider and filter converter for this listing.
|
void |
setDataProvider(DataProvider<T,String> dataProvider)
Sets the data provider for this listing.
|
void |
setDataProvider(ListDataProvider<T> listDataProvider)
Sets a list data provider as the data provider of this combo box.
|
void |
setErrorMessage(String errorMessage)
Description copied from corresponding location in WebComponent:
|
void |
setInvalid(boolean invalid)
Description copied from corresponding location in WebComponent:
|
void |
setItemLabelGenerator(ItemLabelGenerator<T> itemLabelGenerator)
Sets the item label generator that is used to produce the strings shown
in the combo box for each item.
|
void |
setItems(Collection<T> items)
Sets the data items of this component provided as a collection.
|
void |
setItems(ComboBox.ItemFilter<T> itemFilter,
Collection<T> items)
Sets the data items of this combo box and a filtering function for
defining which items are displayed when user types into the combo box.
|
void |
setItems(ComboBox.ItemFilter<T> itemFilter,
T... items)
Sets the data items of this combo box and a filtering function for
defining which items are displayed when user types into the combo box.
|
void |
setLabel(String label)
Sets the label for the combobox.
|
void |
setOpened(boolean opened)
Description copied from corresponding location in WebComponent:
|
void |
setPageSize(int pageSize)
Sets the page size, which is the number of items requested at a time from
the data provider.
|
void |
setPattern(String pattern)
Description copied from corresponding location in WebComponent:
|
void |
setPlaceholder(String placeholder)
Description copied from corresponding location in WebComponent:
|
void |
setPreventInvalidInput(boolean preventInvalidInput)
Description copied from corresponding location in WebComponent:
|
void |
setRenderer(Renderer<T> renderer)
Sets the TemplateRenderer responsible to render the individual items in
the list of possible choices of the ComboBox.
|
void |
setRequired(boolean required)
Description copied from corresponding location in WebComponent:
|
void |
setRequiredIndicatorVisible(boolean requiredIndicatorVisible)
Sets the required indicator visible or not.
|
void |
setValue(T value)
Sets the value of this object.
|
protected void |
validate()
Description copied from corresponding location in WebComponent:
|
addFilterChangeListener, addInvalidChangeListener, addOpenedChangeListener, addSelectedItemChangeListener, addToPrefix, cancel, checkValidity, clearCache, close, getErrorMessageString, getFilteredItemsJsonArray, getFilterString, getItemIdPathString, getItemLabelPathString, getItemsJsonArray, getItemValuePathString, getLabelString, getNameString, getPageSizeDouble, getPatternString, getPlaceholderString, getSelectedItemJsonObject, getSizeDouble, isAllowCustomValueBoolean, isAutofocusBoolean, isClearButtonVisibleBoolean, isDisabledBoolean, isInvalidBoolean, isLoadingBoolean, isOpenedBoolean, isPreventInvalidInputBoolean, isReadonlyBoolean, isRequiredBoolean, open, remove, removeAll, render, setDisabled, setFilter, setFilteredItems, setItemIdPath, setItemLabelPath, setItems, setItemValuePath, setLoading, setName, setPageSize, setReadonly, setSelectedItem, setSize
getSynchronizationRegistration, hasValidValue, setPresentationValue, setSynchronizedEvent
addValueChangeListener, getValue, isEmpty, setModelValue, valueEquals
addListener, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onEnabledStateChanged, set, setElement, setId, setVisible
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getCssSize, getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFull
getHelperComponent, getHelperText, setHelperComponent, setHelperText
getElement
getDefaultValidator
addClientValidatedEventListener
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
addFocusShortcut, blur, focus, getTabIndex, setTabIndex
addBlurListener
addFocusListener
isEnabled, setEnabled
isReadOnly, isRequiredIndicatorVisible, setReadOnly
clear, getOptionalValue
addAttachListener
addDetachListener
public ComboBox(int pageSize)
The default page size is 50.
The page size is also the largest number of items that can support client-side filtering. If you provide more items than the page size, the component has to fall back to server-side filtering.
pageSize
- the amount of items to request at a time for lazy loading#setPageSize(int)}
public ComboBox()
public ComboBox(String label)
label
- the label describing the combo boxpublic ComboBox(String label, Collection<T> items)
label
- the label describing the combo boxitems
- the items to be shown in the list of the combo boxsetItems(Collection)
@SafeVarargs public ComboBox(String label, T... items)
label
- the label describing the combo boxitems
- the items to be shown in the list of the combo boxHasItems.setItems(Object...)
public void setValue(T value)
HasValue
getValue()
, fires a value change event. May throw
IllegalArgumentException
if the value is not acceptable.
Implementation note: the implementing class should document
whether null values are accepted or not, and override
HasValue.getEmptyValue()
if the empty value is not null
.
public void setRenderer(Renderer<T> renderer)
setItemLabelGenerator(ItemLabelGenerator)
.renderer
- a renderer for the items in the selection list of the
ComboBox, not null
Note that filtering of the ComboBox is not affected by the
renderer that is set here. Filtering is done on the original
values and can be affected by
setItemLabelGenerator(ItemLabelGenerator)
.public void setItems(Collection<T> items)
The provided collection instance may be used as-is. Subsequent modification of the collection might cause inconsistent data to be shown in the component unless it is explicitly instructed to read the data again.
Filtering will use a case insensitive match to show all items where the
filter text is a substring of the label displayed for that item, which
you can configure with
setItemLabelGenerator(ItemLabelGenerator)
.
Filtering will be handled in the client-side if the size of the data set
is less than the page size. To force client-side filtering with a larger
data set (at the cost of increased network traffic), you can increase the
page size with setPageSize(int)
.
Setting the items creates a new DataProvider, which in turn resets the
combo box's value to null
. If you want to add and remove items to
the current item set without resetting the value, you should update the
previously set item collection and call
getDataProvider().refreshAll()
.
public void setItems(ComboBox.ItemFilter<T> itemFilter, Collection<T> items)
Note that defining a custom filter will force the component to make
server roundtrips to handle the filtering. Otherwise it can handle
filtering in the client-side, if the size of the data set is less than
the pageSize
.
Setting the items creates a new DataProvider, which in turn resets the
combo box's value to null
. If you want to add and remove items to
the current item set without resetting the value, you should update the
previously set item collection and call
getDataProvider().refreshAll()
.
itemFilter
- filter to check if an item is shown when user typed some text
into the ComboBoxitems
- the data items to displaypublic void setItems(ComboBox.ItemFilter<T> itemFilter, T... items)
Note that defining a custom filter will force the component to make
server roundtrips to handle the filtering. Otherwise it can handle
filtering in the client-side, if the size of the data set is less than
the pageSize
.
Setting the items creates a new DataProvider, which in turn resets the
combo box's value to null
. If you want to add and remove items to
the current item set without resetting the value, you should update the
previously set item collection and call
getDataProvider().refreshAll()
.
itemFilter
- filter to check if an item is shown when user typed some text
into the ComboBoxitems
- the data items to displaypublic void setDataProvider(DataProvider<T,String> dataProvider)
The filter-type of the given data provider must be String so that it can
handle the filters typed into the ComboBox by users. If your data
provider uses some other type of filter, you can provide a function which
converts the ComboBox's filter-string into that type via
setDataProvider(DataProvider, SerializableFunction)
. Another way
to do the same thing is to use this method with your data provider
converted with
DataProvider.withConvertedFilter(SerializableFunction)
.
Changing the combo box's data provider resets its current value to
null
.
setDataProvider
in interface HasFilterableDataProvider<T,String>
dataProvider
- the data provider, not null
public <C> void setDataProvider(DataProvider<T,C> dataProvider, SerializableFunction<String,C> filterConverter)
ComboBox triggers filtering queries based on the strings users type into
the field. For this reason you need to provide the second parameter, a
function which converts the filter-string typed by the user into
filter-type used by your data provider. If your data provider already
supports String as the filter-type, it can be used without a converter
function via setDataProvider(DataProvider)
.
Using this method provides the same result as using a data provider
wrapped with
DataProvider.withConvertedFilter(SerializableFunction)
.
Changing the combo box's data provider resets its current value to
null
.
setDataProvider
in interface HasFilterableDataProvider<T,String>
C
- the filter typedataProvider
- the data provider, not null
filterConverter
- a function that converts filter values produced by this
listing into filter values expected by the provided data
provider, not null
protected void onAttach(AttachEvent attachEvent)
Component
The default implementation does nothing.
This method is invoked before the AttachEvent
is fired for the
component.
protected void onDetach(DetachEvent detachEvent)
Component
The default implementation does nothing.
This method is invoked before the DetachEvent
is fired for the
component.
public void setDataProvider(ListDataProvider<T> listDataProvider)
Filtering will use a case insensitive match to show all items where the
filter text is a substring of the label displayed for that item, which
you can configure with
setItemLabelGenerator(ItemLabelGenerator)
.
Filtering will be handled in the client-side if the size of the data set
is less than the page size. To force client-side filtering with a larger
data set (at the cost of increased network traffic), you can increase the
page size with setPageSize(int)
.
Changing the combo box's data provider resets its current value to
null
.
listDataProvider
- the list data provider to use, not null
public void setDataProvider(ComboBox.FetchItemsCallback<T> fetchItems, SerializableFunction<String,Integer> sizeCallback)
This method is a shorthand for making a CallbackDataProvider
that
handles a partial Query
object.
Changing the combo box's data provider resets its current value to
null
.
fetchItems
- a callback for fetching itemssizeCallback
- a callback for getting the count of itemsCallbackDataProvider
,
setDataProvider(DataProvider)
public void setDataProvider(ComboBox.ItemFilter<T> itemFilter, ListDataProvider<T> listDataProvider)
Note that defining a custom filter will force the component to make
server roundtrips to handle the filtering. Otherwise it can handle
filtering in the client-side, if the size of the data set is less than
the pageSize
.
Changing the combo box's data provider resets its current value to
null
.
itemFilter
- filter to check if an item is shown when user typed some text
into the ComboBoxlistDataProvider
- the list data provider to use, not null
public DataProvider<T,?> getDataProvider()
public void setItemLabelGenerator(ItemLabelGenerator<T> itemLabelGenerator)
String.valueOf(Object)
is used.
When the setRenderer(Renderer)
is used, the ItemLabelGenerator
is only used to show the selected item label.
itemLabelGenerator
- the item label provider to use, not nullpublic ItemLabelGenerator<T> getItemLabelGenerator()
public void setPageSize(int pageSize)
The page size is also the largest number of items that can support client-side filtering. If you provide more items than the page size, the component has to fall back to server-side filtering.
Setting the page size after the ComboBox has been rendered effectively resets the component, and the current page(s) and sent over again.
The default page size is 50.
pageSize
- the maximum number of items sent per request, should be
greater than zeropublic int getPageSize()
The page size is also the largest number of items that can support client-side filtering. If you provide more items than the page size, the component has to fall back to server-side filtering.
The default page size is 50.
#setPageSize(int)}
public void setOpened(boolean opened)
GeneratedVaadinComboBox
Description copied from corresponding location in WebComponent:
True if the dropdown is open, false otherwise.
setOpened
in class GeneratedVaadinComboBox<ComboBox<T>,T>
opened
- the boolean value to setpublic boolean isOpened()
true
if the drop-down is opened, false
otherwisepublic void setInvalid(boolean invalid)
GeneratedVaadinComboBox
Description copied from corresponding location in WebComponent:
Set to true if the value is invalid.
setInvalid
in interface HasValidation
setInvalid
in class GeneratedVaadinComboBox<ComboBox<T>,T>
invalid
- the boolean value to setpublic boolean isInvalid()
return true, if the value is invalid.
isInvalid
in interface HasValidation
validity
property from the componentpublic void setErrorMessage(String errorMessage)
GeneratedVaadinComboBox
Description copied from corresponding location in WebComponent:
The error message to display when the input is invalid.
setErrorMessage
in interface HasValidation
setErrorMessage
in class GeneratedVaadinComboBox<ComboBox<T>,T>
errorMessage
- the String value to setpublic String getErrorMessage()
getErrorMessage
in interface HasValidation
public void setAllowCustomValue(boolean allowCustomValue)
When enabled, a CustomValueSetEvent
will be fired when the user
inputs a string value that does not match any existing items and commits
it eg. by blurring or pressing the enter-key.
Note that ComboBox doesn't do anything with the custom value string
automatically. Use the
addCustomValueSetListener(ComponentEventListener)
method to
determine how the custom value should be handled. For example, when the
ComboBox has String
as the value type, you can add a listener
which sets the custom string as the value of the ComboBox with
setValue(Object)
.
Setting to true
also allows an unfocused ComboBox to display a
string that doesn't match any of its items nor its current value, unless
this is explicitly handled with
addCustomValueSetListener(ComponentEventListener)
. When set to
false
, an unfocused ComboBox will always display the label of the
currently selected item.
setAllowCustomValue
in class GeneratedVaadinComboBox<ComboBox<T>,T>
allowCustomValue
- true
to enable custom value set events, false
to disable themaddCustomValueSetListener(ComponentEventListener)
public boolean isAllowCustomValue()
true
, the user can input string values that do not match to
any existing item labels, which will fire a CustomValueSetEvent
.true
if the component fires custom value set events,
false
otherwisesetAllowCustomValue(boolean)
,
addCustomValueSetListener(ComponentEventListener)
public void setAutoOpen(boolean autoOpen)
false
the dropdown is only opened when clicking the toggle button or pressing
Up or Down arrow keys.autoOpen
- false
to prevent the dropdown from opening
automaticallypublic boolean isAutoOpen()
false
otherwisepublic void setAutofocus(boolean autofocus)
setAutofocus
in class GeneratedVaadinComboBox<ComboBox<T>,T>
autofocus
- the boolean value to setpublic boolean isAutofocus()
This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.
autofocus
property from the comboboxpublic void setPreventInvalidInput(boolean preventInvalidInput)
GeneratedVaadinComboBox
Description copied from corresponding location in WebComponent:
Set to true to prevent the user from entering invalid input.
setPreventInvalidInput
in class GeneratedVaadinComboBox<ComboBox<T>,T>
preventInvalidInput
- the boolean value to setpublic boolean isPreventInvalidInput()
This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.
preventInvalidInput
property of the comboboxpublic void setRequired(boolean required)
GeneratedVaadinComboBox
Description copied from corresponding location in WebComponent:
Set to true to mark the input as required.
setRequired
in class GeneratedVaadinComboBox<ComboBox<T>,T>
required
- the boolean value to setpublic boolean isRequired()
This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.
true
if the input is required, false
otherwisepublic void setLabel(String label)
public String getLabel()
public void setPlaceholder(String placeholder)
GeneratedVaadinComboBox
Description copied from corresponding location in WebComponent:
A placeholder string in addition to the label.
setPlaceholder
in class GeneratedVaadinComboBox<ComboBox<T>,T>
placeholder
- the String value to setpublic String getPlaceholder()
placeholder
property of the comboboxpublic void setPattern(String pattern)
GeneratedVaadinComboBox
Description copied from corresponding location in WebComponent:
A pattern to validate the input
with.
setPattern
in class GeneratedVaadinComboBox<ComboBox<T>,T>
pattern
- the String value to setpublic String getPattern()
pattern
property of the comboboxpublic T getEmptyValue()
HasValue
By default HasValue
is expected to support null
as empty
values. Specific implementations might not support this.
getEmptyValue
in interface HasValue<AbstractField.ComponentValueChangeEvent<ComboBox<T>,T>,T>
getEmptyValue
in class AbstractField<ComboBox<T>,T>
public Registration addCustomValueSetListener(ComponentEventListener<GeneratedVaadinComboBox.CustomValueSetEvent<ComboBox<T>>> listener)
Note that ComboBox doesn't do anything with the custom value string
automatically. Use this method to determine how the custom value should
be handled. For example, when the ComboBox has String
as the
value type, you can add a listener which sets the custom string as the
value of the ComboBox with setValue(Object)
.
As a side effect, this makes the ComboBox allow custom values. If you
want to disable the firing of custom value set events once the listener
is added, please disable it explicitly via the
setAllowCustomValue(boolean)
method.
The custom value becomes disallowed automatically once the last custom value set listener is removed.
addCustomValueSetListener
in class GeneratedVaadinComboBox<ComboBox<T>,T>
listener
- the listener to be notified when a new value is filledRegistration
for removing the event listenersetAllowCustomValue(boolean)
public void setRequiredIndicatorVisible(boolean requiredIndicatorVisible)
HasValue
If set visible, it is visually indicated in the user interface.
The method is intended to be used with Binder
which does
server-side validation. In case HTML element has its own (client-side)
validation it should be disabled when
setRequiredIndicatorVisible(true)
is called and re-enabled
back on setRequiredIndicatorVisible(false)
. It's
responsibility of each component implementation to follow the contract so
that the method call doesn't do anything else than show/hide the
"required" indication. Usually components provide their own
setRequired
method which should be called in case the
client-side validation is required.
setRequiredIndicatorVisible
in interface HasValue<AbstractField.ComponentValueChangeEvent<ComboBox<T>,T>,T>
setRequiredIndicatorVisible
in interface HasValueAndElement<AbstractField.ComponentValueChangeEvent<ComboBox<T>,T>,T>
requiredIndicatorVisible
- true
to make the required indicator visible,
false
if notpublic void setClearButtonVisible(boolean clearButtonVisible)
The clear button is an icon, which can be clicked to set the combo box
value to null
.
setClearButtonVisible
in class GeneratedVaadinComboBox<ComboBox<T>,T>
clearButtonVisible
- true
to display the clear button, false
to
hide itpublic boolean isClearButtonVisible()
true
if this combo box displays a clear button,
false
otherwisesetClearButtonVisible(boolean)
protected void validate()
GeneratedVaadinComboBox
Description copied from corresponding location in WebComponent:
Returns true if value
is valid, and sets the invalid
flag
appropriately.
This function is not supported by Flow because it returns a
boolean
. Functions with return types different than void are
not supported at this moment.
validate
in class GeneratedVaadinComboBox<ComboBox<T>,T>
public Registration addValidationStatusChangeListener(ValidationStatusChangeListener<T> listener)
HasValidator
Note: This method can be overridden by the implementing
classes e.g. components, to enable the associated Binder.Binding
instance subscribing for their validation change events and revalidate
itself.
This method primarily designed for notifying the Binding about the
validation status changes of a bound component at the client-side.
WebComponents such as <vaadin-date-picker>
or any
other component that accept a formatted text as input should be able to
communicate their invalid status to their server-side instance, and a
bound server-side component instance must notify its binding about this
validation status change as well. When the binding instance revalidates,
a chain of validators and convertors get executed one of which is the
default validator provided by HasValidator.getDefaultValidator()
.
Thus, In order for the binding to be able to show/clear errors for its
associated bound field, it is important that implementing components take
that validation status into account while implementing any validator and
converter including HasValidator.getDefaultValidator()
. Here is
an example:
@Tag("date-picker-demo")
public class DatePickerDemo implements HasValidator<LocalDate> {
// Each web component has a way to communicate its validation status
// to its server-side component instance. The following clientSideValid
// state is introduced here just for the sake of simplicity of this code
// snippet:
boolean clientSideValid = true;
/**
* Note how clientSideValid
engaged in the definition
* of this method. It is important to reflect this status either
* in the returning validation result of this method or any other
* validation that is associated with this component.
*/
@Override
public Validator getDefaultValidator() {
return (value, valueContext) -> clientSideValid ? ValidationResult.ok()
: ValidationResult.error("Invalid date format");
}
private final Collection<ValidationStatusChangeListener<LocalDate>>
validationStatusListeners = new ArrayList<>();
/**
* This enables the binding to subscribe for the validation status
* change events that are fired by this component and revalidate
* itself respectively.
*/
@Override
public Registration addValidationStatusChangeListener(
ValidationStatusChangeListener<LocalDate> listener) {
validationStatusListeners.add(listener);
return () -> validationStatusListeners.remove(listener);
}
private void fireValidationStatusChangeEvent(
boolean newValidationStatus) {
if (this.clientSideValid != newValidationStatus) {
this.clientSideValid = newValidationStatus;
var event = new ValidationStatusChangeEvent<>(this,
newValidationStatus);
validationStatusListeners.forEach(
listener -> listener.validationStatusChanged(event));
}
}
}
addValidationStatusChangeListener
in interface HasValidator<T>
Binder.BindingBuilderImpl.bind(ValueProvider,
Setter)
protected boolean isEnforcedFieldValidationEnabled()
Copyright © 2025. All rights reserved.