aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/validation/ValidationOfRequiredEmptyFields.java
blob: 3384e40586afb7b2606d5e15361e760d08dbd5a0 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.vaadin.tests.validation;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.validator.IntegerValidator;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.legacy.data.Validator;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.TextField;

@SuppressWarnings("deprecation")
public class ValidationOfRequiredEmptyFields extends AbstractTestUI {

    private TextField tf;
    private CheckBox requiredInput;
    private TextField requiredErrorInput;

    private Validator integerValidator = new IntegerValidator(
            "Must be an integer");
    private Validator stringLengthValidator = new StringLengthValidator(
            "Must be 5-10 chars", 5, 10, false);
    private CheckBox integerValidatorInput;
    private CheckBox stringLengthValidatorInput;

    @Override
    protected void setup(VaadinRequest request) {
        requiredInput = new CheckBox("Field required");
        requiredInput.setImmediate(true);
        requiredInput.addValueChangeListener(new ValueChangeListener() {
            @Override
            public void valueChange(ValueChangeEvent event) {
                tf.setRequired(requiredInput.getValue());
            }
        });

        requiredErrorInput = new TextField("Required error message");
        requiredErrorInput.setImmediate(true);
        requiredErrorInput.addValueChangeListener(new ValueChangeListener() {
            @Override
            public void valueChange(ValueChangeEvent event) {
                tf.setRequiredError(requiredErrorInput.getValue());
            }
        });

        integerValidatorInput = new CheckBox("Integer validator");
        integerValidatorInput.setImmediate(true);
        integerValidatorInput.addValueChangeListener(new ValueChangeListener() {

            @Override
            public void valueChange(ValueChangeEvent event) {
                if (integerValidatorInput.getValue()) {
                    tf.addValidator(integerValidator);
                } else {
                    tf.removeValidator(integerValidator);
                }
            }
        });
        stringLengthValidatorInput = new CheckBox("String length validator");
        stringLengthValidatorInput.setImmediate(true);
        stringLengthValidatorInput
                .addValueChangeListener(new ValueChangeListener() {

                    @Override
                    public void valueChange(ValueChangeEvent event) {
                        if (stringLengthValidatorInput.getValue()) {
                            tf.addValidator(stringLengthValidator);
                        } else {
                            tf.removeValidator(stringLengthValidator);
                        }
                    }
                });

        tf = new TextField();
        tf.setImmediate(true);

        requiredInput.setValue(false);
        requiredErrorInput.setValue("");
        integerValidatorInput.setValue(false);
        stringLengthValidatorInput.setValue(false);

        addComponent(requiredInput);
        addComponent(requiredErrorInput);
        addComponent(integerValidatorInput);
        addComponent(stringLengthValidatorInput);
        addComponent(tf);
    }

    @Override
    protected String getTestDescription() {
        return "Tests that the lower textfield's tooltip displays validation error messages correctly.";
    }

    @Override
    protected Integer getTicketNumber() {
        return 3851;
    }

}