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

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