From 4da5f803bfb0aca451aaace5165cb55bd4d13850 Mon Sep 17 00:00:00 2001 From: Tatu Lund Date: Fri, 17 Apr 2020 10:12:46 +0300 Subject: [PATCH] Add methods to control validation - Enable / disable all validators on Binder level - Enable / disable validators on Binding level - add writeBeanAsDraft(bean,boolean) for writing draft bean with validators disabled Fixes: https://github.com/vaadin/flow/issues/5030 --- .../com/vaadin/flow/data/binder/Binder.java | 112 ++++++++++++++++-- 1 file changed, 105 insertions(+), 7 deletions(-) diff --git a/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java b/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java index 9b42f81c780..d85bf3c6fde 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java @@ -235,6 +235,21 @@ default BindingValidationStatus validate() { * {@code true} otherwise (default) */ public boolean isAsRequiredEnabled(); + + /** + * Define whether validators are disabled or enabled for this + * specific binding. + * + * @param validatorsDisabled A boolean value + */ + public void setValidatorsDisabled(boolean validatorsDisabled); + + /** + * Returns if validators are currently disabled or not + * + * @return A boolean value + */ + public boolean isValidatorsDisabled(); } /** @@ -754,6 +769,7 @@ protected static class BindingBuilderImpl private final HasValue field; private BindingValidationStatusHandler statusHandler; private boolean isStatusHandlerChanged; + private Binding binding; private boolean bound; @@ -815,6 +831,7 @@ public Binding bind(ValueProvider getter, if (getBinder().incompleteBindings != null) { getBinder().incompleteBindings.remove(getField()); } + this.binding = binding; return binding; } @@ -849,6 +866,7 @@ public Binding bind(String propertyName) { Binding binding = ((BindingBuilder) finalBinding).bind(getter, setter); getBinder().boundProperties.put(propertyName, binding); + this.binding = binding; return binding; } finally { if (getBinder().incompleteMemberFieldBindings != null) { @@ -872,8 +890,17 @@ public BindingBuilder withValidator( checkUnbound(); Objects.requireNonNull(validator, "validator cannot be null"); + Validator wrappedValidator = ((value, context) -> { + if (getBinder().isValidatorsDisabled() || + (binding != null && binding.isValidatorsDisabled())) { + return ValidationResult.ok(); + } else { + return validator.apply(value, context); + } + }); + converterValidatorChain = ((Converter) converterValidatorChain) - .chain(new ValidatorAsConverter<>(validator)); + .chain(new ValidatorAsConverter<>(wrappedValidator)); return this; } @@ -1026,6 +1053,8 @@ protected static class BindingImpl private final boolean asRequiredSet; + private boolean validatorsDisabled = false; + public BindingImpl(BindingBuilderImpl builder, ValueProvider getter, Setter setter) { @@ -1306,6 +1335,16 @@ public void setAsRequiredEnabled(boolean asRequiredEnabled) { public boolean isAsRequiredEnabled() { return field.isRequiredIndicatorVisible(); } + + @Override + public void setValidatorsDisabled(boolean validatorsDisabled) { + this.validatorsDisabled = validatorsDisabled; + } + + @Override + public boolean isValidatorsDisabled() { + return validatorsDisabled; + } } /** @@ -1412,6 +1451,8 @@ void setIdentity() { private Set> changedBindings = new LinkedHashSet<>(); + private boolean validatorsDisabled = false; + /** * Creates a binder using a custom {@link PropertySet} implementation for * finding and resolving property names for @@ -1831,7 +1872,27 @@ public void writeBean(BEAN bean) throws ValidationException { * {@code null} */ public void writeBeanAsDraft(BEAN bean) { - doWriteDraft(bean, new ArrayList<>(bindings)); + doWriteDraft(bean, new ArrayList<>(bindings),false); + } + + /** + * Writes successfully converted changes from the bound fields bypassing + * all the Validation or all fields passing conversion if forced = true. + * If the conversion fails, the value written to the bean will be null. + * + * @see #writeBean(Object) + * @see #writeBeanIfValid(Object) + * @see #readBean(Object) + * @see #setBean(Object) + * + * @param bean + * the object to which to write the field values, not + * {@code null} + * @param forced + * disable all Validators during write + */ + public void writeBeanAsDraft(BEAN bean, boolean forced) { + doWriteDraft(bean, new ArrayList<>(bindings),true); } /** @@ -1929,14 +1990,24 @@ private BinderValidationStatus doWriteIfValid(BEAN bean, * the bean to write field values into * @param bindings * the set of bindings to write to the bean + * @param forced + * disable validators during write if true */ @SuppressWarnings({ "unchecked" }) - private void doWriteDraft(BEAN bean, - Collection> bindings) { + private void doWriteDraft(BEAN bean, + Collection> bindings, boolean forced) { Objects.requireNonNull(bean, "bean cannot be null"); - bindings.forEach(binding -> ((BindingImpl) binding) - .writeFieldValue(bean)); + if (!forced) { + bindings.forEach(binding -> ((BindingImpl) binding) + .writeFieldValue(bean)); + } else { + boolean isDisabled = isValidatorsDisabled(); + setValidatorsDisabled(true); + bindings.forEach(binding -> ((BindingImpl) binding) + .writeFieldValue(bean)); + setValidatorsDisabled(isDisabled); + } } /** @@ -1998,7 +2069,14 @@ protected void restoreBeanState(BEAN bean, */ public Binder withValidator(Validator validator) { Objects.requireNonNull(validator, "validator cannot be null"); - validators.add(validator); + Validator wrappedValidator = ((value, context) -> { + if (isValidatorsDisabled()) { + return ValidationResult.ok(); + } else { + return validator.apply(value, context); + } + }); + validators.add(wrappedValidator); return this; } @@ -2986,4 +3064,24 @@ public void removeBinding(String propertyName) { Optional.ofNullable(boundProperties.get(propertyName)) .ifPresent(Binding::unbind); } + + /** + * Control whether validators including bean level validators are + * disabled or enabled globally for this Binder. + * + * @param validatorsDisabled Boolean value + */ + public void setValidatorsDisabled(boolean validatorsDisabled) { + this.validatorsDisabled = validatorsDisabled; + } + + /** + * Returns if the validators including bean level validators + * are disabled or enabled for this Binder. + * + * @return Boolean value + */ + public boolean isValidatorsDisabled() { + return validatorsDisabled; + } }