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.

NotEmptyValidator.java 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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.validator;
  17. import java.util.Objects;
  18. import com.vaadin.data.Binder.BindingBuilder;
  19. import com.vaadin.data.HasValue;
  20. import com.vaadin.data.ValidationResult;
  21. import com.vaadin.data.Validator;
  22. import com.vaadin.data.util.converter.ValueContext;
  23. /**
  24. * Simple validator to check against {@code null} value and empty {@link String}
  25. * value.
  26. * <p>
  27. * This validator works similar to {@link NotNullValidator} but in addition it
  28. * also check whether the value is not an empty String.
  29. * <p>
  30. * This validator can be suitable for fields that have been marked as required
  31. * with {@link HasValue#setRequiredIndicatorVisible(boolean)}.
  32. * <p>
  33. * Note that {@link BindingBuilder#setRequired(com.vaadin.data.ErrorMessageProvider)}
  34. * does almost the same thing, but verifies against the value NOT being equal to
  35. * what {@link HasValue#getEmptyValue()} returns and sets the required indicator
  36. * visible with {@link HasValue#setRequiredIndicatorVisible(boolean)}.
  37. *
  38. * @see HasValue#setRequiredIndicatorVisible(boolean)
  39. * @see BindingBuilder#setRequired(com.vaadin.data.ErrorMessageProvider)
  40. * @author Vaadin Ltd
  41. * @since 8.0
  42. *
  43. */
  44. public class NotEmptyValidator<T> implements Validator<T> {
  45. private final String message;
  46. /**
  47. * @param message
  48. * error validation message
  49. */
  50. public NotEmptyValidator(String message) {
  51. this.message = message;
  52. }
  53. @Override
  54. public ValidationResult apply(T value, ValueContext context) {
  55. if (Objects.isNull(value) || Objects.equals(value, "")) {
  56. return ValidationResult.error(message);
  57. } else {
  58. return ValidationResult.ok();
  59. }
  60. }
  61. }