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.

AbstractValidator.java 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package com.vaadin.data.validator;
  2. import com.vaadin.data.Validator;
  3. /**
  4. * Default Validator base class. See
  5. * {@link com.vaadin.data.validator.Validator} for more information.
  6. * <p>
  7. * If the validation fails, the exception thrown contains the error message with
  8. * its argument 0 replaced with the toString() of the object being validated.
  9. * </p>
  10. *
  11. * @author IT Mill Ltd.
  12. * @version
  13. * @VERSION@
  14. * @since 5.4
  15. */
  16. @SuppressWarnings("serial")
  17. public abstract class AbstractValidator implements Validator {
  18. /**
  19. * Error message.
  20. */
  21. private String errorMessage;
  22. /**
  23. * Constructs a validator with an error message.
  24. *
  25. * @param errorMessage
  26. * the message included in the exception (with its parameter {0}
  27. * replaced by toString() of the object to be validated) in case
  28. * the validation fails
  29. */
  30. public AbstractValidator(String errorMessage) {
  31. this.errorMessage = errorMessage;
  32. }
  33. public void validate(Object value) throws InvalidValueException {
  34. if (!isValid(value)) {
  35. String message;
  36. if (value == null) {
  37. message = errorMessage.replace("{0}", "null");
  38. } else {
  39. message = errorMessage.replace("{0}", value.toString());
  40. }
  41. throw new InvalidValueException(message);
  42. }
  43. }
  44. /**
  45. * Gets the message to be displayed in case the value does not validate.
  46. *
  47. * @return the Error Message.
  48. */
  49. public String getErrorMessage() {
  50. return errorMessage;
  51. }
  52. /**
  53. * Sets the message to be displayed in case the value does not validate.
  54. *
  55. * @param errorMessage
  56. * the Error Message to set.
  57. */
  58. public void setErrorMessage(String errorMessage) {
  59. this.errorMessage = errorMessage;
  60. }
  61. }