Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Validator.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.v7.data;
  17. import java.io.Serializable;
  18. import com.vaadin.data.Binder;
  19. import com.vaadin.server.AbstractErrorMessage;
  20. import com.vaadin.server.AbstractErrorMessage.ContentMode;
  21. import com.vaadin.server.ErrorMessage;
  22. import com.vaadin.server.ErrorMessageProducer;
  23. import com.vaadin.server.UserError;
  24. import com.vaadin.server.VaadinServlet;
  25. import com.vaadin.shared.ui.ErrorLevel;
  26. /**
  27. * Interface that implements a method for validating if an {@link Object} is
  28. * valid or not.
  29. * <p>
  30. * Implementors of this class can be added to any {@link Validatable}
  31. * implementor to verify its value.
  32. * </p>
  33. * <p>
  34. * {@link #validate(Object)} can be used to check if a value is valid. An
  35. * {@link InvalidValueException} with an appropriate validation error message is
  36. * thrown if the value is not valid.
  37. * </p>
  38. * <p>
  39. * Validators must not have any side effects.
  40. * </p>
  41. * <p>
  42. * Since Vaadin 7, the method isValid(Object) does not exist in the interface -
  43. * {@link #validate(Object)} should be used instead, and the exception caught
  44. * where applicable. Concrete classes implementing {@link Validator} can still
  45. * internally implement and use isValid(Object) for convenience or to ease
  46. * migration from earlier Vaadin versions.
  47. * </p>
  48. *
  49. * @author Vaadin Ltd.
  50. * @since 3.0
  51. * @deprecated As of 8.0, replaced by {@link com.vaadin.data.Validator}. The validation is performed
  52. * outside components, see {@link Binder}.{@code withValidator(...)}
  53. */
  54. @Deprecated
  55. public interface Validator extends Serializable {
  56. /**
  57. * Checks the given value against this validator. If the value is valid the
  58. * method does nothing. If the value is invalid, an
  59. * {@link InvalidValueException} is thrown.
  60. *
  61. * @param value
  62. * the value to check
  63. * @throws Validator.InvalidValueException
  64. * if the value is invalid
  65. */
  66. public void validate(Object value) throws Validator.InvalidValueException;
  67. /**
  68. * Exception that is thrown by a {@link Validator} when a value is invalid.
  69. *
  70. * <p>
  71. * The default implementation of InvalidValueException does not support HTML
  72. * in error messages. To enable HTML support, override
  73. * {@link #getHtmlMessage()} and use the subclass in validators.
  74. * </p>
  75. *
  76. * @author Vaadin Ltd.
  77. * @since 3.0
  78. */
  79. @Deprecated
  80. @SuppressWarnings("serial")
  81. public class InvalidValueException extends RuntimeException
  82. implements ErrorMessageProducer {
  83. /**
  84. * Array of one or more validation errors that are causing this
  85. * validation error.
  86. */
  87. private InvalidValueException[] causes = null;
  88. /**
  89. * Constructs a new {@code InvalidValueException} with the specified
  90. * message.
  91. *
  92. * @param message
  93. * The detail message of the problem.
  94. */
  95. public InvalidValueException(String message) {
  96. this(message, new InvalidValueException[] {});
  97. }
  98. /**
  99. * Constructs a new {@code InvalidValueException} with a set of causing
  100. * validation exceptions. The causing validation exceptions are included
  101. * when the exception is painted to the client.
  102. *
  103. * @param message
  104. * The detail message of the problem.
  105. * @param causes
  106. * One or more {@code InvalidValueException}s that caused
  107. * this exception.
  108. */
  109. public InvalidValueException(String message,
  110. InvalidValueException... causes) {
  111. super(message);
  112. if (causes == null) {
  113. throw new NullPointerException(
  114. "Possible causes array must not be null");
  115. }
  116. this.causes = causes;
  117. }
  118. /**
  119. * Check if the error message should be hidden.
  120. *
  121. * An empty (null or "") message is invisible unless it contains nested
  122. * exceptions that are visible.
  123. *
  124. * @return true if the error message should be hidden, false otherwise
  125. */
  126. public boolean isInvisible() {
  127. String msg = getMessage();
  128. if (msg != null && !msg.isEmpty()) {
  129. return false;
  130. }
  131. if (causes != null) {
  132. for (int i = 0; i < causes.length; i++) {
  133. if (!causes[i].isInvisible()) {
  134. return false;
  135. }
  136. }
  137. }
  138. return true;
  139. }
  140. /**
  141. * Returns the message of the error in HTML.
  142. *
  143. * Note that this API may change in future versions.
  144. */
  145. public String getHtmlMessage() {
  146. return VaadinServlet.safeEscapeForHtml(getLocalizedMessage());
  147. }
  148. /**
  149. * Returns the {@code InvalidValueExceptions} that caused this
  150. * exception.
  151. *
  152. * @return An array containing the {@code InvalidValueExceptions} that
  153. * caused this exception. Returns an empty array if this
  154. * exception was not caused by other exceptions.
  155. */
  156. public InvalidValueException[] getCauses() {
  157. return causes;
  158. }
  159. // Intentional change in compatibility package
  160. @Override
  161. public ErrorMessage getErrorMessage() {
  162. UserError error = new UserError(getHtmlMessage(), ContentMode.HTML,
  163. ErrorLevel.ERROR);
  164. for (Validator.InvalidValueException nestedException : getCauses()) {
  165. error.addCause(AbstractErrorMessage
  166. .getErrorMessageForException(nestedException));
  167. }
  168. return error;
  169. }
  170. }
  171. /**
  172. * A specific type of {@link InvalidValueException} that indicates that
  173. * validation failed because the value was empty. What empty means is up to
  174. * the thrower.
  175. *
  176. * @author Vaadin Ltd.
  177. * @since 5.3.0
  178. */
  179. @SuppressWarnings("serial")
  180. @Deprecated
  181. public class EmptyValueException extends Validator.InvalidValueException {
  182. public EmptyValueException(String message) {
  183. super(message);
  184. }
  185. }
  186. }