summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/tests/tickets/Ticket1900.java
blob: a6f3c5c2244a42f81d0cc90a543bc8596a1c0267 (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
package com.vaadin.tests.tickets;

import com.vaadin.Application;
import com.vaadin.data.Property;
import com.vaadin.data.Validator;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

public class Ticket1900 extends Application {

    TextField f[] = new TextField[5];
    Window main = new Window("#1900 test");

    @Override
    public void init() {

        setMainWindow(main);

        for (int i = 0; i < 5; i++) {
            final int j = i;
            f[i] = new TextField("Testcase " + i);
            f[i].setImmediate(true);
            f[i].setRequired(true);
            main.addComponent(f[i]);
            f[i].addListener(new Property.ValueChangeListener() {
                public void valueChange(ValueChangeEvent event) {
                    main.showNotification("Validity test", "Testcase " + j
                            + " is " + (f[j].isValid() ? "valid" : "invalid"));
                }
            });
            f[i].addValidator(new ContainsValidator("1"));
            f[i].addValidator(new ContainsValidator("2"));

        }

        f[0].setDescription("Field is empty, requiredError(null): *");

        f[1]
                .setDescription("Field is empty, requiredError(\"foo\"): * (popup shows the validation error)");
        f[1].setRequiredError("The field must not be empty");

        f[2]
                .setDescription("Field is non-empty, validators do not give validation error: *");
        f[2].setValue("valid 12");

        f[3]
                .setDescription("Field is non-empty, requiredError(null), validators "
                        + "give validation error: * ! (popup shows the validation error)");
        f[3].setValue("invalid");

        f[4]
                .setDescription("Field is non-empty, requiredError(\"foo\"), validators "
                        + "give validation error: * ! (popup shows the validation error)");
        f[4].setValue("invalid");
        f[4].setRequiredError("The field must not be empty");

    }

    class ContainsValidator implements Validator {
        private final String c;

        public ContainsValidator(String c) {
            this.c = c;
        }

        public boolean isValid(Object value) {
            return value != null && value.toString().contains(c);
        }

        public void validate(Object value) throws InvalidValueException {
            if (!isValid(value)) {
                throw new InvalidValueException("Value does not contain " + c);
            }

        }

    }

}