crudui icon indicating copy to clipboard operation
crudui copied to clipboard

Prevent form from closing if add/edit operation fails with exception

Open ewagasow opened this issue 5 years ago • 5 comments

When you have an JPA entity with a unique constraint and type in an already in use value, the whole form closes before an error pop up is shown. This way you lose all you typed in data. Instead it is better when the form remains, so you do not need to reenter all of your data again.

ewagasow avatar Apr 18 '19 09:04 ewagasow

Hi, +1 for this.. this issue is also making us headaches...

JulianFeinauer avatar Apr 24 '19 09:04 JulianFeinauer

Same problem here. I tried resolving it by adding a custom validator to the underlying binder. My custom formfactory looks like

import java.util.List;
import org.vaadin.crudui.crud.CrudOperation;
import org.vaadin.crudui.form.impl.form.factory.DefaultCrudFormFactory;
import com.vaadin.flow.component.HasValueAndElement;
import com.vaadin.flow.data.binder.Binder;

public abstract class BindingCrudFormFactory<T> extends DefaultCrudFormFactory<T> {

  public BindingCrudFormFactory(Class<T> domainType) {
    super(domainType);
  }

  @Override
  protected List<HasValueAndElement> buildFields(CrudOperation operation, T domainObject,
      boolean readOnly) {
    final List<HasValueAndElement> fields = super.buildFields(operation, domainObject, readOnly);
    binder.withValidator(bean -> validateUniqueness(bean), "Item has to be unique");
    return fields;
  }

   /**
   * Validates bean. If returning false the dialog will not be closed.
   * @param bean
   * @return
   */
  protected abstract boolean validateUniqueness(T bean);

}

The implementation of validateUniqueness() depends on the Beantype. I'm using it by calling the db with a name-property:

 BindingCrudFormFactory<Specification> formFactory =
        new BindingCrudFormFactory<Specification>(Specification.class) {

          @Override
          protected boolean validateUniqueness(Specification bean) {
            if (dao.getByName(bean.getName()).isPresent()) {
              Notification.show("Namefield needs to be unique");
              return false;
            }
            return true;
          }
        };
    crud.setCrudFormFactory(formFactory);

The dialog will not be closed and a notification is shown, when the bean is not unique.

I'm not entirely happy with this solution. It would be better to have a validator on field-level which shows the error message on the field like the required messages. I tried to add one with a FieldCreationListener but the validator never get called. I assume it is somehow overridden by ones generated by crud-ui.

watho avatar May 08 '19 08:05 watho

Fixed in version 3.9.0 which requires Vaadin 13.0.7. Thanks @ewagasow for the contribution!

alejandro-du avatar May 22 '19 13:05 alejandro-du

Hi! Is this issue fixed? If during an update operation performed on the entity a new CrudOperationException is thrown, the error notification appears but the form (dialog window) is indeed closed.

Thanks

ruizrube avatar Oct 17 '22 09:10 ruizrube

I was able to reproduce the issue. In case someone wants to take this one, what's happening is that the Grid is refreshed after an error in order to prevent invalid data from being shown in the Grid later after clicking cancel. The fix should check both scenarios.

alejandro-du avatar Oct 17 '22 17:10 alejandro-du

Hi, any updates on this? I cannot find any solution or workaround

TheLust avatar Jun 01 '24 14:06 TheLust

Fixed in version 7.1.2.

alejandro-du avatar Jul 02 '24 13:07 alejandro-du