-
Notifications
You must be signed in to change notification settings - Fork 24
Constraints
As mentioned before, validation in Regula centers around constraints. Constraints take the following form (in general):
@constraint-name(
param-1=value-1,
..,
param-n=value-n
[, label=label-text]
[, message=message-text]
[, groups=array-of-group-names]
);
As can be seen, the constraints take named parameters. They also take three optional parameters called label
, message
, and groups
. The message
parameter specifies the error message to display when the constraint fails. If this parameter is not provided, the default message is used. The value of the message parameter is a regular string, which can contain interpolated values. For example, assume you had message="{label} is lesser than {max}"
. In this case, the value of label
is substituted for {label}
, and the value of the parameter max
is substituted for {max}
. This lets you create customizable error messages. The groups
parameter lets you group your constraints into validation groups. These are analogous (for the most part) to validation groups in JSR-303. There is no mechanism in Regula to specify inheritance between groups. However, group-sequences can be emulated by explicitly specifying a sequences of groups to validate.
Regula comes with twenty default-constraints, out of which two are form-specific constraints. Form-specific constraints only apply to forms (as a whole) while the other constraints apply to input elements.
- @Required – Enforces the constraint a form element is required (must be checked, selected, or contain a value).
- @Checked – Enforces the constraint that a checkbox or a radio button must be checked.
- @Selected – Enforces the constraint that a select box must be selected.
- @Integer – Enforces the constraint that a field can only hold a positive or negative integer number.
- @Real – Enforces the constraint that a field can only hold a positive or negative real number.
- @Max – Enforces the constraint that a field can only hold a number lesser than or equal to a certain value.
- @Min – Enforces the constraint that a field can only hold a number that is greater than or equal to a certain value.
- @Range – Enforces the constraint that a field can only hold a number that is between two values (both inclusive).
- @Between – See @Range
- @NotBlank – Enforces the constraint that a field cannot be blank.
- @NotEmpty – See @NotBlank
- @Blank – Enforces the constraint that a field must be blank.
- @Empty – See @Blank
- @Pattern – Enforces the constraint that a field must match the specified pattern.
- @Matches – See @Pattern
- @Email – Enforces the constraint that the field must contain a valid email.
- @Alpha – Enforces the constraint that the field can only contain letters.
- @Numeric – Enforces the constraint that the field can only contain numbers.
- @AlphaNumeric – Enforces the constraint that the field can only contain letters and numbers.
- @Length – Enforces the constraint that the number of characters in the field must be between two specified values (a maximum and a minimum).
- @Digits – Enforces the constraint that number of digits in the integer part of the value of the field can be up to a specified number of digits, and the number of digits in the fractional part of the value can be up to another specified number of digits.
- @Past – Enforces the constraint that the date value of the field must be in the past.
- @Future – Enforces the constraint that the date value of the field must be in the future.
- @CompletelyFilled – Enforces the form-specific constraint that all fields in the form must be filled.
- @PasswordsMatch – Enforces the constraint that the values of the password fields must match.