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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright 2000-2016 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.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.Objects;
  22. import java.util.stream.Collectors;
  23. import com.vaadin.data.Binder.BindingBuilder;
  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)} to handle
  34. * 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(binderStatuses,
  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. ArrayList<ValidationResult> errors = new ArrayList<>(
  131. getFieldValidationErrors().stream()
  132. .map(s -> s.getResult().get())
  133. .collect(Collectors.toList()));
  134. errors.addAll(getBeanValidationErrors());
  135. return errors;
  136. }
  137. /**
  138. * Gets the field level validation statuses.
  139. * <p>
  140. * The field level validtors have been added with
  141. * {@link BindingBuilder#withValidator(Validator)}.
  142. *
  143. * @return the field validation statuses
  144. */
  145. public List<BindingValidationStatus<?>> getFieldValidationStatuses() {
  146. return bindingStatuses;
  147. }
  148. /**
  149. * Gets the bean level validation results.
  150. *
  151. * @return the bean level validation results
  152. */
  153. public List<ValidationResult> getBeanValidationResults() {
  154. return binderStatuses;
  155. }
  156. /**
  157. * Gets the failed field level validation statuses.
  158. * <p>
  159. * The field level validtors have been added with
  160. * {@link BindingBuilder#withValidator(Validator)}.
  161. *
  162. * @return a list of failed field level validation statuses
  163. */
  164. public List<BindingValidationStatus<?>> getFieldValidationErrors() {
  165. return bindingStatuses.stream().filter(BindingValidationStatus::isError)
  166. .collect(Collectors.toList());
  167. }
  168. /**
  169. * Gets the failed bean level validation results.
  170. *
  171. * @return a list of failed bean level validation results
  172. */
  173. public List<ValidationResult> getBeanValidationErrors() {
  174. return binderStatuses.stream().filter(ValidationResult::isError)
  175. .collect(Collectors.toList());
  176. }
  177. }