Slim3 Controller‎ > ‎


Slim3 provides a simple validation framework as follows:
Validators v = new Validators(request);
v.add("arg1", v.required(), v.integerType());
v.add("arg2", v.required(), v.integerType(), v.longRange(3, 5));
if (v.validate()) {
} else {
    Errors errors = v.getErrors();

If you add multiple validators on same request attribute, the message of the first validator that determins the attribute value is invalid is adapted.

The error messages are stored in <project>/src/application[_locale].properties as follows:
validator.required={0} is required.
validator.byteType={0} must be a byte.
validator.shortType={0} must be a short.
validator.integerType={0} must be an integer.
validator.longType={0} must be a long.
validator.floatType={0} must be a float.
validator.doubleType={0} must be a double.
validator.numberType={0} is not a number({1}).
validator.dateType={0} is not a date({1}).
validator.minlength={0} can not be less than {1} characters.
validator.maxlength={0} can not be greater than {1} characters.
validator.range={0} is not in the {1} to {2} range.
validator.regexp={0} is invalid.

{0} is an attribute name. If you want to change the attribute name by locale, define label.<attribute name> entry in <project>/src/application[_locale].properties.

Due to HOT reloading, when you chage the message of application[_locale].properties, you can see the result without restarting web application.

If the attribute is stored to a model, you can specify it type-safely using Meta data of model:
Validators v = new Validators(request);
BlogMeta meta = BlogMeta.get();
v.add(meta.title, v.required());
v.add(meta.content, v.required());

See Validators and demo.