Class BeanValidationForm<T>
- All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasStyle, Serializable
This version uses FormBinder (published in Viritin 2.8) instead of the basic Vaadin Binder. This might already be better, but most likely there are regressions and edge case that may be incompatible with the old AbstractForm, thus bringing in this with a different name for testing. Once tested enough and if no big regressions, AbstractForm becomes this and possible a backwards compatibility version is created for "core Binder version".
In the default configuration BeanValidationForm expects that fields are lazily pushing their value change events to the server. This way it can automatically adjust e.g. save/cancel buttons based on the state/validity. Either use Viritin fields like VTextField or configure e.g. with setValueChangeMode.
- Author:
- mstahv
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceDeprecated.A handler called when the built-in delete button of the form is called.static interfaceDeprecated.A handler called when the built-in reset/cancel button of the form is called.static interfaceDeprecated.A handler called when the built-in save button of the form is called. -
Constructor Summary
ConstructorsConstructorDescriptionBeanValidationForm(Class<T> entityType) Deprecated.Constructor for the abstract form. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidDeprecated.Adjusts the reset button state.protected voidDeprecated.Adjust save button state.Deprecated.Sizes this form as a part of a page rather than as the page.protected voidbind()Deprecated.voidDeprecated.protected com.vaadin.flow.component.ComponentDeprecated.This method should return the actual content of the form, including possible toolbar.protected com.vaadin.flow.component.button.ButtonDeprecated.protected com.vaadin.flow.component.button.ButtonDeprecated.protected com.vaadin.flow.component.button.ButtonDeprecated.protected voiddelete(com.vaadin.flow.component.ClickEvent<com.vaadin.flow.component.button.Button> e) Deprecated.protected <T> Set<jakarta.validation.ConstraintViolation<T>> doBeanValidation(T object) Deprecated.voidDeprecated.Focuses the first field found from the form.Deprecated.Deprecated.com.vaadin.flow.component.ComponentDeprecated.com.vaadin.flow.component.button.ButtonDeprecated.Deprecated.Deprecated.Deprecated.Deprecated.protected List<com.vaadin.flow.component.Component> Deprecated.Return the list of field components the defaultcreateContent()stacks into the form body, in order.protected com.vaadin.flow.component.formlayout.FormLayoutDeprecated.Deprecated.com.vaadin.flow.component.dialog.DialoggetPopup()Deprecated.com.vaadin.flow.component.button.ButtonDeprecated.Deprecated.com.vaadin.flow.component.button.ButtonDeprecated.Deprecated.Deprecated.com.vaadin.flow.component.orderedlayout.HorizontalLayoutDeprecated.Class<?>[]Deprecated.protected jakarta.validation.ValidatorDeprecated.The validator this form checks its entity with.booleanDeprecated.protected booleanisBound()Deprecated.booleanisValid()Deprecated.protected voidlazyInit()Deprecated.Deprecated.protected voidreset(com.vaadin.flow.component.ClickEvent<com.vaadin.flow.component.button.Button> e) Deprecated.protected voidsave(com.vaadin.flow.component.ClickEvent<com.vaadin.flow.component.button.Button> e) Deprecated.voidsetCancelCaption(String cancelCaption) Deprecated.voidsetClassLevelViolationsDisplay(com.vaadin.flow.component.HasComponents classLevelViolationsDisplay) Deprecated.voidsetDeleteButton(com.vaadin.flow.component.button.Button deleteButton) Deprecated.voidsetDeleteCaption(String deleteCaption) Deprecated.voidsetDeleteHandler(BeanValidationForm.DeleteHandler<T> deleteHandler) Deprecated.voidsetEagerSavedHandler(BeanValidationForm.SavedHandler<T> eagerSavedHandler) Deprecated.Saves as the reader types, without a save button.voidDeprecated.Sets the object to be edited by this form.voidsetEntityWithEnabledSave(T entity) Deprecated.by default only save button get's enabled when form has any changes
you can use this method in case the prefilled entity is already valid and save should be possible to press without any changes
if entity is not valid saveButton will stay disabled!protected voidsetHasChanges(boolean hasChanges) Deprecated.voidsetModalWindowTitle(String modalWindowTitle) Deprecated.voidsetResetButton(com.vaadin.flow.component.button.Button resetButton) Deprecated.voidsetResetHandler(BeanValidationForm.ResetHandler<T> resetHandler) Deprecated.voidsetSaveButton(com.vaadin.flow.component.button.Button button) Deprecated.voidsetSaveCaption(String saveCaption) Deprecated.voidsetSavedHandler(BeanValidationForm.SavedHandler<T> savedHandler) Deprecated.voidsetSaveOnEnter(boolean saveOnEnter) Deprecated.Controls whether the save button created by this form submits on ENTER.voidsetValidationGroups(Class<?>... groups) Deprecated.Methods inherited from class com.vaadin.flow.component.Composite
getChildren, getContent, getElement, initContentMethods inherited from class com.vaadin.flow.component.Component
addListener, bindVisible, findAncestor, fireEvent, from, get, getEventBus, getId, getListeners, getLocale, getParent, getTestId, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setTestId, setVisibleMethods inherited from class 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.HasStyle
addClassName, addClassNames, bindClassName, bindClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
-
Constructor Details
-
BeanValidationForm
-
-
Method Details
-
asSection
Deprecated.Sizes this form as a part of a page rather than as the page.The composition root is full size by default, which suits a form that has a view or a modal to itself. Embedded anywhere else the default is a trap with two different faces: as a section of a page a full-height form pushes everything below it off the screen, and inside a popover — which sizes itself by its content — a full-height child is a child with no height at all. This call gives the root full width and content-driven height instead.
- Returns:
- the form, for further configuration
-
setSaveOnEnter
public void setSaveOnEnter(boolean saveOnEnter) Deprecated.Controls whether the save button created by this form submits on ENTER.On by default, and right for a form that has the view to itself. With two bound forms in one view — or one popover — one keypress would perform two saves, so the form that is not the main verdict of the page switches its shortcut off. Affects the
DefaultButtonthatcreateSaveButton()creates; a save button set explicitly withsetSaveButton(Button)is managed by whoever created it.- Parameters:
saveOnEnter- true to submit this form on ENTER
-
setEntityWithEnabledSave
Deprecated.by default only save button get's enabled when form has any changes
you can use this method in case the prefilled entity is already valid and save should be possible to press without any changes
if entity is not valid saveButton will stay disabled!- Parameters:
entity- the object to be edited by this form
-
hasChanges
public boolean hasChanges()Deprecated.- Returns:
- true if bean has been changed since last setEntity call.
-
setHasChanges
protected void setHasChanges(boolean hasChanges) Deprecated. -
isValid
Deprecated. -
getResetHandler
Deprecated. -
setResetHandler
Deprecated. -
getSavedHandler
Deprecated. -
setSavedHandler
Deprecated. -
getEagerSavedHandler
Deprecated. -
setEagerSavedHandler
Deprecated.Saves as the reader types, without a save button.For a form that is a row in a list or a panel of settings, where a save button of its own would be one button too many. The handler is called after every change the reader makes that leaves the form valid; a change that does not is shown on the field and not saved, and what was stored stays as it was.
Shows no button and does not need
setSavedHandler(BeanValidationForm.SavedHandler), which is the other, deliberate way to save. A form may have both, though it is worth asking why.- Parameters:
eagerSavedHandler- called with the entity after each valid change, or null to stop
-
getDeleteHandler
Deprecated. -
setDeleteHandler
Deprecated. -
getSaveCaption
Deprecated. -
setSaveCaption
Deprecated. -
getModalWindowTitle
Deprecated. -
setModalWindowTitle
Deprecated. -
getDeleteCaption
Deprecated. -
setDeleteCaption
Deprecated. -
getCancelCaption
Deprecated. -
setCancelCaption
Deprecated. -
getBinder
Deprecated. -
lazyInit
protected void lazyInit()Deprecated. -
bind
protected void bind()Deprecated. -
getValidationGroups
Deprecated. -
setValidationGroups
Deprecated. -
doBeanValidation
Deprecated. -
getValidator
protected jakarta.validation.Validator getValidator()Deprecated.The validator this form checks its entity with.Built here from the default provider, with the locale taken from the UI rather than from the JVM. That is enough while the constraints are self-contained.
It stops being enough as soon as a
ConstraintValidatorneeds something from the application — a repository to ask whether an identifier is still free, a registry of what exists. A validator built from the default provider is instantiated by reflection, so its injection points stay null and the check fails withHV000028instead of answering. Message templates are read fromValidationMessages.propertiesfor the same reason, rather than from an application's own message source.In a Spring application both are already solved by the container's validator, and giving it to the form is two lines:
public PersonForm(jakarta.validation.Validator validator) { super(Person.class); this.validator = validator; } @Override protected Validator getValidator() { return validator; }There is deliberately no Spring aware version of this class: one overridable method costs less than a second artifact to keep in step. SeeSpringManagedValidatorTestin the test sources for the whole example, including what the failure looks like without it.- Returns:
- the validator, built on first use
-
createContent
protected com.vaadin.flow.component.Component createContent()Deprecated.This method should return the actual content of the form, including possible toolbar.Use setEntity(T entity) to fill in the data. Am example implementation could look like this:
public class PersonForm extends AbstractForm<Person> { private TextField firstName = new MTextField("First Name"); private TextField lastName = new MTextField("Last Name"); @Override protected Component createContent() { return new MVerticalLayout( new FormLayout( firstName, lastName ), getToolbar() ); } }- Returns:
- the content of the form
-
getFormLayout
protected com.vaadin.flow.component.formlayout.FormLayout getFormLayout()Deprecated. -
getClassLevelViolationsDisplay
public com.vaadin.flow.component.Component getClassLevelViolationsDisplay()Deprecated. -
setClassLevelViolationsDisplay
public void setClassLevelViolationsDisplay(com.vaadin.flow.component.HasComponents classLevelViolationsDisplay) Deprecated. -
getFormComponents
Deprecated.Return the list of field components the defaultcreateContent()stacks into the form body, in order.Only consulted by that default. A form that overrides
createContent()to lay itself out never causes this to be called — which is why it is no longer abstract: forcing every such form to declare an empty list said nothing except that the form declines a default it does not use.- Returns:
- the fields displayed by the default createContent(), empty by default
-
adjustSaveButtonState
protected void adjustSaveButtonState()Deprecated.Adjust save button state. Override if you for example want to have Save button always enabled, even if the Binder has not tracked any changes yet. -
getSaveButton
public com.vaadin.flow.component.button.Button getSaveButton()Deprecated. -
setSaveButton
public void setSaveButton(com.vaadin.flow.component.button.Button button) Deprecated. -
createSaveButton
protected com.vaadin.flow.component.button.Button createSaveButton()Deprecated. -
isBound
protected boolean isBound()Deprecated. -
createResetButton
protected com.vaadin.flow.component.button.Button createResetButton()Deprecated. -
getResetButton
public com.vaadin.flow.component.button.Button getResetButton()Deprecated. -
setResetButton
public void setResetButton(com.vaadin.flow.component.button.Button resetButton) Deprecated. -
createDeleteButton
protected com.vaadin.flow.component.button.Button createDeleteButton()Deprecated. -
getDeleteButton
public com.vaadin.flow.component.button.Button getDeleteButton()Deprecated. -
setDeleteButton
public void setDeleteButton(com.vaadin.flow.component.button.Button deleteButton) Deprecated. -
adjustResetButtonState
protected void adjustResetButtonState()Deprecated.Adjusts the reset button state. Override if you for example wish to keep reset/cancel button enabled even if there is nothing to reset. -
getEntity
Deprecated.- Returns:
- the currently edited entity or null if the form is currently unbound
-
setEntity
Deprecated.Sets the object to be edited by this form. This method binds all fields from this form to given objects.If your form needs to manually configure something based on the state of the edited object, you can override this method to do that either before the object is bound to fields or to do something after the bean binding.
- Parameters:
entity- the object to be edited by this form
-
save
protected void save(com.vaadin.flow.component.ClickEvent<com.vaadin.flow.component.button.Button> e) Deprecated. -
reset
protected void reset(com.vaadin.flow.component.ClickEvent<com.vaadin.flow.component.button.Button> e) Deprecated. -
delete
protected void delete(com.vaadin.flow.component.ClickEvent<com.vaadin.flow.component.button.Button> e) Deprecated. -
getToolbar
public com.vaadin.flow.component.orderedlayout.HorizontalLayout getToolbar()Deprecated.- Returns:
- A default toolbar containing save/cancel/delete buttons
-
openInModalPopup
Deprecated. -
focusFirst
public void focusFirst()Deprecated.Focuses the first field found from the form. It often improves UX to call this method, or focus another field, when you assign a bean for editing. -
getPopup
public com.vaadin.flow.component.dialog.Dialog getPopup()Deprecated.- Returns:
- the last Popup into which the Form was opened with #openInModalPopup method or null if the form hasn't been use in window
-
closePopup
public void closePopup()Deprecated.
-