aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/data/validator/NotEmptyValidator.java
blob: 1842ed376bf3ea95be600bd10e0d1ac5900b385a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.vaadin.data.validator;

import java.util.Objects;

import com.vaadin.data.Binder.BindingBuilder;
import com.vaadin.data.HasValue;
import com.vaadin.data.ValidationResult;
import com.vaadin.data.Validator;
import com.vaadin.data.ValueContext;

/**
 * Simple validator to check against {@code null} value and empty {@link String}
 * value.
 * <p>
 * This validator can be suitable for fields that have been marked as required
 * with {@link HasValue#setRequiredIndicatorVisible(boolean)}.
 * <p>
 * Note that
 * {@link BindingBuilder#asRequired(com.vaadin.data.ErrorMessageProvider)} does
 * almost the same thing, but verifies against the value NOT being equal to what
 * {@link HasValue#getEmptyValue()} returns and sets the required indicator
 * visible with {@link HasValue#setRequiredIndicatorVisible(boolean)}.
 *
 * @see HasValue#setRequiredIndicatorVisible(boolean)
 * @see BindingBuilder#asRequired(com.vaadin.data.ErrorMessageProvider)
 * @author Vaadin Ltd
 * @since 8.0
 *
 */
public class NotEmptyValidator<T> implements Validator<T> {

    private final String message;

    /**
     * @param message
     *            error validation message
     */
    public NotEmptyValidator(String message) {
        this.message = message;
    }

    @Override
    public ValidationResult apply(T value, ValueContext context) {
        if (Objects.isNull(value) || Objects.equals(value, "")) {
            return ValidationResult.error(message);
        } else {
            return ValidationResult.ok();
        }
    }

}