crudui
crudui copied to clipboard
Prevent form from closing if add/edit operation fails with exception
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.
Hi, +1 for this.. this issue is also making us headaches...
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.
Fixed in version 3.9.0 which requires Vaadin 13.0.7. Thanks @ewagasow for the contribution!
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
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.
Hi, any updates on this? I cannot find any solution or workaround
Fixed in version 7.1.2.