On this page
Class RulesChecker
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 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().
Constants
-
stringCREATE'create'Indicates that the checking rules to apply are those used for creating entities
-
stringDELETE'delete'Indicates that the checking rules to apply are those used for deleting entities
-
stringUPDATE'update'Indicates that the checking rules to apply are those used for updating entities
Property Summary
-
$_createRules protected
arrayCake\Datasource\RuleInvoker>The list of rules to check during create operations
-
$_deleteRules protected
arrayCake\Datasource\RuleInvoker>The list of rules to check during delete operations
-
$_options protected
arrayList of options to pass to every callable rule
-
$_rules protected
arrayCake\Datasource\RuleInvoker>The list of rules to be checked on both create and update operations
-
$_updateRules protected
arrayCake\Datasource\RuleInvoker>The list of rules to check during update operations
-
$_useI18n protected
boolWhether to use I18n functions for translating default error messages
Method Summary
__construct() public
Constructor. Takes the options to be passed to all rules.
_addError() protected
Utility method for decorating any callable so that if it returns false, the correct property in the entity is marked as invalid.
_checkRules() protected
Used by top level functions checkDelete, checkCreate and checkUpdate, this function iterates an array containing the rules to be checked and checks them all.
add() public
Adds a rule that will be applied to the entity both on create and update operations.
addCreate() public
Adds a rule that will be applied to the entity on create operations.
addDelete() public
Adds a rule that will be applied to the entity on delete operations.
addUpdate() public
Adds a rule that will be applied to the entity on update operations.
check() public
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
checkCreate() public
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'
checkDelete() public
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'
checkUpdate() public
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'
Method Detail
__construct() public
__construct(array<string, mixed> $options = [])
Constructor. Takes the options to be passed to all rules.
Parameters
array<string, mixed>$options optional-
The options to pass to every rule
_addError() protected
_addError(callableCake\Datasource\RuleInvoker $rule, array|string|null $name = null, array<string, mixed> $options = []): Cake\Datasource\RuleInvoker
Utility method for decorating any callable so that if it returns false, the correct property in the entity is marked as invalid.
Parameters
callableCake\Datasource\RuleInvoker$rule-
The rule to decorate
array|string|null$name optional-
The alias for a rule or an array of options
array<string, mixed>$options optional-
The options containing the error message and field.
Returns
Cake\Datasource\RuleInvoker_checkRules() protected
_checkRules(Cake\Datasource\EntityInterface $entity, array<string, mixed> $options = [], arrayCake\Datasource\RuleInvoker> $rules = []): bool
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<string, mixed>$options optional-
Extra options to pass to checker functions.
arrayCake\Datasource\RuleInvoker>$rules optional-
The list of rules that must be checked.
Returns
booladd() public
add(callable $rule, array|string|null $name = null, array<string, mixed> $options = []): $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 toerrorFieldif the rule does not pass.
Parameters
callable$rule-
A callable function or object that will return whether the entity is valid or not.
array|string|null$name optional-
The alias for a rule, or an array of options.
array<string, mixed>$options optional-
List of extra options to pass to the rule callable as second argument.
Returns
$thisaddCreate() public
addCreate(callable $rule, array|string|null $name = null, array<string, mixed> $options = []): $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 toerrorFieldif the rule does not pass.
Parameters
callable$rule-
A callable function or object that will return whether the entity is valid or not.
array|string|null$name optional-
The alias for a rule or an array of options.
array<string, mixed>$options optional-
List of extra options to pass to the rule callable as second argument.
Returns
$thisaddDelete() public
addDelete(callable $rule, array|string|null $name = null, array<string, mixed> $options = []): $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 toerrorFieldif the rule does not pass.
Parameters
callable$rule-
A callable function or object that will return whether the entity is valid or not.
array|string|null$name optional-
The alias for a rule, or an array of options.
array<string, mixed>$options optional-
List of extra options to pass to the rule callable as second argument.
Returns
$thisaddUpdate() public
addUpdate(callable $rule, array|string|null $name = null, array<string, mixed> $options = []): $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 toerrorFieldif the rule does not pass.
Parameters
callable$rule-
A callable function or object that will return whether the entity is valid or not.
array|string|null$name optional-
The alias for a rule, or an array of options.
array<string, mixed>$options optional-
List of extra options to pass to the rule callable as second argument.
Returns
$thischeck() public
check(Cake\Datasource\EntityInterface $entity, string $mode, array<string, mixed> $options = []): bool
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<string, mixed>$options optional-
Extra options to pass to checker functions.
Returns
boolThrows
InvalidArgumentExceptionif an invalid mode is passed.
checkCreate() public
checkCreate(Cake\Datasource\EntityInterface $entity, array<string, mixed> $options = []): bool
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<string, mixed>$options optional-
Extra options to pass to checker functions.
Returns
boolcheckDelete() public
checkDelete(Cake\Datasource\EntityInterface $entity, array<string, mixed> $options = []): bool
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<string, mixed>$options optional-
Extra options to pass to checker functions.
Returns
boolcheckUpdate() public
checkUpdate(Cake\Datasource\EntityInterface $entity, array<string, mixed> $options = []): bool
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<string, mixed>$options optional-
Extra options to pass to checker functions.
Returns
boolProperty Detail
$_createRules protected
The list of rules to check during create operations
Type
arrayCake\Datasource\RuleInvoker>$_deleteRules protected
The list of rules to check during delete operations
Type
arrayCake\Datasource\RuleInvoker>$_options protected
List of options to pass to every callable rule
Type
array$_rules protected
The list of rules to be checked on both create and update operations
Type
arrayCake\Datasource\RuleInvoker>$_updateRules protected
The list of rules to check during update operations
Type
arrayCake\Datasource\RuleInvoker>$_useI18n protected
Whether to use I18n functions for translating default error messages
Type
bool© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.4/class-Cake.Datasource.RulesChecker.html