Validation Rules
Sparta has a simple rule's syntax that can be defined in two different ways. Here are the explanations of both:
Option 1:
The default syntax is to build your rule as string using various delimiters to denote the different parts (i.e., validator, arguments, and arguments values). A single attribute rule can be written as follows:
'attribute_name' => 'rule1 : argument1 = value1 , argument2 = value2 | rule2 | ....'
Rule's Syntax
The following list providers a brief description of each rule segment:
attribute_name
: Name of the field to be validated as defined in your input collectionrule1
: Rule name in lowercase (e.g. alpha, email, required,...etc.):
: Default delimiter used to separate a given rule and its argumentsargument1
: Argument name to be passed to the validator=
: Default delimiter used between an argument and its valuevalue1
: Argument's value,
:Default delimiter used between multiple arguments|
:Default delimiter used between multiple rulesrule2
:Other rule name
A simple example that illustrates the usage of this syntax option is given below:
<?php
$rules = [
'username' => 'required|alpha_num|min:12|max:50',
'email_address' => 'required|email'
];
Option 2:
Alternatively, a rule can be built using a simple array syntax as follows:
<?php
$rules = [
'username' => [
'required',
'alpha',
'length_min:12',
'length_max:50',
]
'email_address' => [
'required',
'email'
]
];
Predefined Validators List
Sparta comes with the below out of the box validators:
- Accepted: a field must be
yes
,on
,1
, ortrue
. - Alpha: a field must contain only
alphabetic
characters. - AlphaNum: a field must contain only
numerical
andalphabetic
characters. - Between: a field must be a number that falls within the
specified range
. - Boolean: a field must be
boolean
. - Callback: use this to add your custom validations using a valid
callback
. - Contains: a field must contain another given value.
- Date: a field must be a valid
date
. - Different: a field must have a different value than the other given field.
- Email: a field must be a valid
email
address. - Equals: a field must be
equal
to the specified other specified value. - Even: a field must be an
even
number. - FloatNumber: a field must be a
float
number. - In: an alias of the validator
InArray
- InArray: a field must be contained in the given
array
- Integer: a field must be a valid
integer
number - Ip: a field must be a valid
IP
address - IsArray: a field must be a valid
array
- IsInstanceOf: a field must be an instance of given reference/value
- Json: a field must be a valid
JSON
- Length: a field length must be equal to the specified length
- LengthMax: a field length must not exceed the given length
- LengthMin: a field length must not be less than the given length
- Max: an alias of
LengthMax
- Min: an alias of
LengthMix
- Negative: a field must be a
negative
number - NotEmpty: a field must not be
empty
- Numeric: a field must contain
numerical
characters - Odd: a field must be an
odd
number - Positive: a field must be a
positive
number - Regex: a field must match the given regular expression.
- Required: a field must be present and cannot be empty
- Slug: a field must be a valid
slug
- Timezone: a field must be a valid
timezone
identifier according to thetimezone_identifiers_list
PHP function. - Url: a field must be a valid
URL
However, this does not mean that we are only limited to this list. In fact, adding new validators is a breeze. To learn more about creating new validators, please refer to the contribution section