Download the PHP package madesimple/php-form-validator without Composer
On this page you can find all versions of the php package madesimple/php-form-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download madesimple/php-form-validator
More information about madesimple/php-form-validator
Files in madesimple/php-form-validator
Package php-form-validator
Short Description Simple, extendable form validator for multi-dimensional forms
License MIT
Informations about the package php-form-validator
php-form-validator
Simple, extendable form validator for multi-dimensional forms
Installation
PHP Form Validator is available on Packagist (using semantic versioning), and installation via Composer is the recommended way to install.
Just add this line to your composer.json
file:
or run:
Validation Rules
Validation rules are an associative array of dot notation field names
in the input array to a pipe separated string of rules. The asterisk, *
,
can be use in the dot notation as a wildcard. For example, a set of rules
could look like this:
The following are all the validation rules that this library provides:
Rule Name | Keyword | Parameters | Description |
---|---|---|---|
Present | present |
The field must be present but can have any value including null . |
|
Required | required |
The field must be present and cannot be null (can be an empty string). |
|
Required If | required-if |
field(,value)+ |
The field is required if the specified field(s) and the specified value(s). |
Required With | required-with |
field |
The field is required if the other field is not null . |
Required With All | required-with-all |
field(,field)* |
The field is required if all of the other fields are not null . |
Required With Any | required-with-any |
field(,field)* |
The field is required if any of the other fields are not null . |
Required Without | required-without |
field |
The field is required if the other field is null . |
Equals | equals |
field |
The field's value must equals the other specified field's value. |
Not Equals | not-equals |
field |
The field's value must not equal the other specified field's value. |
Identical | identical |
field |
The field's value must be identical the other specified field's value. |
Not Identical | not-identical |
field |
The field's value must not be identical the other specified field's value. |
In | in |
value(,value)* |
The field must equal one of the specified options. |
Not In | not-in |
value(,value)* |
The field must not equal one of the specified options. |
Contains | contains |
value(,value)* |
The field should be an array and must contain all the specified options (may contain other values not listed). |
Contains Only | contains-only |
value(,value)* |
The field should be an array and must contain only the specified options. |
Minimum Array Count | min-arr-count |
int |
The field should be an array and must have an array_count of at least the specified value. |
Maximum Array Count | max-arr-count |
int |
The field should be an array and must have an array_count of at most the specified value. |
Minimum | min |
int |
The field should be numeric and must be at least the specified value. |
Maximum | max |
int |
The field should be numeric and must be at most the specified value. |
Greater Than | greater-than |
field |
The field should be numeric and must have a value greater than the other field. |
Less Than | less-than |
field |
The field should be numeric and must have a value less than the other field. |
Alpha | alpha |
The field must only contain alphabetic characters. | |
Alpha Numeric | alpha-numeric |
The field must only contain alphabetic and numerical characters. | |
Minimum String Length | min-str-len |
int |
The field should be string and must have a strlen of at least the specified value. |
Maximum String Length | max-str-len |
int |
The field should be a string and must have a strlen of at most the specified value. |
String Length | str-len |
int |
The field should be a string and must have a strlen of exactly the specified value. |
Human Name | human-name |
The field must be a valid human name. | |
Is: ... | is |
type |
The field must be of the specified basic PHP type. There must be a corresponding is_<type> method. |
email |
The field must be a valid email address | ||
Date | date |
(format)? |
The field must be a valid date in the specified format (defaults to 'Y-m-d' ). |
URL | url |
The field must be a valid URL. | |
UUID | uuid |
The field must be a valid UUID (\universally unique identifier). | |
Card Number | card-number |
The field must be a valid card number. | |
Regex | regex |
regex pattern |
The field must match the regex pattern. |
Not Regex | not-regex |
regex pattern |
The field must not match the regex pattern. |
Adding Extra Rules
Extra rules can be added to the validator to extend its functionality to provide specific rules for your project.
If you believe your rule should be added to the core library please submit a pull request.
To add your extra rule you must call both addRule
and setRuleMessage
.
Simple extra rule
For example, if you wanted to add a rule that would validate that a timezone was valid:
Extra rule with parameters
For example, if you wanted to add a rule that would validate that an identifier existed in your database:
Another example, if you wanted to do a complex validation of a sub-array:
Validation Rules
Present
The field must be present but can have any value including null
.
Required
The field must be present and cannot be null
(can be an empty string).
Required If
The field is required if the specified field(s) and the specified value(s).
Required With
The field is required if the other field is not null
.
Required With All
The field is required if all the other fields are not null
.
Required With Any
The field is required if any of the other fields are not null
.
Required Without
The field is required if the other field is null
.
Equals
The field's value must be equal to the other specified field's value.
Not Equals
The field's value must not equal the other specified field's value.
Identical
The field's value must be identical the other specified field's value.
Not Identical
The field's value must not be identical the other specified field's value.
In
The field must equal one of the specified options.
Not In
The field must not equal one of the specified options.
Contains
The field should be an array and must contain all the specified options (may contain other values not listed).
Contains Only
The field should be an array and must contain only the specified options.
Min Array Count
The field should be an array and must have an array_count
of at least the specified value.
Max Array Count
The field should be an array and must have an array_count
of at most the specified value.
Min
The field should be numeric and must be at least the specified value.
Max
The field should be numeric and must be at most the specified value.
Greater Than
The field should be numeric and must have a value greater than the other field.
Less Than
The field should be numeric and must have a value less than the other field.
Alpha
The field must only contain alphabetic characters.
Alpha Numeric
The field must only contain alphabetic and numerical characters.
Min String Length
The field should be string and must have a strlen
of at least the specified value.
Max String Length
The field should be a string and must have a strlen
of at most the specified value.
String Length
The field should be a string and must have a strlen
of exactly the specified value.
Human Name
The field must be a valid human name.
Is ...
The field must be of the specified basic PHP type. There must be a corresponding is_<type>
method.
The field must be a valid email address
Date
The field must be a valid date in the specified format (defaults to 'Y-m-d'
).
URL
The field must be a valid URL.
UUID
The field must be a valid UUID (\universally unique identifier).
Card Number
The field must be a valid card number. See http://stackoverflow.com/questions/174730/what-is-the-best-way-to-validate-a-credit-card-in-php for more details.
Regex
The field must match the regex pattern provided as parameter. Specify rules in an array when using this rule, especially when the regex expression contains a | character. See https://www.php.net/preg_match for nore details.
Not Regex
The field must not match the regex pattern provided as parameter. Specify rules in an array when using this rule, especially when the regex expression contains a | character. See https://www.php.net/preg_match for nore details.