Class EasyForm<T>
- Type Parameters:
T- the bean type
- All Implemented Interfaces:
AttachNotifier,DetachNotifier,HasElement,HasSize,HasStyle,Serializable
EasyForm introspects the properties of the given bean type (via getter/setter
conventions) and creates an appropriate Vaadin form field for each one, configures validations
based on JSR-380 (Bean Validation) annotations, and manages data binding through an internal
Binder. Properties without both a getter and a setter, or whose type has no registered
component factory, are ignored.
All customization is programmatic through a fluent API:
EasyForm<Person> form = new EasyForm<>(Person.class);
form.getField("email").withLabel("Email Address").asRequired("Email is required");
form.setSaveAction(person -> personService.save(person));
add(form);
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classFluent configuration wrapper for a single field of anEasyForm. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddBeanValidator(Validator<? super T> validator) Adds a bean-level (cross-field) validator.addButton(String text, ComponentEventListener<ClickEvent<Button>> clickListener) Adds an extra button to the button bar.addButton(String text, Component icon, ButtonVariant variant, ComponentEventListener<ClickEvent<Button>> clickListener) Adds an extra button with an icon and a theme variant to the button bar.voidclear()Clears all fields.<V> EasyForm.EasyFormField<V>Returns the configuration wrapper for the field generated for the given property.<V> EasyForm.EasyFormField<V>Returns the configuration wrapper for the field generated for the given property, typed to the given presentation value type.Validates the form and returns the bean with the current field values written to it.voidhideFields(String... propertyNames) Hides the given fields, excluding them from the layout and the binding.voidPopulates the fields with values from the given bean without live binding.voidreadOnlyFields(String... propertyNames) Makes the given fields read-only.voidreset()Resets the fields to the values of the last bean set throughsetBean(Object)orreadBean(Object).voidBinds the given bean to the form in edit mode: fields are populated from the bean and valid value changes are written through to it.voidsetCancelAction(SerializableRunnable cancelAction) Sets the action invoked when the cancel button is clicked, and makes the cancel button visible.voidsetCancelButtonText(String text) Sets the text of the cancel button.voidsetCancelButtonVisible(boolean visible) Overrides the visibility of the cancel button.<V,P> void setComponentFactory(Class<P> propertyType, SerializableSupplier<HasValue<?, V>> factory, Converter<V, P> converter) Registers a component factory for the given property type in this form instance, together with a converter that adapts the component presentation type to the property type, overriding the global defaults.<V> voidsetComponentFactory(Class<V> type, SerializableSupplier<HasValue<?, V>> factory) Registers a component factory for the given value type in this form instance, overriding the global defaults.static <V,P> void setDefaultComponentFactory(Class<P> propertyType, SerializableSupplier<HasValue<?, V>> factory, Converter<V, P> converter) Registers a global default component factory for the given property type, together with a converter that adapts the component presentation type to the property type (e.g. aNumberFieldwhoseDoublevalue is converted to aLongproperty).static <V> voidsetDefaultComponentFactory(Class<V> type, SerializableSupplier<HasValue<?, V>> factory) Registers a global default component factory for the given value type.voidsetFieldOrder(String... propertyNames) Sets the display order of the fields.voidsetResponsiveSteps(FormLayout.ResponsiveStep... steps) Configures the responsive steps of the internal form layout.voidsetSaveAction(SerializableConsumer<T> saveAction) Sets the action invoked with the validated bean when the save button is clicked, and makes the save button visible.voidsetSaveButtonText(String text) Sets the text of the save button.voidsetSaveButtonVisible(boolean visible) Overrides the visibility of the save button.Methods inherited from class com.vaadin.flow.component.Composite
getChildren, getContent, getElement, initContentMethods inherited from class com.vaadin.flow.component.Component
addListener, findAncestor, fireEvent, from, get, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, 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.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods 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, setClassName
-
Constructor Details
-
EasyForm
Creates a form whose fields are generated from the properties of the given bean type.- Parameters:
beanType- the bean type to generate the form for, notnull- Throws:
NullPointerException- ifbeanTypeisnullIllegalArgumentException- ifbeanTypecannot be introspected, or if a registered component factory supplies a value that is not aComponent
-
-
Method Details
-
setDefaultComponentFactory
public static <V> void setDefaultComponentFactory(Class<V> type, SerializableSupplier<HasValue<?, V>> factory) Registers a global default component factory for the given value type. The factory applies to allEasyForminstances created after this call, unless overridden per form instance or per property. The supplied component must be aComponent.The built-in type mappings (e.g.
StringtoTextField) are registered through this same registry and can be replaced by calling this method.- Type Parameters:
V- the value type- Parameters:
type- the value type to register the factory for, notnullfactory- the factory that creates a component for the type, notnull- Throws:
NullPointerException- iftypeorfactoryisnull
-
setDefaultComponentFactory
public static <V,P> void setDefaultComponentFactory(Class<P> propertyType, SerializableSupplier<HasValue<?, V>> factory, Converter<V, P> converter) Registers a global default component factory for the given property type, together with a converter that adapts the component presentation type to the property type (e.g. aNumberFieldwhoseDoublevalue is converted to aLongproperty). The factory applies to allEasyForminstances created after this call, unless overridden per form instance or per property. The supplied component must be aComponent.- Type Parameters:
V- the presentation value type of the created componentsP- the property type- Parameters:
propertyType- the property type to register the factory for, notnullfactory- the factory that creates a component for the type, notnullconverter- the converter from the presentation type to the property type, notnull- Throws:
NullPointerException- ifpropertyType,factoryorconverterisnull
-
setComponentFactory
Registers a component factory for the given value type in this form instance, overriding the global defaults. Fields already generated for properties of this type are recreated, unless a custom component was set for them viaEasyForm.EasyFormField.withComponent(HasValue). The supplied component must be aComponent.- Type Parameters:
V- the value type- Parameters:
type- the value type to register the factory for, notnullfactory- the factory that creates a component for the type, notnull- Throws:
NullPointerException- iftypeorfactoryisnullIllegalArgumentException- if the factory supplies a value that is not aComponent
-
setComponentFactory
public <V,P> void setComponentFactory(Class<P> propertyType, SerializableSupplier<HasValue<?, V>> factory, Converter<V, P> converter) Registers a component factory for the given property type in this form instance, together with a converter that adapts the component presentation type to the property type, overriding the global defaults. Fields already generated for properties of this type are recreated, unless a custom component was set for them viaEasyForm.EasyFormField.withComponent(HasValue). The supplied component must be aComponent.- Type Parameters:
V- the presentation value type of the created componentsP- the property type- Parameters:
propertyType- the property type to register the factory for, notnullfactory- the factory that creates a component for the type, notnullconverter- the converter from the presentation type to the property type, notnull- Throws:
NullPointerException- ifpropertyType,factoryorconverterisnullIllegalArgumentException- if the factory supplies a value that is not aComponent
-
getField
Returns the configuration wrapper for the field generated for the given property.The presentation value type cannot be inferred from the property name, so it is taken from the assignment target and resolves to
Objectwhen the result is used directly in a fluent chain. That is harmless for the state and presentation methods, but the methods that take the value type as a parameter —EasyForm.EasyFormField.withValidator(Validator)andEasyForm.EasyFormField.withConverter(Converter)— then reject any argument that is not typed toObject. UsegetField(String, Class)in that case.- Type Parameters:
V- the presentation value type of the field- Parameters:
propertyName- the name of the bean property- Returns:
- the field wrapper
- Throws:
IllegalArgumentException- if no property with the given name was discovered
-
getField
Returns the configuration wrapper for the field generated for the given property, typed to the given presentation value type. The type is inferred from the argument, so the wrapper can be configured in a fluent chain without an explicit type argument:form.getField("email", String.class).withValidator(new EmailValidator("Invalid email"));The given type is checked against the value type of the component currently generated for the property, which is the type the field's validators and converters see. Note that this is the presentation type and not necessarily the property type: a
Longproperty bound through the built-inNumberFieldfactory has a presentation type ofDouble. The check is skipped for properties without a component, and for components whose value type cannot be resolved (such asComboBox).Because the check reflects the component in place at the time of the call, replacing the component for a property is done through
getField(String)andEasyForm.EasyFormField.withComponent(HasValue), which types the returned wrapper after the new component.- Type Parameters:
V- the presentation value type of the field- Parameters:
propertyName- the name of the bean propertyvalueType- the expected presentation value type, notnull- Returns:
- the field wrapper
- Throws:
NullPointerException- ifvalueTypeisnullIllegalArgumentException- if no property with the given name was discovered, or if the component of the property does not have the given presentation value type
-
setFieldOrder
Sets the display order of the fields. Only the listed properties are shown (and included in the binding), in the given order. Repeated names are ignored after their first occurrence.- Parameters:
propertyNames- the names of the properties to display, in order- Throws:
NullPointerException- if the array or any of its elements isnullIllegalArgumentException- if any property name is unknown
-
hideFields
Hides the given fields, excluding them from the layout and the binding.- Parameters:
propertyNames- the names of the properties to hide- Throws:
IllegalArgumentException- if any property name is unknown
-
readOnlyFields
Makes the given fields read-only. Read-only fields are displayed and populated from the bean but cannot be edited.- Parameters:
propertyNames- the names of the properties to make read-only- Throws:
IllegalArgumentException- if any property name is unknown
-
setBean
Binds the given bean to the form in edit mode: fields are populated from the bean and valid value changes are written through to it.- Parameters:
bean- the bean to edit, ornullto clear the form- Throws:
IllegalStateException- if a field has a component whose value type cannot be written to its property and no converter was set for it
-
readBean
Populates the fields with values from the given bean without live binding. Changes are not written to the bean untilgetValidBean()or the save action runs.- Parameters:
bean- the bean to read values from, ornullto clear the form- Throws:
IllegalStateException- if a field has a component whose value type cannot be written to its property and no converter was set for it
-
getValidBean
Validates the form and returns the bean with the current field values written to it. If no bean has been set, a new instance is created (the bean type must have an accessible no-args constructor in that case).- Returns:
- the populated bean, or an empty optional if validation failed
- Throws:
IllegalStateException- if no bean has been set and the bean type cannot be instantiated, or if a field has a component whose value type cannot be written to its property and no converter was set for it
-
reset
public void reset()Resets the fields to the values of the last bean set throughsetBean(Object)orreadBean(Object). If no bean was set, all fields are cleared. -
clear
public void clear()Clears all fields. The last bean set is remembered and can be restored withreset(). -
addBeanValidator
Adds a bean-level (cross-field) validator. Bean validators run after all field-level validators have passed.- Parameters:
validator- the bean validator to add, notnull- Throws:
NullPointerException- ifvalidatorisnull
-
setSaveAction
Sets the action invoked with the validated bean when the save button is clicked, and makes the save button visible.- Parameters:
saveAction- the save action, ornullto remove it
-
setCancelAction
Sets the action invoked when the cancel button is clicked, and makes the cancel button visible.- Parameters:
cancelAction- the cancel action, ornullto remove it
-
setSaveButtonText
Sets the text of the save button.- Parameters:
text- the button text
-
setCancelButtonText
Sets the text of the cancel button.- Parameters:
text- the button text
-
setSaveButtonVisible
public void setSaveButtonVisible(boolean visible) Overrides the visibility of the save button. By default the button is visible if and only if a save action has been set.- Parameters:
visible- whether the save button is visible
-
setCancelButtonVisible
public void setCancelButtonVisible(boolean visible) Overrides the visibility of the cancel button. By default the button is visible if and only if a cancel action has been set.- Parameters:
visible- whether the cancel button is visible
-
addButton
Adds an extra button to the button bar.- Parameters:
text- the button textclickListener- the click listener- Returns:
- the added button
-
addButton
public Button addButton(String text, Component icon, ButtonVariant variant, ComponentEventListener<ClickEvent<Button>> clickListener) Adds an extra button with an icon and a theme variant to the button bar.- Parameters:
text- the button texticon- the button iconvariant- the theme variant to applyclickListener- the click listener- Returns:
- the added button
-
setResponsiveSteps
Configures the responsive steps of the internal form layout.- Parameters:
steps- the responsive steps- See Also:
-