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.
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.
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().
string
'create'
Indicates that the checking rules to apply are those used for creating entities
string
'delete'
Indicates that the checking rules to apply are those used for deleting entities
string
'update'
Indicates that the checking rules to apply are those used for updating entities
arrayCake\Datasource\RuleInvoker>
The list of rules to check during create operations
arrayCake\Datasource\RuleInvoker>
The list of rules to check during delete operations
array
List of options to pass to every callable rule
arrayCake\Datasource\RuleInvoker>
The list of rules to be checked on both create and update operations
arrayCake\Datasource\RuleInvoker>
The list of rules to check during update operations
bool
Whether to use I18n functions for translating default error messages
Constructor. Takes the options to be passed to all rules.
Utility method for decorating any callable so that if it returns false, the correct property in the entity is marked as invalid.
Used by top level functions checkDelete, checkCreate and checkUpdate, this function iterates an array containing the rules to be checked and checks them all.
Adds a rule that will be applied to the entity both on create and update operations.
Adds a rule that will be applied to the entity on create operations.
Adds a rule that will be applied to the entity on delete operations.
Adds a rule that will be applied to the entity on update operations.
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
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'
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'
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'
__construct(array<string, mixed> $options = [])
Constructor. Takes the options to be passed to all rules.
array<string, mixed>
$options optional The options to pass to every rule
_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.
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.
Cake\Datasource\RuleInvoker
_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.
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.
bool
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.
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.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.
$this
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.
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.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.
$this
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.
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.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.
$this
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.
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.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.
$this
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
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.
bool
InvalidArgumentException
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'
Cake\Datasource\EntityInterface
$entity The entity to check for validity.
array<string, mixed>
$options optional Extra options to pass to checker functions.
bool
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'
Cake\Datasource\EntityInterface
$entity The entity to check for validity.
array<string, mixed>
$options optional Extra options to pass to checker functions.
bool
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'
Cake\Datasource\EntityInterface
$entity The entity to check for validity.
array<string, mixed>
$options optional Extra options to pass to checker functions.
bool
The list of rules to check during create operations
arrayCake\Datasource\RuleInvoker>
The list of rules to check during delete operations
arrayCake\Datasource\RuleInvoker>
List of options to pass to every callable rule
array
The list of rules to be checked on both create and update operations
arrayCake\Datasource\RuleInvoker>
The list of rules to check during update operations
arrayCake\Datasource\RuleInvoker>
Whether to use I18n functions for translating default error messages
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