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.

ValidationException.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.util.Collections;
  18. import java.util.List;
  19. import java.util.stream.Collectors;
  20. /**
  21. * Indicates validation errors in a {@link Binder} when a field value is
  22. * validated.
  23. *
  24. * @see Binder#writeBean(Object)
  25. * @see Binder#writeBeanIfValid(Object)
  26. *
  27. * @author Vaadin Ltd
  28. * @since 8.0
  29. *
  30. */
  31. public class ValidationException extends Exception {
  32. private final List<BindingValidationStatus<?>> fieldValidationErrors;
  33. private final List<ValidationResult> beanValidationErrors;
  34. /**
  35. * Constructs a new exception with validation {@code errors} list.
  36. *
  37. * @param fieldValidationErrors
  38. * binding validation errors list
  39. * @param beanValidationErrors
  40. * binder validation errors list
  41. */
  42. public ValidationException(
  43. List<BindingValidationStatus<?>> fieldValidationErrors,
  44. List<ValidationResult> beanValidationErrors) {
  45. super("Validation has failed for some fields");
  46. this.fieldValidationErrors = Collections
  47. .unmodifiableList(fieldValidationErrors);
  48. this.beanValidationErrors = Collections
  49. .unmodifiableList(beanValidationErrors);
  50. }
  51. /**
  52. * Gets both field and bean level validation errors.
  53. *
  54. * @return a list of all validation errors
  55. */
  56. public List<ValidationResult> getValidationErrors() {
  57. List<ValidationResult> errors = getFieldValidationErrors().stream()
  58. .map(s -> s.getResult().get()).collect(Collectors.toList());
  59. errors.addAll(getBeanValidationErrors());
  60. return errors;
  61. }
  62. /**
  63. * Returns a list of the field level validation errors which caused the
  64. * exception, or an empty list if the exception was caused by
  65. * {@link #getBeanValidationErrors() bean level validation errors}.
  66. *
  67. * @return binding validation errors list
  68. */
  69. public List<BindingValidationStatus<?>> getFieldValidationErrors() {
  70. return fieldValidationErrors;
  71. }
  72. /**
  73. * Returns a list of the bean level validation errors which caused the
  74. * exception, or an empty list if the exception was caused by
  75. * {@link #getFieldValidationErrors() field level validation errors}.
  76. *
  77. * @return binder validation errors list
  78. */
  79. public List<ValidationResult> getBeanValidationErrors() {
  80. return beanValidationErrors;
  81. }
  82. }