\Cake\DatasourceRulesChecker

Contains logic for storing and checking rules on entities

RulesCheckers are used by Table classes to ensure that the current entity state satisfies the application logic and business rules.

RulesCheckers afford different rules to be applied in the create and update scenario.

Adding rules

Rules must be callable objects that return true/false depending on whether or not the rule has been satisfied. You can use RulesChecker::add(), RulesChecker::addCreate(), RulesChecker::addUpdate() and RulesChecker::addDelete to add rules to a checker.

Running checks

Generally a Table object will invoke the rules objects, but you can manually invoke the checks by calling RulesChecker::checkCreate(), RulesChecker::checkUpdate() or RulesChecker::checkDelete().

Summary

Methods
Properties
Constants
__construct()
add()
addCreate()
addUpdate()
addDelete()
check()
checkCreate()
checkUpdate()
checkDelete()
No public properties found
CREATE
UPDATE
DELETE
_checkRules()
_addError()
$_rules
$_createRules
$_updateRules
$_deleteRules
$_options
$_useI18n
N/A
No private methods found
No private properties found
N/A

Constants

CREATE

CREATE = 'create' : string

Indicates that the checking rules to apply are those used for creating entities

UPDATE

UPDATE = 'update' : string

Indicates that the checking rules to apply are those used for updating entities

DELETE

DELETE = 'delete' : string

Indicates that the checking rules to apply are those used for deleting entities

Properties

$_rules

$_rules : array<mixed,callable>

The list of rules to be checked on both create and update operations

Type

array<mixed,callable>

$_createRules

$_createRules : array<mixed,callable>

The list of rules to check during create operations

Type

array<mixed,callable>

$_updateRules

$_updateRules : array<mixed,callable>

The list of rules to check during update operations

Type

array<mixed,callable>

$_deleteRules

$_deleteRules : array<mixed,callable>

The list of rules to check during delete operations

Type

array<mixed,callable>

$_options

$_options : array

List of options to pass to every callable rule

Type

array

$_useI18n

$_useI18n : boolean

Whether or not to use I18n functions for translating default error messages

Type

boolean

Methods

__construct()

__construct(array  $options = array()) 

Constructor. Takes the options to be passed to all rules.

Parameters

array $options

The options to pass to every rule

add()

add(callable  $rule, string|null  $name = null, array  $options = array()) : $this

Adds a rule that will be applied to the entity both on create and update operations.

Options

The options array accept the following special keys:

  • errorField: The name of the entity field that will be marked as invalid if the rule does not pass.
  • message: The error message to set to errorField if the rule does not pass.

Parameters

callable $rule

A callable function or object that will return whether the entity is valid or not.

string|null $name

The alias for a rule.

array $options

List of extra options to pass to the rule callable as second argument.

Returns

$this

addCreate()

addCreate(callable  $rule, string|null  $name = null, array  $options = array()) : $this

Adds a rule that will be applied to the entity on create operations.

Options

The options array accept the following special keys:

  • errorField: The name of the entity field that will be marked as invalid if the rule does not pass.
  • message: The error message to set to errorField if the rule does not pass.

Parameters

callable $rule

A callable function or object that will return whether the entity is valid or not.

string|null $name

The alias for a rule.

array $options

List of extra options to pass to the rule callable as second argument.

Returns

$this

addUpdate()

addUpdate(callable  $rule, string|null  $name = null, array  $options = array()) : $this

Adds a rule that will be applied to the entity on update operations.

Options

The options array accept the following special keys:

  • errorField: The name of the entity field that will be marked as invalid if the rule does not pass.
  • message: The error message to set to errorField if the rule does not pass.

Parameters

callable $rule

A callable function or object that will return whether the entity is valid or not.

string|null $name

The alias for a rule.

array $options

List of extra options to pass to the rule callable as second argument.

Returns

$this

addDelete()

addDelete(callable  $rule, string|null  $name = null, array  $options = array()) : $this

Adds a rule that will be applied to the entity on delete operations.

Options

The options array accept the following special keys:

  • errorField: The name of the entity field that will be marked as invalid if the rule does not pass.
  • message: The error message to set to errorField if the rule does not pass.

Parameters

callable $rule

A callable function or object that will return whether the entity is valid or not.

string|null $name

The alias for a rule.

array $options

List of extra options to pass to the rule callable as second argument.

Returns

$this

check()

check(\Cake\Datasource\EntityInterface  $entity, string  $mode, array  $options = array()) : boolean

Runs each of the rules by passing the provided entity and returns true if all of them pass. The rules to be applied are depended on the $mode parameter which can only be RulesChecker::CREATE, RulesChecker::UPDATE or RulesChecker::DELETE

Parameters

\Cake\Datasource\EntityInterface $entity

The entity to check for validity.

string $mode

Either 'create, 'update' or 'delete'.

array $options

Extra options to pass to checker functions.

Throws

\InvalidArgumentException

if an invalid mode is passed.

Returns

boolean

checkCreate()

checkCreate(\Cake\Datasource\EntityInterface  $entity, array  $options = array()) : boolean

Runs each of the rules by passing the provided entity and returns true if all of them pass. The rules selected will be only those specified to be run on 'create'

Parameters

\Cake\Datasource\EntityInterface $entity

The entity to check for validity.

array $options

Extra options to pass to checker functions.

Returns

boolean

checkUpdate()

checkUpdate(\Cake\Datasource\EntityInterface  $entity, array  $options = array()) : boolean

Runs each of the rules by passing the provided entity and returns true if all of them pass. The rules selected will be only those specified to be run on 'update'

Parameters

\Cake\Datasource\EntityInterface $entity

The entity to check for validity.

array $options

Extra options to pass to checker functions.

Returns

boolean

checkDelete()

checkDelete(\Cake\Datasource\EntityInterface  $entity, array  $options = array()) : boolean

Runs each of the rules by passing the provided entity and returns true if all of them pass. The rules selected will be only those specified to be run on 'delete'

Parameters

\Cake\Datasource\EntityInterface $entity

The entity to check for validity.

array $options

Extra options to pass to checker functions.

Returns

boolean

_checkRules()

_checkRules(\Cake\Datasource\EntityInterface  $entity, array  $options = array(), array  $rules = array()) : boolean

Used by top level functions checkDelete, checkCreate and checkUpdate, this function iterates an array containing the rules to be checked and checks them all.

Parameters

\Cake\Datasource\EntityInterface $entity

The entity to check for validity.

array $options

Extra options to pass to checker functions.

array $rules

The list of rules that must be checked.

Returns

boolean

_addError()

_addError(callable  $rule, string  $name, array  $options) : callable

Utility method for decorating any callable so that if it returns false, the correct property in the entity is marked as invalid.

Parameters

callable $rule

The rule to decorate

string $name

The alias for a rule.

array $options

The options containing the error message and field.

Returns

callable