Validator is an abstract class for Record validation See \PHPFUI\ORM\Record\Validation namespace for examples.
Individual validators are listed in the table below. Validators can be combined. For example, a field can be required, and have a minlength and maxlength. Validators can have parameters. Parameters are separated by a colon (:) and then commas for each separate parameter.
Usage
$record = new \App\Record\Example();
$record->setFrom($_POST);
$validationErrors = $record->validate();
if (! validationErrors)
{
$insertedId = $record->insert();
}
$validationErrors is an array indexed by field name containing an array of translated errors.
foreach ($validationErrors as $field => $fieldErrors)
{
echo "Field {$field} has the following errors:\n";
foreach ($fieldErrors as $error)
{
echo $error . "\n";
}
}
Validator Name | Description | Parameters |
---|---|---|
alnum | Numbers and characters only (ctype_alnum) | None |
alpha | Characters only (ctype_alpha) | None |
bool | Must be one or zero | None |
card | Credit card number (LUHN validation) | None |
color | HTML color (#fff or #fafbfc, '#' is optional) | None |
contains | Field must contain (case sensitive) | comma separated list of strings |
cvv | Credit card cvv number | None |
date | Loosely formatted date (Y-M-D) | None |
dateISO | Strictly formatted ISO Date (YYYY-MM-DD) | None |
datetime | Loosely formatted date (Y-M-D) followed by time format | None |
day_month_year | Loosely formatted date (D-M-Y) | None |
domain | Valid domain | None |
Valid email | None | |
ends_with | Field must end with (case sensitive) | comma separated list of strings |
enum | MySQL enum value, case insensitive | comma separated list of identifiersExample: enum:Get,Post,Put,Delete |
enum_exact | MySQL enum value, case sensitive | comma separated list of identifiersExample: enum:ssl,tls |
eq_field | Equal to field | field, required |
equal | Value must be equal | value, required |
gt_field | Greater Than field | field, required |
gte_field | Greater Than or Equal to field | field, required |
icontains | Field must contain (case insensitive) | comma separated list of strings |
iends_with | Field must end with (case insensitive) | comma separated list of strings |
integer | Whole number, no fractional part | None |
istarts_with | Field must start with (case insensitive) | comma separated list of strings |
lt_field | Less Than field | field, required |
lte_field | Less Than or Equal to field | field, required |
maxlength | Length must be greater or equal | Optional length, else MySQL limit |
maxvalue | Value must be greater or equal | value, required |
minlength | Must be less than or equal | number, default field size |
minvalue | Must be less than or equal | value, required |
month_day_year | Loosely formatted date (M-D-Y) | None |
month_year | Loosely formatted Month Year | None |
neq_field | Not Equal to field | field, required |
not_equal | Value must not be equal | value, required |
number | Floating point number or whole number | None |
required | Field is required, can't be null or blank, 0 is OK | None |
starts_with | Field must start with (case sensitive) | comma separated list of strings |
time | Time (ampm or military), : separators | None |
unique | Column must be a unique value | See Below |
url | Valid URL (ftp, http, etc) | None |
website | Valid URL (http or https only) | None |
year_month | Loosely formatted Year Month | None |
Field Comparison Validators
You can compare one field to another on the same \App\Record with the field validators.
- gt_field
- lt_field
- gte_field
- lte_field
- eq_field
- neq_field
Field validators take another field name as a parameter and perform the specified condition test. To compare against a specific value, use minvalue, maxvalue, equal or not_equal.
Unique Parameters
Without any parameters, the unique validator will make sure no other record has a matching value for the field being validated. The current record is always exempted from the unique test so it can be updated.
If there are parameters, the first parameter must be a field of the current record. If this is the only parameter, or if the next parameter is also a field of the record, then the unique test is only done with the value of this field set to the current record's value.
If the next parameter is not a field of the record, it is used as a value to match for the preceeding field for the unique test.
The above repeats until all parameters are exhausted.
Examples:
Suppose you have a table with the following fields:
- name
- company
- division
- type
You want the name to be unique per company: unique:company You want the name to be unique per division with in the company: unique:company,division You want the name to be unique for a specific type in the division: unique:type,shoes,division You want the name to be unique for a specific type and division: unique:type,shoes,division,10
NOT Operator
You can reverse any validator by preceding the validator with an ! (exclamation mark).
Example: !starts_with:/ will fail if the field starts with a /
OR Operator
You can validate a field if any one of the validators passes. Use the vertical bar (|) to separate validators. If one of the validators passes, then the the field is valid.
Example: website|starts_with:/ will validate a fully qualified http url, or a root relative url.
Optional Validation
You may need to do additional checks for a specific record type. A second parameter can be passed to the contructor which would represent the original values of the record.
You can also pass an optional method to validate to perform more complex validation. If you use an optional method, the validator will not perform the standard validations unless you specifically call the validate() method again without the optional method parameter.
Multi Validator Example
class Order extends \PHPFUI\ORM\Validator
{
public static array $validators = [
'order_date' => ['required', 'maxlength', 'datetime', 'minvalue:2000-01-01', 'maxvalue:2099-12-31'],
];
}
Properties |
public static array $dateSeparators = ['-', '.', '_', ':', '/']
|
public static array $validators = []
|
Methods |
public __construct(PHPFUI |
public getErrors() : array Return any errors.
|
public validate(string $optionalMethod = '') : bool Return true if the entire record validates
|
Properties |
protected string $currentField = '' |
protected array $currentFieldDefinitions = []
|
protected bool $currentNot = false |
protected array $currentParameters = []
|
protected bool $currentRequired = false |
protected array $fieldDefinitions = []
|
protected ?PHPFUI |
protected PHPFUI |
Methods |
protected testIt(bool $condition, string $token, array $values = []) : string
|
Properties |
private array $errors = []
|
Methods |
private getFieldErrors(?mixed $value, array $validators, array $fieldDefinitions) : array Gets the errors for a value with the record definition and associated validators
|
private validate_alpha(?mixed $value) : string |
private validate_alpha_numeric(?mixed $value) : string |
private validate_bool(?mixed $value) : string |
private validate_card(string $number) : string |
private validate_color(?mixed $value) : string |
private validate_contains(?mixed $value) : string |
private validate_cvv(?mixed $value) : string |
private validate_date(?mixed $value) : string |
private validate_dateISO(?mixed $value) : string |
private validate_datetime(?mixed $value) : string |
private validate_day_month_year(?mixed $value) : string |
private validate_domain(?mixed $value) : string |
private validate_email(?mixed $value) : string |
private validate_ends_with(?mixed $value) : string |
private validate_enum(?mixed $value) : string |
private validate_enum_exact(?mixed $value) : string |
private validate_eq_field(?mixed $value) : string |
private validate_equal(?mixed $value) : string |
private validate_gt_field(?mixed $value) : string |
private validate_gte_field(?mixed $value) : string |
private validate_icontains(?mixed $value) : string |
private validate_iends_with(?mixed $value) : string |
private validate_integer(?mixed $value) : string |
private validate_istarts_with(?mixed $value) : string |
private validate_lt_field(?mixed $value) : string |
private validate_lte_field(?mixed $value) : string |
private validate_maxlength(?mixed $value) : string |
private validate_maxvalue(?mixed $value) : string |
private validate_minlength(?mixed $value) : string |
private validate_minvalue(?mixed $value) : string |
private validate_month_day_year(?mixed $value) : string |
private validate_month_year(?mixed $value) : string |
private validate_neq_field(?mixed $value) : string |
private validate_not_equal(?mixed $value) : string |
private validate_number(?mixed $value) : string |
private validate_required(?mixed $value) : string |
private validate_starts_with(?mixed $value) : string |
private validate_time(?mixed $value) : string |
private validate_unique(?mixed $value) : string |
private validate_url(?mixed $value) : string |
private validate_website(?mixed $value) : string |
private validate_year_month(?mixed $value) : string |
private validateRule(string $validator, ?mixed $value, array $fieldDefinitions) : array Validate one rule.
|
Properties |
public static array $dateSeparators = ['-', '.', '_', ':', '/']
|
public static array $validators = []
|