Validation

Client side

To make client side validation need define rules for special form (see jQuery Validation Plugin).

Example of how to do it you can find at restore_password_validation.js

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
$('#restorePasswordFormEmail').validate({

    rules: {

        email: {
            required: true,
            email: true,
            checkNotUnique: "/restore_password/emailunique"
        }

    },

    messages: {

        email: {
            required: "Email is required",
            email: "Email not valid",
            checkNotUnique: "User with this email does not exist"
        }

    }

});

Server side

Server side validation based on class GeneralValidator. It uses interface Validator.

For API object validation need override method getFieldsConstraints() for example in IncidentRestControllerImpl

 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
@Override
protected Map<String, Map<GeneralValidator.Constraint, String>> getFieldsConstraints() {
    Map<String, Map<GeneralValidator.Constraint, String>> fieldsConstraints = super.getFieldsConstraints();

    GeneralValidator.buildField(fieldsConstraints, "title",
            new AbstractMap.SimpleEntry<>(GeneralValidator.Constraint.NOT_NULL_OR_EMPTY, null),
            new AbstractMap.SimpleEntry<>(GeneralValidator.Constraint.MIN, "4")
    );

    GeneralValidator.buildField(fieldsConstraints, "description",
            new AbstractMap.SimpleEntry<>(GeneralValidator.Constraint.NOT_NULL_OR_EMPTY, null),
            new AbstractMap.SimpleEntry<>(GeneralValidator.Constraint.MIN, "4")
    );

    GeneralValidator.buildField(fieldsConstraints, "categories",
            new AbstractMap.SimpleEntry<>(GeneralValidator.Constraint.NOT_NULL_OR_EMPTY, null)
    );

    GeneralValidator.buildField(fieldsConstraints, "pupils",
            new AbstractMap.SimpleEntry<>(GeneralValidator.Constraint.NOT_NULL_OR_EMPTY, null)
    );

    GeneralValidator.buildField(fieldsConstraints, "priority",
            new AbstractMap.SimpleEntry<>(GeneralValidator.Constraint.NOT_NULL_OR_EMPTY, null)
    );

    return fieldsConstraints;
}

Example how create validation from AdminController for form parameters.

236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
Map<String, Map<GeneralValidator.Constraint, String>> constraints = new HashMap<>();

GeneralValidator.buildField(constraints, "password",
        new SimpleEntry<>(GeneralValidator.Constraint.NOT_NULL_OR_EMPTY, null),
        new SimpleEntry<>(GeneralValidator.Constraint.MIN, "4"),
        new SimpleEntry<>(GeneralValidator.Constraint.MATCH_WITH, "confirmPassword")
);

GeneralValidator.buildField(constraints, "person.firstName",
        new SimpleEntry<>(GeneralValidator.Constraint.NOT_NULL_OR_EMPTY, null),
        new SimpleEntry<>(GeneralValidator.Constraint.MIN, "4")
);

GeneralValidator.buildField(constraints, "person.lastName",
        new SimpleEntry<>(GeneralValidator.Constraint.NOT_NULL_OR_EMPTY, null),
        new SimpleEntry<>(GeneralValidator.Constraint.MIN, "4")
);

GeneralValidator.buildField(constraints, "person.emails",
        new SimpleEntry<>(GeneralValidator.Constraint.NOT_NULL_OR_EMPTY, null),
        new SimpleEntry<>(GeneralValidator.Constraint.REGEX, GeneralValidator.EMAIL_PATTERN)
);

GeneralValidator.buildField(constraints, "person.phones",
        new SimpleEntry<>(GeneralValidator.Constraint.NOT_NULL_OR_EMPTY, null),
        new SimpleEntry<>(GeneralValidator.Constraint.MIN, "8")
);


if (userService.findByUsername(user.getUsername()) != null) {
    bindingResult.reject(null, "username not unique");
}

if (userService.findByEmail(email) != null) {
    bindingResult.reject(null, "email not unique");
}

new GeneralValidator(constraints).invoke(user, bindingResult);