Fixes vaadin/framework8-issues#414 Change-Id: I2d4e101f29542fadfdd30ce63455a0e078e0e735tags/8.0.0.alpha6
@@ -619,6 +619,26 @@ public class Binder<BEAN> implements Serializable { | |||
return status; | |||
} | |||
/** | |||
* Returns the field value run through all converters and validators, | |||
* but doesn't pass the {@link ValidationStatus} to any status handler. | |||
* | |||
* @return the conversation result | |||
*/ | |||
private Result<TARGET> doConversation() { | |||
FIELDVALUE fieldValue = field.getValue(); | |||
return converterValidatorChain.convertToModel(fieldValue, | |||
createValueContext()); | |||
} | |||
private ValidationStatus<TARGET> toValidationStatus( | |||
Result<TARGET> result) { | |||
return new ValidationStatus<>(this, | |||
result.isError() | |||
? ValidationResult.error(result.getMessage().get()) | |||
: ValidationResult.ok()); | |||
} | |||
/** | |||
* Returns the field value run through all converters and validators, | |||
* but doesn't pass the {@link ValidationStatus} to any status handler. | |||
@@ -626,10 +646,7 @@ public class Binder<BEAN> implements Serializable { | |||
* @return the validation status | |||
*/ | |||
private ValidationStatus<TARGET> doValidation() { | |||
FIELDVALUE fieldValue = field.getValue(); | |||
Result<TARGET> dataValue = converterValidatorChain | |||
.convertToModel(fieldValue, createValueContext()); | |||
return new ValidationStatus<>(this, dataValue); | |||
return toValidationStatus(doConversation()); | |||
} | |||
/** | |||
@@ -677,7 +694,7 @@ public class Binder<BEAN> implements Serializable { | |||
// store field value if valid | |||
ValidationStatus<TARGET> fieldValidationStatus = writeFieldValue( | |||
bean); | |||
List<Result<?>> binderValidationResults; | |||
List<ValidationResult> binderValidationResults; | |||
// if all field level validations pass, run bean level validation | |||
if (!getBinder().bindings.stream().map(BindingImpl::doValidation) | |||
.anyMatch(ValidationStatus::isError)) { | |||
@@ -702,12 +719,11 @@ public class Binder<BEAN> implements Serializable { | |||
private ValidationStatus<TARGET> writeFieldValue(BEAN bean) { | |||
assert bean != null; | |||
ValidationStatus<TARGET> validationStatus = doValidation(); | |||
Result<TARGET> result = doConversation(); | |||
if (setter != null) { | |||
validationStatus.getResult().ifPresent(result -> result | |||
.ifOk(value -> setter.accept(bean, value))); | |||
result.ifOk(value -> setter.accept(bean, value)); | |||
} | |||
return validationStatus; | |||
return toValidationStatus(result); | |||
} | |||
private void notifyStatusHandler(ValidationStatus<?> status) { | |||
@@ -740,10 +756,9 @@ public class Binder<BEAN> implements Serializable { | |||
@Override | |||
public Result<T> convertToModel(T value, ValueContext context) { | |||
Result<? super T> validationResult = validator.apply(value, | |||
context); | |||
ValidationResult validationResult = validator.apply(value, context); | |||
if (validationResult.isError()) { | |||
return Result.error(validationResult.getMessage().get()); | |||
return Result.error(validationResult.getErrorMessage()); | |||
} else { | |||
return Result.ok(value); | |||
} | |||
@@ -1069,9 +1084,9 @@ public class Binder<BEAN> implements Serializable { | |||
bindings.forEach(binding -> binding.writeFieldValue(bean)); | |||
// Now run bean level validation against the updated bean | |||
List<Result<?>> binderResults = validateBean(bean); | |||
boolean hasErrors = binderResults.stream().filter(Result::isError) | |||
.findAny().isPresent(); | |||
List<ValidationResult> binderResults = validateBean(bean); | |||
boolean hasErrors = binderResults.stream() | |||
.filter(ValidationResult::isError).findAny().isPresent(); | |||
if (hasErrors) { | |||
// Bean validator failed, revert values | |||
bindings.forEach((BindingImpl binding) -> binding.setter | |||
@@ -1216,9 +1231,9 @@ public class Binder<BEAN> implements Serializable { | |||
* @return a list of validation errors or an empty list if validation | |||
* succeeded | |||
*/ | |||
private List<Result<?>> validateBean(BEAN bean) { | |||
private List<ValidationResult> validateBean(BEAN bean) { | |||
Objects.requireNonNull(bean, "bean cannot be null"); | |||
List<Result<?>> results = Collections.unmodifiableList(validators | |||
List<ValidationResult> results = Collections.unmodifiableList(validators | |||
.stream() | |||
.map(validator -> validator.apply(bean, new ValueContext())) | |||
.collect(Collectors.toList())); | |||
@@ -1444,7 +1459,7 @@ public class Binder<BEAN> implements Serializable { | |||
// show first possible error or OK status in the label if set | |||
if (getStatusLabel().isPresent()) { | |||
String statusMessage = binderStatus.getBeanValidationErrors() | |||
.stream().findFirst().flatMap(Result::getMessage) | |||
.stream().findFirst().map(ValidationResult::getErrorMessage) | |||
.orElse(""); | |||
getStatusLabel().get().setValue(statusMessage); | |||
} |
@@ -53,7 +53,7 @@ public class BinderValidationStatus<BEAN> implements Serializable { | |||
private final Binder<BEAN> binder; | |||
private final List<ValidationStatus<?>> bindingStatuses; | |||
private final List<Result<?>> binderStatuses; | |||
private final List<ValidationResult> binderStatuses; | |||
/** | |||
* Convenience method for creating a unresolved validation status for the | |||
@@ -90,7 +90,7 @@ public class BinderValidationStatus<BEAN> implements Serializable { | |||
*/ | |||
public BinderValidationStatus(Binder<BEAN> source, | |||
List<ValidationStatus<?>> bindingStatuses, | |||
List<Result<?>> binderStatuses) { | |||
List<ValidationResult> binderStatuses) { | |||
Objects.requireNonNull(binderStatuses, | |||
"binding statuses cannot be null"); | |||
Objects.requireNonNull(binderStatuses, | |||
@@ -116,8 +116,8 @@ public class BinderValidationStatus<BEAN> implements Serializable { | |||
* passed | |||
*/ | |||
public boolean hasErrors() { | |||
return binderStatuses.stream().filter(Result::isError).findAny() | |||
.isPresent() | |||
return binderStatuses.stream().filter(ValidationResult::isError) | |||
.findAny().isPresent() | |||
|| bindingStatuses.stream().filter(ValidationStatus::isError) | |||
.findAny().isPresent(); | |||
} | |||
@@ -136,10 +136,11 @@ public class BinderValidationStatus<BEAN> implements Serializable { | |||
* | |||
* @return a list of all validation errors | |||
*/ | |||
public List<Result<?>> getValidationErrors() { | |||
ArrayList<Result<?>> errors = new ArrayList<>(getFieldValidationErrors() | |||
.stream().map(s -> s.getResult().get()) | |||
.collect(Collectors.toList())); | |||
public List<ValidationResult> getValidationErrors() { | |||
ArrayList<ValidationResult> errors = new ArrayList<>( | |||
getFieldValidationErrors().stream() | |||
.map(s -> s.getResult().get()) | |||
.collect(Collectors.toList())); | |||
errors.addAll(getBeanValidationErrors()); | |||
return errors; | |||
} | |||
@@ -166,7 +167,7 @@ public class BinderValidationStatus<BEAN> implements Serializable { | |||
* | |||
* @return the bean level validation results | |||
*/ | |||
public List<Result<?>> getBeanValidationResults() { | |||
public List<ValidationResult> getBeanValidationResults() { | |||
return binderStatuses; | |||
} | |||
@@ -193,8 +194,8 @@ public class BinderValidationStatus<BEAN> implements Serializable { | |||
* | |||
* @return a list of failed bean level validation results | |||
*/ | |||
public List<Result<?>> getBeanValidationErrors() { | |||
return binderStatuses.stream().filter(Result::isError) | |||
public List<ValidationResult> getBeanValidationErrors() { | |||
return binderStatuses.stream().filter(ValidationResult::isError) | |||
.collect(Collectors.toList()); | |||
} | |||
} |
@@ -24,9 +24,9 @@ import java.util.function.Function; | |||
import java.util.function.Supplier; | |||
/** | |||
* Represents the result of an operation that might fail, such as input | |||
* validation or type conversion. A result may contain either a value, | |||
* signifying a successful operation, or an error message in case of a failure. | |||
* Represents the result of an operation that might fail, such as type | |||
* conversion. A result may contain either a value, signifying a successful | |||
* operation, or an error message in case of a failure. | |||
* <p> | |||
* Result instances are created using the factory methods {@link #ok(R)} and | |||
* {@link #error(String)}, denoting success and failure respectively. |
@@ -34,7 +34,7 @@ import java.util.stream.Collectors; | |||
public class ValidationException extends Exception { | |||
private final List<ValidationStatus<?>> fieldValidationErrors; | |||
private final List<Result<?>> beanValidationErrors; | |||
private final List<ValidationResult> beanValidationErrors; | |||
/** | |||
* Constructs a new exception with validation {@code errors} list. | |||
@@ -45,7 +45,7 @@ public class ValidationException extends Exception { | |||
* binder validation errors list | |||
*/ | |||
public ValidationException(List<ValidationStatus<?>> fieldValidationErrors, | |||
List<Result<?>> beanValidationErrors) { | |||
List<ValidationResult> beanValidationErrors) { | |||
super("Validation has failed for some fields"); | |||
this.fieldValidationErrors = Collections | |||
.unmodifiableList(fieldValidationErrors); | |||
@@ -58,10 +58,11 @@ public class ValidationException extends Exception { | |||
* | |||
* @return a list of all validation errors | |||
*/ | |||
public List<Result<?>> getValidationErrors() { | |||
ArrayList<Result<?>> errors = new ArrayList<>(getFieldValidationErrors() | |||
.stream().map(s -> s.getResult().get()) | |||
.collect(Collectors.toList())); | |||
public List<ValidationResult> getValidationErrors() { | |||
ArrayList<ValidationResult> errors = new ArrayList<>( | |||
getFieldValidationErrors().stream() | |||
.map(s -> s.getResult().get()) | |||
.collect(Collectors.toList())); | |||
errors.addAll(getBeanValidationErrors()); | |||
return errors; | |||
} | |||
@@ -84,7 +85,7 @@ public class ValidationException extends Exception { | |||
* | |||
* @return binder validation errors list | |||
*/ | |||
public List<Result<?>> getBeanValidationErrors() { | |||
public List<ValidationResult> getBeanValidationErrors() { | |||
return beanValidationErrors; | |||
} | |||
} |
@@ -0,0 +1,103 @@ | |||
/* | |||
* Copyright 2000-2016 Vaadin Ltd. | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | |||
* use this file except in compliance with the License. You may obtain a copy of | |||
* the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |||
* License for the specific language governing permissions and limitations under | |||
* the License. | |||
*/ | |||
package com.vaadin.data; | |||
import java.io.Serializable; | |||
import java.util.Objects; | |||
/** | |||
* Represents the result of a validation. A result may be either successful or | |||
* contain an error message in case of a failure. | |||
* <p> | |||
* ValidationResult instances are created using the factory methods | |||
* {@link #ok()} and {@link #error(String)}, denoting success and failure | |||
* respectively. | |||
* | |||
* @author Vaadin Ltd | |||
* @since 8.0 | |||
* | |||
*/ | |||
public interface ValidationResult extends Serializable { | |||
class SimpleValidationResult implements ValidationResult { | |||
private final String error; | |||
SimpleValidationResult(String error) { | |||
this.error = error; | |||
} | |||
@Override | |||
public String getErrorMessage() { | |||
if (error == null) { | |||
throw new IllegalStateException("The result is not an error. " | |||
+ "It cannot contain error message"); | |||
} else { | |||
return error; | |||
} | |||
} | |||
@Override | |||
public boolean isError() { | |||
return error != null; | |||
} | |||
} | |||
/** | |||
* Returns the result message. | |||
* <p> | |||
* Throws an {@link IllegalStateException} if the result represents success. | |||
* | |||
* @return the error message | |||
* @throws IllegalStateException | |||
* if the result represents success | |||
*/ | |||
String getErrorMessage(); | |||
/** | |||
* Checks if the result denotes an error. | |||
* | |||
* @return <code>true</code> if the result denotes an error, | |||
* <code>false</code> otherwise | |||
*/ | |||
boolean isError(); | |||
/** | |||
* Returns a successful result. | |||
* | |||
* @return the successful result | |||
*/ | |||
public static ValidationResult ok() { | |||
return new SimpleValidationResult(null); | |||
} | |||
/** | |||
* Creates the validation result which represent an error with the given | |||
* {@code errorMessage}. | |||
* | |||
* @param errorMessage | |||
* error message, not {@code null} | |||
* @return validation result which represent an error with the given | |||
* {@code errorMessage} | |||
* @throws NullPointerException | |||
* if {@code errorMessage} is null | |||
*/ | |||
public static ValidationResult error(String errorMessage) { | |||
Objects.requireNonNull(errorMessage); | |||
return new SimpleValidationResult(errorMessage); | |||
} | |||
} |
@@ -22,8 +22,11 @@ import java.util.Optional; | |||
import com.vaadin.data.Binder.Binding; | |||
/** | |||
* Represents the outcome of field level validation. Use | |||
* {@link Binding#withValidationStatusHandler(ValidationStatusHandler)} to | |||
* Represents the status of field validation. Status can be {@code Status.OK}, | |||
* {@code Status.ERROR} or {@code Status.RESET}. Status OK and ERROR are always | |||
* associated with a ValidationResult {@link #getResult}. | |||
* <p> | |||
* Use {@link Binding#withValidationStatusHandler(ValidationStatusHandler)} to | |||
* register a handler for field level validation status changes. | |||
* | |||
* @author Vaadin Ltd | |||
@@ -65,7 +68,7 @@ public class ValidationStatus<TARGET> implements Serializable { | |||
} | |||
private final Status status; | |||
private final Result<TARGET> result; | |||
private final ValidationResult result; | |||
private final Binding<?, ?, TARGET> binding; | |||
/** | |||
@@ -94,7 +97,7 @@ public class ValidationStatus<TARGET> implements Serializable { | |||
* the result of the validation | |||
*/ | |||
public ValidationStatus(Binding<?, ?, TARGET> source, | |||
Result<TARGET> result) { | |||
ValidationResult result) { | |||
this(source, result.isError() ? Status.ERROR : Status.OK, result); | |||
} | |||
@@ -112,7 +115,7 @@ public class ValidationStatus<TARGET> implements Serializable { | |||
* the related result, may be {@code null} | |||
*/ | |||
public ValidationStatus(Binding<?, ?, TARGET> source, Status status, | |||
Result<TARGET> result) { | |||
ValidationResult result) { | |||
Objects.requireNonNull(source, "Event source may not be null"); | |||
Objects.requireNonNull(status, "Status may not be null"); | |||
if (Objects.equals(status, Status.OK) && result.isError() | |||
@@ -154,7 +157,8 @@ public class ValidationStatus<TARGET> implements Serializable { | |||
* status is not an error | |||
*/ | |||
public Optional<String> getMessage() { | |||
return Optional.ofNullable(result).flatMap(Result::getMessage); | |||
return Optional.ofNullable(result).filter(ValidationResult::isError) | |||
.map(ValidationResult::getErrorMessage); | |||
} | |||
/** | |||
@@ -164,7 +168,7 @@ public class ValidationStatus<TARGET> implements Serializable { | |||
* | |||
* @return the validation result | |||
*/ | |||
public Optional<Result<TARGET>> getResult() { | |||
public Optional<ValidationResult> getResult() { | |||
return Optional.ofNullable(result); | |||
} | |||
@@ -34,9 +34,9 @@ import com.vaadin.server.SerializablePredicate; | |||
* <pre> | |||
* Validator<Integer> v = num -> { | |||
* if (num >= 0) | |||
* return Result.ok(num); | |||
* return ValidationResult.ok(); | |||
* else | |||
* return Result.error("number must be positive"); | |||
* return ValidationResult.error("number must be positive"); | |||
* }; | |||
* </pre> | |||
* | |||
@@ -45,15 +45,15 @@ import com.vaadin.server.SerializablePredicate; | |||
* @param <T> | |||
* the type of the value to validate | |||
* | |||
* @see Result | |||
* @see ValidationResult | |||
*/ | |||
@FunctionalInterface | |||
public interface Validator<T> | |||
extends BiFunction<T, ValueContext, Result<T>>, Serializable { | |||
extends BiFunction<T, ValueContext, ValidationResult>, Serializable { | |||
/** | |||
* Validates the given value. Returns a {@code Result} instance representing | |||
* the outcome of the validation. | |||
* Validates the given value. Returns a {@code ValidationResult} instance | |||
* representing the outcome of the validation. | |||
* | |||
* @param value | |||
* the input value to validate | |||
@@ -62,7 +62,7 @@ public interface Validator<T> | |||
* @return the validation result | |||
*/ | |||
@Override | |||
public Result<T> apply(T value, ValueContext context); | |||
public ValidationResult apply(T value, ValueContext context); | |||
/** | |||
* Returns a validator that passes any value. | |||
@@ -72,14 +72,15 @@ public interface Validator<T> | |||
* @return an always-passing validator | |||
*/ | |||
public static <T> Validator<T> alwaysPass() { | |||
return (v, ctx) -> Result.ok(v); | |||
return (value, context) -> ValidationResult.ok(); | |||
} | |||
/** | |||
* Builds a validator out of a conditional function and an error message. If | |||
* the function returns true, the validator returns {@code Result.ok()}; if | |||
* it returns false or throws an exception, {@code Result.error()} is | |||
* returned with the given message. | |||
* it returns false or throws an exception, | |||
* {@link ValidationResult#error(String)} is returned with the given | |||
* message. | |||
* <p> | |||
* For instance, the following validator checks if a number is between 0 and | |||
* 10, inclusive: | |||
@@ -126,12 +127,14 @@ public interface Validator<T> | |||
return (value, context) -> { | |||
try { | |||
if (guard.test(value)) { | |||
return Result.ok(value); | |||
return ValidationResult.ok(); | |||
} else { | |||
return Result.error(errorMessageProvider.apply(context)); | |||
return ValidationResult | |||
.error(errorMessageProvider.apply(context)); | |||
} | |||
} catch (Exception e) { | |||
return Result.error(errorMessageProvider.apply(context)); | |||
return ValidationResult | |||
.error(errorMessageProvider.apply(context)); | |||
} | |||
}; | |||
} |
@@ -17,7 +17,7 @@ package com.vaadin.data.validator; | |||
import java.util.Objects; | |||
import com.vaadin.data.Result; | |||
import com.vaadin.data.ValidationResult; | |||
import com.vaadin.data.Validator; | |||
import com.vaadin.server.SerializableFunction; | |||
@@ -79,7 +79,8 @@ public abstract class AbstractValidator<T> implements Validator<T> { | |||
* whether the value is valid or not | |||
* @return the validation result | |||
*/ | |||
protected Result<T> toResult(T value, boolean isValid) { | |||
return isValid ? Result.ok(value) : Result.error(getMessage(value)); | |||
protected ValidationResult toResult(T value, boolean isValid) { | |||
return isValid ? ValidationResult.ok() | |||
: ValidationResult.error(getMessage(value)); | |||
} | |||
} |
@@ -19,8 +19,8 @@ package com.vaadin.data.validator; | |||
import java.io.Serializable; | |||
import java.util.Locale; | |||
import java.util.Objects; | |||
import java.util.Optional; | |||
import java.util.Set; | |||
import java.util.function.BinaryOperator; | |||
import javax.validation.ConstraintViolation; | |||
import javax.validation.MessageInterpolator.Context; | |||
@@ -28,7 +28,7 @@ import javax.validation.Validation; | |||
import javax.validation.ValidatorFactory; | |||
import javax.validation.metadata.ConstraintDescriptor; | |||
import com.vaadin.data.Result; | |||
import com.vaadin.data.ValidationResult; | |||
import com.vaadin.data.Validator; | |||
import com.vaadin.data.util.BeanUtil; | |||
import com.vaadin.data.util.converter.ValueContext; | |||
@@ -116,16 +116,17 @@ public class BeanValidator implements Validator<Object> { | |||
* @return the validation result | |||
*/ | |||
@Override | |||
public Result<Object> apply(final Object value, ValueContext context) { | |||
public ValidationResult apply(final Object value, ValueContext context) { | |||
Set<? extends ConstraintViolation<?>> violations = getJavaxBeanValidator() | |||
.validateValue(beanType, propertyName, value); | |||
BinaryOperator<Result<Object>> accumulator = (result1, | |||
result2) -> result1.flatMap(val -> result2); | |||
Locale locale = context.getLocale().orElse(Locale.getDefault()); | |||
return violations.stream().map(v -> Result.error(getMessage(v, locale))) | |||
.reduce(Result.ok(value), accumulator); | |||
Optional<ValidationResult> result = violations.stream() | |||
.map(violation -> ValidationResult | |||
.error(getMessage(violation, locale))) | |||
.findFirst(); | |||
return result.orElse(ValidationResult.ok()); | |||
} | |||
@Override |
@@ -18,7 +18,7 @@ package com.vaadin.data.validator; | |||
import java.util.Objects; | |||
import com.vaadin.data.HasValue; | |||
import com.vaadin.data.Result; | |||
import com.vaadin.data.ValidationResult; | |||
import com.vaadin.data.Validator; | |||
import com.vaadin.data.util.converter.ValueContext; | |||
@@ -67,11 +67,11 @@ public class NotEmptyValidator<T> implements Validator<T> { | |||
} | |||
@Override | |||
public Result<T> apply(T value, ValueContext context) { | |||
public ValidationResult apply(T value, ValueContext context) { | |||
if (Objects.isNull(value) || Objects.equals(value, "")) { | |||
return Result.error(message); | |||
return ValidationResult.error(message); | |||
} else { | |||
return Result.ok(value); | |||
return ValidationResult.ok(); | |||
} | |||
} | |||
@@ -18,7 +18,7 @@ package com.vaadin.data.validator; | |||
import java.util.Objects; | |||
import com.vaadin.data.Result; | |||
import com.vaadin.data.ValidationResult; | |||
import com.vaadin.data.util.converter.ValueContext; | |||
/** | |||
@@ -42,9 +42,9 @@ public class NotNullValidator extends AbstractValidator<String> { | |||
} | |||
@Override | |||
public Result<String> apply(String value, ValueContext context) { | |||
return Objects.isNull(value) ? Result.error(getMessage(value)) | |||
: Result.ok(value); | |||
public ValidationResult apply(String value, ValueContext context) { | |||
return Objects.isNull(value) ? ValidationResult.error(getMessage(value)) | |||
: ValidationResult.ok(); | |||
} | |||
} |
@@ -18,7 +18,7 @@ package com.vaadin.data.validator; | |||
import java.util.Comparator; | |||
import java.util.Objects; | |||
import com.vaadin.data.Result; | |||
import com.vaadin.data.ValidationResult; | |||
import com.vaadin.data.util.converter.ValueContext; | |||
/** | |||
@@ -98,7 +98,7 @@ public class RangeValidator<T> extends AbstractValidator<T> { | |||
* behavior depends on the used comparator. | |||
*/ | |||
@Override | |||
public Result<T> apply(T value, ValueContext context) { | |||
public ValidationResult apply(T value, ValueContext context) { | |||
return toResult(value, isValid(value)); | |||
} | |||
@@ -18,7 +18,7 @@ package com.vaadin.data.validator; | |||
import java.util.regex.Matcher; | |||
import java.util.regex.Pattern; | |||
import com.vaadin.data.Result; | |||
import com.vaadin.data.ValidationResult; | |||
import com.vaadin.data.util.converter.ValueContext; | |||
/** | |||
@@ -71,7 +71,7 @@ public class RegexpValidator extends AbstractValidator<String> { | |||
} | |||
@Override | |||
public Result<String> apply(String value, ValueContext context) { | |||
public ValidationResult apply(String value, ValueContext context) { | |||
return toResult(value, isValid(value)); | |||
} | |||
@@ -16,7 +16,7 @@ | |||
package com.vaadin.data.validator; | |||
import com.vaadin.data.Result; | |||
import com.vaadin.data.ValidationResult; | |||
import com.vaadin.data.util.converter.ValueContext; | |||
/** | |||
@@ -50,11 +50,11 @@ public class StringLengthValidator extends AbstractValidator<String> { | |||
} | |||
@Override | |||
public Result<String> apply(String value, ValueContext context) { | |||
public ValidationResult apply(String value, ValueContext context) { | |||
if (value == null) { | |||
return toResult(value, true); | |||
} | |||
Result<?> lengthCheck = validator.apply(value.length(), context); | |||
ValidationResult lengthCheck = validator.apply(value.length(), context); | |||
return toResult(value, !lengthCheck.isError()); | |||
} | |||
@@ -30,6 +30,7 @@ import java.util.logging.Logger; | |||
import org.jsoup.nodes.Element; | |||
import com.vaadin.data.Result; | |||
import com.vaadin.data.ValidationResult; | |||
import com.vaadin.data.util.converter.ValueContext; | |||
import com.vaadin.data.validator.DateRangeValidator; | |||
import com.vaadin.event.FieldEvents.BlurEvent; | |||
@@ -678,7 +679,7 @@ public abstract class AbstractDateField extends AbstractField<LocalDate> | |||
getDateOutOfRangeMessage(), | |||
getDate(getRangeStart(), getResolution()), | |||
getDate(getRangeEnd(), getResolution())); | |||
Result<LocalDate> result = validator.apply(value, | |||
ValidationResult result = validator.apply(value, | |||
new ValueContext(this)); | |||
if (result.isError()) { | |||
setComponentError(new UserError(getDateOutOfRangeMessage())); |
@@ -610,7 +610,7 @@ public class BinderBookOfVaadinTest { | |||
Assert.assertEquals(0, status.getFieldValidationErrors().size()); | |||
Assert.assertEquals(1, status.getBeanValidationErrors().size()); | |||
Assert.assertEquals( | |||
status.getBeanValidationErrors().get(0).getMessage().get(), | |||
status.getBeanValidationErrors().get(0).getErrorMessage(), | |||
message); | |||
Assert.assertEquals(message, formStatusLabel.getValue()); | |||
@@ -644,9 +644,10 @@ public class BinderBookOfVaadinTest { | |||
binder.setValidationStatusHandler(status -> { | |||
// create an error message on failed bean level validations | |||
List<Result<?>> errors = status.getBeanValidationErrors(); | |||
String errorMessage = errors.stream().map(Result::getMessage) | |||
.map(o -> o.get()).collect(Collectors.joining("\n")); | |||
List<ValidationResult> errors = status.getBeanValidationErrors(); | |||
String errorMessage = errors.stream() | |||
.map(ValidationResult::getErrorMessage) | |||
.collect(Collectors.joining("\n")); | |||
// show error in a label | |||
formStatusLabel.setValue(errorMessage); | |||
formStatusLabel.setVisible(!errorMessage.isEmpty()); | |||
@@ -685,7 +686,7 @@ public class BinderBookOfVaadinTest { | |||
Assert.assertEquals(0, status.getFieldValidationErrors().size()); | |||
Assert.assertEquals(1, status.getBeanValidationErrors().size()); | |||
Assert.assertEquals(message, | |||
status.getBeanValidationErrors().get(0).getMessage().get()); | |||
status.getBeanValidationErrors().get(0).getErrorMessage()); | |||
Assert.assertEquals(message, formStatusLabel.getValue()); | |||
@@ -93,8 +93,8 @@ public class BinderConverterValidatorTest | |||
String msg2 = "bar"; | |||
binding.withValidator(new Validator<String>() { | |||
@Override | |||
public Result<String> apply(String value, ValueContext context) { | |||
return new SimpleResult<>(null, msg1); | |||
public ValidationResult apply(String value, ValueContext context) { | |||
return ValidationResult.error(msg1); | |||
} | |||
}); | |||
binding.withValidator(value -> false, msg2); | |||
@@ -128,9 +128,9 @@ public class BinderConverterValidatorTest | |||
TextField salaryField = new TextField(); | |||
Validator<Number> positiveNumberValidator = (value, context) -> { | |||
if (value.doubleValue() >= 0) { | |||
return Result.ok(value); | |||
return ValidationResult.ok(); | |||
} else { | |||
return Result.error(NEGATIVE_ERROR_MESSAGE); | |||
return ValidationResult.error(NEGATIVE_ERROR_MESSAGE); | |||
} | |||
}; | |||
binder.forField(salaryField) |
@@ -386,7 +386,7 @@ public class BinderValidationStatusTest | |||
Assert.assertEquals(1, status.getBeanValidationErrors().size()); | |||
Assert.assertEquals("Need first name and age", | |||
status.getBeanValidationErrors().get(0).getMessage().get()); | |||
status.getBeanValidationErrors().get(0).getErrorMessage()); | |||
} | |||
@Test |
@@ -33,17 +33,15 @@ public class ValidatorTest extends ValidatorTestBase { | |||
@Test | |||
public void alwaysPass() { | |||
Validator<String> alwaysPass = Validator.alwaysPass(); | |||
Result<String> result = alwaysPass.apply("foo", new ValueContext()); | |||
Assert.assertTrue(result instanceof SimpleResult); | |||
SimpleResult<String> implRes = (SimpleResult<String>) result; | |||
Assert.assertFalse(implRes.getMessage().isPresent()); | |||
ValidationResult result = alwaysPass.apply("foo", new ValueContext()); | |||
Assert.assertFalse(result.isError()); | |||
} | |||
@Test | |||
public void from() { | |||
Validator<String> validator = Validator.from(Objects::nonNull, | |||
"Cannot be null"); | |||
Result<String> result = validator.apply(null, new ValueContext()); | |||
ValidationResult result = validator.apply(null, new ValueContext()); | |||
Assert.assertTrue(result.isError()); | |||
result = validator.apply("", new ValueContext()); |
@@ -18,7 +18,7 @@ package com.vaadin.data.validator; | |||
import org.junit.Assert; | |||
import org.junit.Test; | |||
import com.vaadin.data.Result; | |||
import com.vaadin.data.ValidationResult; | |||
import com.vaadin.data.util.converter.ValueContext; | |||
/** | |||
@@ -30,26 +30,25 @@ public class NotEmptyValidatorTest { | |||
@Test | |||
public void nullValueIsDisallowed() { | |||
NotEmptyValidator<String> validator = new NotEmptyValidator<>("foo"); | |||
Result<String> result = validator.apply(null, new ValueContext()); | |||
ValidationResult result = validator.apply(null, new ValueContext()); | |||
Assert.assertTrue(result.isError()); | |||
Assert.assertEquals("foo", result.getMessage().get()); | |||
Assert.assertEquals("foo", result.getErrorMessage()); | |||
} | |||
@Test | |||
public void emptyValueIsDisallowed() { | |||
NotEmptyValidator<String> validator = new NotEmptyValidator<>("foo"); | |||
Result<String> result = validator.apply("", new ValueContext()); | |||
ValidationResult result = validator.apply("", new ValueContext()); | |||
Assert.assertTrue(result.isError()); | |||
Assert.assertEquals("foo", result.getMessage().get()); | |||
Assert.assertEquals("foo", result.getErrorMessage()); | |||
} | |||
@Test | |||
public void nonNullValueIsAllowed() { | |||
NotEmptyValidator<Object> validator = new NotEmptyValidator<>("foo"); | |||
Object value = new Object(); | |||
Result<Object> result = validator.apply(value, new ValueContext()); | |||
ValidationResult result = validator.apply(value, new ValueContext()); | |||
Assert.assertFalse(result.isError()); | |||
Assert.assertFalse(result.isError()); | |||
result.ifOk(val -> Assert.assertEquals(value, val)); | |||
result.ifError(msg -> Assert.fail()); | |||
} | |||
} |
@@ -18,7 +18,7 @@ package com.vaadin.data.validator; | |||
import org.junit.Assert; | |||
import org.junit.Test; | |||
import com.vaadin.data.Result; | |||
import com.vaadin.data.ValidationResult; | |||
import com.vaadin.data.util.converter.ValueContext; | |||
public class NotNullValidatorTest { | |||
@@ -26,18 +26,17 @@ public class NotNullValidatorTest { | |||
@Test | |||
public void nullValueIsDisallowed() { | |||
NotNullValidator validator = new NotNullValidator("foo"); | |||
Result<String> result = validator.apply(null, new ValueContext()); | |||
ValidationResult result = validator.apply(null, new ValueContext()); | |||
Assert.assertTrue(result.isError()); | |||
Assert.assertEquals("foo", result.getMessage().get()); | |||
Assert.assertEquals("foo", result.getErrorMessage()); | |||
} | |||
@Test | |||
public void nonNullValueIsAllowed() { | |||
NotNullValidator validator = new NotNullValidator("foo"); | |||
Result<String> result = validator.apply("bar", new ValueContext()); | |||
ValidationResult result = validator.apply("bar", new ValueContext()); | |||
Assert.assertFalse(result.isError()); | |||
Assert.assertFalse(result.isError()); | |||
result.ifOk(value -> Assert.assertEquals("bar", value)); | |||
result.ifError(msg -> Assert.fail()); | |||
} | |||
} |
@@ -5,6 +5,7 @@ import java.util.Locale; | |||
import org.junit.Assert; | |||
import org.junit.Before; | |||
import com.vaadin.data.ValidationResult; | |||
import com.vaadin.data.Validator; | |||
import com.vaadin.data.util.converter.ValueContext; | |||
import com.vaadin.ui.Label; | |||
@@ -18,17 +19,20 @@ public class ValidatorTestBase { | |||
localeContext = new Label(); | |||
} | |||
protected <T> void assertPasses(T value, Validator<? super T> v) { | |||
v.apply(value, new ValueContext()) | |||
.handle(val -> Assert.assertEquals(value, val), err -> Assert | |||
.fail(value + " should pass " + v + " but got " + err)); | |||
protected <T> void assertPasses(T value, Validator<? super T> validator) { | |||
ValidationResult result = validator.apply(value, new ValueContext()); | |||
if (result.isError()) { | |||
Assert.fail(value + " should pass " + validator + " but got " | |||
+ result.getErrorMessage()); | |||
} | |||
} | |||
protected <T> void assertFails(T value, String errorMessage, | |||
Validator<? super T> v) { | |||
v.apply(value, new ValueContext(localeContext)).handle( | |||
val -> Assert.fail(value + " should fail " + v), | |||
err -> Assert.assertEquals(errorMessage, err)); | |||
Validator<? super T> validator) { | |||
ValidationResult result = validator.apply(value, | |||
new ValueContext(localeContext)); | |||
Assert.assertTrue(result.isError()); | |||
Assert.assertEquals(errorMessage, result.getErrorMessage()); | |||
} | |||
protected <T> void assertFails(T value, AbstractValidator<? super T> v) { |