RulesValidatorTrait
Use validator functions to validate properties.
Tags
Table of Contents
Methods
- getValidator() : RulesValidatorInterface
- rules() : array<string|int, mixed>
- Specify validators to run across the object properties.
- valid() : array<string|int, mixed>|true
- Validate this scenario (or default).
- validate() : void
- Validate this scenario (or default).
Methods
getValidator()
public
getValidator() : RulesValidatorInterface
Return values
RulesValidatorInterfacerules()
Specify validators to run across the object properties.
public
abstract rules([string|null $scenario = null ]) : array<string|int, mixed>
The syntax is as follow:
// A built-in rule from the 'Validity' class.
'rule-name' => ['field1', 'field2']
// A custom validity class.
'exotic-rule' => ['field1', 'field2'],
'validity' => MyValidity::class,
// Specifying rule arguments.
'rule-name' => ['field1', 'field2', 'args' => [0, 10]]
// A custom inline validator.
'rule-name' => ['field1', 'field2', 'func' => 'trim']
// Specify different args per field.
'rule-name' => [
['field1', 'args' => [0, 10]],
['field2', 'args' => [0, 25]],
]
Special rules exist for 'validity' and 'required':
The 'validity' rule will switch out the Validity helper class for a custom one. It's probably best to inherit the built-in Validity class so you don't break any inherited rules.
'validity' => Validity:class
The 'required' rule cannot accept 'args' or 'func'.
'required' => ['field1', 'field2']
Custom inline validators will ignore the 'rule-name'. You can instead use this as a sort of ID to reuse the in inherited classes.
// Parent class.
'my-rule' => ['field1', 'func' => 'trim']
// Child class.
$rules = parent::rules($scenario);
$rules['my-rule'][] = 'field2';
Refer to the Validity class for more about validator functions.
Parameters
- $scenario : string|null = null
Return values
array<string|int, mixed>valid()
Validate this scenario (or default).
public
valid([string|null $scenario = null ]) : array<string|int, mixed>|true
Parameters
- $scenario : string|null = null
-
null for default.
Tags
Return values
array<string|int, mixed>|true —True if valid, errors array if invalid.
validate()
Validate this scenario (or default).
public
validate([string|null $scenario = null ]) : void
This will throw if invalid or pass silently if valid.
Parameters
- $scenario : string|null = null
-
null for default.