Skip to content

Latest commit

 

History

History
135 lines (114 loc) · 6.65 KB

7. Validation.md

File metadata and controls

135 lines (114 loc) · 6.65 KB

PHPFUI\ORM Validation

Note: Referenced namespaces in this document refer to the PHPFUI\ORM defaults.

Validator is an abstract class for \App\Record validation See \PHPFUI\ORM\Validator 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
email 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 identifiers
Example: enum:Get,Post,Put,Delete
enum_exact MySQL enum value, case sensitive comma separated list of identifiers
Example: 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
  {
  /** @var array<string, string[]> */
  public static array $validators = [
    'order_date' => ['required', 'maxlength', 'datetime', 'minvalue:2000-01-01', 'maxvalue:2099-12-31'],
    ];
  }