You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BinderValidationStatus.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data;
  17. import java.io.Serializable;
  18. import java.util.Collections;
  19. import java.util.List;
  20. import java.util.Objects;
  21. import java.util.stream.Collectors;
  22. import com.vaadin.data.Binder.BindingBuilder;
  23. import com.vaadin.server.SerializablePredicate;
  24. /**
  25. * Binder validation status change. Represents the outcome of binder level
  26. * validation. Has information about the validation results for the
  27. * {@link BindingBuilder#withValidator(Validator) field level} and
  28. * {@link Binder#withValidator(Validator) binder level} validation.
  29. * <p>
  30. * Note: if there are any field level validation errors, the bean level
  31. * validation is not run.
  32. * <p>
  33. * Use {@link Binder#setValidationStatusHandler(BinderValidationStatusHandler)}
  34. * to handle form level validation status changes.
  35. *
  36. * @author Vaadin Ltd
  37. *
  38. * @param <BEAN>
  39. * the bean type of the binder
  40. *
  41. * @see BinderValidationStatusHandler
  42. * @see Binder#setValidationStatusHandler(BinderValidationStatusHandler)
  43. * @see Binder#validate()
  44. * @see BindingValidationStatus
  45. *
  46. * @since 8.0
  47. */
  48. public class BinderValidationStatus<BEAN> implements Serializable {
  49. private final Binder<BEAN> binder;
  50. private final List<BindingValidationStatus<?>> bindingStatuses;
  51. private final List<ValidationResult> binderStatuses;
  52. /**
  53. * Convenience method for creating a unresolved validation status for the
  54. * given binder.
  55. * <p>
  56. * In practice this status means that the values might not be valid, but
  57. * validation errors should be hidden.
  58. *
  59. * @param source
  60. * the source binder
  61. * @return a unresolved validation status
  62. * @param <BEAN>
  63. * the bean type of the binder
  64. */
  65. public static <BEAN> BinderValidationStatus<BEAN> createUnresolvedStatus(
  66. Binder<BEAN> source) {
  67. return new BinderValidationStatus<>(source,
  68. source.getBindings().stream().map(
  69. b -> BindingValidationStatus.createUnresolvedStatus(b))
  70. .collect(Collectors.toList()),
  71. Collections.emptyList());
  72. }
  73. /**
  74. * Creates a new binder validation status for the given binder and
  75. * validation results.
  76. *
  77. * @param source
  78. * the source binder
  79. * @param bindingStatuses
  80. * the validation results for the fields
  81. * @param binderStatuses
  82. * the validation results for binder level validation
  83. */
  84. public BinderValidationStatus(Binder<BEAN> source,
  85. List<BindingValidationStatus<?>> bindingStatuses,
  86. List<ValidationResult> binderStatuses) {
  87. Objects.requireNonNull(bindingStatuses,
  88. "binding statuses cannot be null");
  89. Objects.requireNonNull(binderStatuses,
  90. "binder statuses cannot be null");
  91. this.binder = source;
  92. this.bindingStatuses = Collections.unmodifiableList(bindingStatuses);
  93. this.binderStatuses = Collections.unmodifiableList(binderStatuses);
  94. }
  95. /**
  96. * Gets whether validation for the binder passed or not.
  97. *
  98. * @return {@code true} if validation has passed, {@code false} if not
  99. */
  100. public boolean isOk() {
  101. return !hasErrors();
  102. }
  103. /**
  104. * Gets whether the validation for the binder failed or not.
  105. *
  106. * @return {@code true} if validation failed, {@code false} if validation
  107. * passed
  108. */
  109. public boolean hasErrors() {
  110. return binderStatuses.stream().filter(ValidationResult::isError)
  111. .findAny().isPresent()
  112. || bindingStatuses.stream()
  113. .filter(BindingValidationStatus::isError).findAny()
  114. .isPresent();
  115. }
  116. /**
  117. * Gets the source binder of the status.
  118. *
  119. * @return the source binder
  120. */
  121. public Binder<BEAN> getBinder() {
  122. return binder;
  123. }
  124. /**
  125. * Gets both field and bean level validation errors.
  126. *
  127. * @return a list of all validation errors
  128. */
  129. public List<ValidationResult> getValidationErrors() {
  130. List<ValidationResult> errors = getFieldValidationErrors().stream()
  131. .map(s -> s.getResult().get()).collect(Collectors.toList());
  132. errors.addAll(getBeanValidationErrors());
  133. return errors;
  134. }
  135. /**
  136. * Gets the field level validation statuses.
  137. * <p>
  138. * The field level validators have been added with
  139. * {@link BindingBuilder#withValidator(Validator)}.
  140. *
  141. * @return the field validation statuses
  142. */
  143. public List<BindingValidationStatus<?>> getFieldValidationStatuses() {
  144. return bindingStatuses;
  145. }
  146. /**
  147. * Gets the bean level validation results.
  148. *
  149. * @return the bean level validation results
  150. */
  151. public List<ValidationResult> getBeanValidationResults() {
  152. return binderStatuses;
  153. }
  154. /**
  155. * Gets the failed field level validation statuses.
  156. * <p>
  157. * The field level validators have been added with
  158. * {@link BindingBuilder#withValidator(Validator)}.
  159. *
  160. * @return a list of failed field level validation statuses
  161. */
  162. public List<BindingValidationStatus<?>> getFieldValidationErrors() {
  163. return bindingStatuses.stream().filter(BindingValidationStatus::isError)
  164. .collect(Collectors.toList());
  165. }
  166. /**
  167. * Gets the failed bean level validation results.
  168. *
  169. * @return a list of failed bean level validation results
  170. */
  171. public List<ValidationResult> getBeanValidationErrors() {
  172. return binderStatuses.stream().filter(ValidationResult::isError)
  173. .collect(Collectors.toList());
  174. }
  175. /**
  176. * Notifies all validation status handlers in bindings.
  177. *
  178. * @see #notifyBindingValidationStatusHandlers(SerializablePredicate)
  179. *
  180. * @since 8.2
  181. */
  182. public void notifyBindingValidationStatusHandlers() {
  183. notifyBindingValidationStatusHandlers(t -> true);
  184. }
  185. /**
  186. * Notifies validation status handlers for bindings that pass given filter.
  187. * The filter should return {@code true} for each
  188. * {@link BindingValidationStatus} that should be delegated to the status
  189. * handler in the binding.
  190. *
  191. * @see #notifyBindingValidationStatusHandlers()
  192. *
  193. * @param filter
  194. * the filter to select bindings to run status handling for
  195. *
  196. * @since 8.2
  197. */
  198. public void notifyBindingValidationStatusHandlers(
  199. SerializablePredicate<BindingValidationStatus<?>> filter) {
  200. bindingStatuses.stream().filter(filter).forEach(s -> s.getBinding()
  201. .getValidationStatusHandler().statusChange(s));
  202. }
  203. }