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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 ValidationStatus
  46. *
  47. * @since 8.0
  48. */
  49. public class BinderValidationStatus<BEAN> implements Serializable {
  50. private final Binder<BEAN> binder;
  51. private final List<ValidationStatus<?>> 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()
  70. .map(b -> ValidationStatus.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<ValidationStatus<?>> 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().filter(ValidationStatus::isError)
  114. .findAny().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<ValidationStatus<?>> getFieldValidationStatuses() {
  146. return bindingStatuses;
  147. }
  148. /**
  149. * Gets the bean level validation results.
  150. * <p>
  151. * The bean level validators have been added with
  152. * {@link Binder#withValidator(Validator)} or in case of a
  153. * {@link BeanBinder} they might be automatically added {@link BeanValidator
  154. * BeanValidators}.
  155. *
  156. * @return the bean level validation results
  157. */
  158. public List<ValidationResult> getBeanValidationResults() {
  159. return binderStatuses;
  160. }
  161. /**
  162. * Gets the failed field level validation statuses.
  163. * <p>
  164. * The field level validtors have been added with
  165. * {@link BindingBuilder#withValidator(Validator)}.
  166. *
  167. * @return a list of failed field level validation statuses
  168. */
  169. public List<ValidationStatus<?>> getFieldValidationErrors() {
  170. return bindingStatuses.stream().filter(ValidationStatus::isError)
  171. .collect(Collectors.toList());
  172. }
  173. /**
  174. * Gets the failed bean level validation results.
  175. * <p>
  176. * The bean level validators have been added with
  177. * {@link Binder#withValidator(Validator)} or in case of a
  178. * {@link BeanBinder} they might be automatically added {@link BeanValidator
  179. * BeanValidators}.
  180. *
  181. * @return a list of failed bean level validation results
  182. */
  183. public List<ValidationResult> getBeanValidationErrors() {
  184. return binderStatuses.stream().filter(ValidationResult::isError)
  185. .collect(Collectors.toList());
  186. }
  187. }