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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. import com.vaadin.data.validator.BeanValidator;
  25. /**
  26. * Binder validation status change. Represents the outcome of binder level
  27. * validation. Has information about the validation results for the
  28. * {@link BindingBuilder#withValidator(Validator) field level} and
  29. * {@link Binder#withValidator(Validator)binder level} validation.
  30. * <p>
  31. * Note: if there are any field level validation errors, the bean level
  32. * validation is not run.
  33. * <p>
  34. * Use {@link Binder#setValidationStatusHandler(BinderStatusHandler)} to handle
  35. * form level validation status changes.
  36. *
  37. * @author Vaadin Ltd
  38. *
  39. * @param <BEAN>
  40. * the bean type of the binder
  41. *
  42. * @see BinderValidationStatusHandler
  43. * @see Binder#setValidationStatusHandler(BinderStatusHandler)
  44. * @see Binder#validate()
  45. * @see BindingValidationStatus
  46. *
  47. * @since 8.0
  48. */
  49. public class BinderValidationStatus<BEAN> implements Serializable {
  50. private final Binder<BEAN> binder;
  51. private final List<BindingValidationStatus<?>> bindingStatuses;
  52. private final List<ValidationResult> binderStatuses;
  53. /**
  54. * Convenience method for creating a unresolved validation status for the
  55. * given binder.
  56. * <p>
  57. * In practice this status means that the values might not be valid, but
  58. * validation errors should be hidden.
  59. *
  60. * @param source
  61. * the source binder
  62. * @return a unresolved validation status
  63. * @param <BEAN>
  64. * the bean type of the binder
  65. */
  66. public static <BEAN> BinderValidationStatus<BEAN> createUnresolvedStatus(
  67. Binder<BEAN> source) {
  68. return new BinderValidationStatus<>(source,
  69. source.getBindings().stream().map(
  70. b -> BindingValidationStatus.createUnresolvedStatus(b))
  71. .collect(Collectors.toList()),
  72. Collections.emptyList());
  73. }
  74. /**
  75. * Creates a new binder validation status for the given binder and
  76. * validation results.
  77. *
  78. * @param source
  79. * the source binder
  80. * @param bindingStatuses
  81. * the validation results for the fields
  82. * @param binderStatuses
  83. * the validation results for binder level validation
  84. */
  85. public BinderValidationStatus(Binder<BEAN> source,
  86. List<BindingValidationStatus<?>> bindingStatuses,
  87. List<ValidationResult> binderStatuses) {
  88. Objects.requireNonNull(binderStatuses,
  89. "binding statuses cannot be null");
  90. Objects.requireNonNull(binderStatuses,
  91. "binder statuses cannot be null");
  92. this.binder = source;
  93. this.bindingStatuses = Collections.unmodifiableList(bindingStatuses);
  94. this.binderStatuses = Collections.unmodifiableList(binderStatuses);
  95. }
  96. /**
  97. * Gets whether validation for the binder passed or not.
  98. *
  99. * @return {@code true} if validation has passed, {@code false} if not
  100. */
  101. public boolean isOk() {
  102. return !hasErrors();
  103. }
  104. /**
  105. * Gets whether the validation for the binder failed or not.
  106. *
  107. * @return {@code true} if validation failed, {@code false} if validation
  108. * passed
  109. */
  110. public boolean hasErrors() {
  111. return binderStatuses.stream().filter(ValidationResult::isError)
  112. .findAny().isPresent()
  113. || bindingStatuses.stream()
  114. .filter(BindingValidationStatus::isError).findAny()
  115. .isPresent();
  116. }
  117. /**
  118. * Gets the source binder of the status.
  119. *
  120. * @return the source binder
  121. */
  122. public Binder<BEAN> getBinder() {
  123. return binder;
  124. }
  125. /**
  126. * Gets both field and bean level validation errors.
  127. *
  128. * @return a list of all validation errors
  129. */
  130. public List<ValidationResult> getValidationErrors() {
  131. ArrayList<ValidationResult> errors = new ArrayList<>(
  132. getFieldValidationErrors().stream()
  133. .map(s -> s.getResult().get())
  134. .collect(Collectors.toList()));
  135. errors.addAll(getBeanValidationErrors());
  136. return errors;
  137. }
  138. /**
  139. * Gets the field level validation statuses.
  140. * <p>
  141. * The field level validtors have been added with
  142. * {@link BindingBuilder#withValidator(Validator)}.
  143. *
  144. * @return the field validation statuses
  145. */
  146. public List<BindingValidationStatus<?>> getFieldValidationStatuses() {
  147. return bindingStatuses;
  148. }
  149. /**
  150. * Gets the bean level validation results.
  151. * <p>
  152. * The bean level validators have been added with
  153. * {@link Binder#withValidator(Validator)} or in case of a
  154. * {@link BeanBinder} they might be automatically added {@link BeanValidator
  155. * BeanValidators}.
  156. *
  157. * @return the bean level validation results
  158. */
  159. public List<ValidationResult> getBeanValidationResults() {
  160. return binderStatuses;
  161. }
  162. /**
  163. * Gets the failed field level validation statuses.
  164. * <p>
  165. * The field level validtors have been added with
  166. * {@link BindingBuilder#withValidator(Validator)}.
  167. *
  168. * @return a list of failed field level validation statuses
  169. */
  170. public List<BindingValidationStatus<?>> getFieldValidationErrors() {
  171. return bindingStatuses.stream().filter(BindingValidationStatus::isError)
  172. .collect(Collectors.toList());
  173. }
  174. /**
  175. * Gets the failed bean level validation results.
  176. * <p>
  177. * The bean level validators have been added with
  178. * {@link Binder#withValidator(Validator)} or in case of a
  179. * {@link BeanBinder} they might be automatically added {@link BeanValidator
  180. * BeanValidators}.
  181. *
  182. * @return a list of failed bean level validation results
  183. */
  184. public List<ValidationResult> getBeanValidationErrors() {
  185. return binderStatuses.stream().filter(ValidationResult::isError)
  186. .collect(Collectors.toList());
  187. }
  188. }