class Validator (View source)

Constants

MODE_STOP

MODE_THROW

Methods

__construct(array $data, $mode = 0)

No description

static Validator
get(int $mode = 0)

Create a Validator instance using $_GET

static Validator
post(int $mode = 0)

Create a Validator instance using $_POST

setMessage(string $type, string $message)

Configures failure messages of standard rules.

setRule(string $name, string $rule)

Set a new rule or overwrite an existing rule

field(string $path, mixed $var, string|array $rules, mixed $default = null)

Rules can be a string separated by | or an array (use an array for regex: rules, since patterns may contain |).

bool
valid()

Returns True if there are no failures; otherwise, returns False

array<string, string>
failures()

Returns all error messages

getRuleInstance($rule)

Get an instance of an extended rule

Details

__construct(array $data, $mode = 0)

No description

Parameters

array $data
$mode

static Validator get(int $mode = 0)

Create a Validator instance using $_GET

Parameters

int $mode

Return Value

Validator

static Validator post(int $mode = 0)

Create a Validator instance using $_POST

Parameters

int $mode

Return Value

Validator

Validator setMessage(string $type, string $message)

Configures failure messages of standard rules.

Note: To customize the messages for additional rules, use the getRuleInstance() method.

Parameters

string $type
string $message

Return Value

Validator

Validator setRule(string $name, string $rule)

Set a new rule or overwrite an existing rule

Parameters

string $name
string $rule

Return Value

Validator

Validator field(string $path, mixed $var, string|array $rules, mixed $default = null)

Rules can be a string separated by | or an array (use an array for regex: rules, since patterns may contain |).

Supported rules:

  • boolean (On, Yes, True, 1, Off, No, False, and 0)
  • decimal (if valid convert to float)
  • equals:
  • email (validates only the format)
  • in:<a,b,c> (unexpected value)
  • integer (if valid convert to int)
  • max: (value for decimal and int; length for strings)
  • min: (value for decimal and int; length for strings)
  • regex: (not match with the expected format)
  • required (is required)
  • url (a valid url)

Parameters

string $path
mixed $var
string|array $rules
mixed $default

Used when absent/empty and not required.

Return Value

Validator

Exceptions

Exception

bool valid()

Returns True if there are no failures; otherwise, returns False

Return Value

bool

array<string, string> failures()

Returns all error messages

Return Value

array<string, string>

Rule getRuleInstance($rule)

Get an instance of an extended rule

Parameters

$rule

Return Value

Rule

Exceptions

Exception