ORM flavoured rules checker.
Adds ORM related features to the RulesChecker class.
string
'create'
string
'delete'
string
'update'
\Cake\Datasource\RuleInvoker[]
The list of rules to check during create operations
\Cake\Datasource\RuleInvoker[]
The list of rules to check during delete operations
array
List of options to pass to every callable rule
\Cake\Datasource\RuleInvoker[]
The list of rules to be checked on both create and update operations
\Cake\Datasource\RuleInvoker[]
The list of rules to check during update operations
bool
Whether or not to use I18n functions for translating default error messages
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.
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'
Returns a callable that can be used as a rule for checking that the values extracted from the entity to check exist as the primary key in another table.
Returns a callable that can be used as a rule for checking the uniqueness of a value in the table.
__construct(array $options)
Constructor. Takes the options to be passed to all rules.
array
$options optional The options to pass to every rule
_addError(callable $rule, mixed $name, array $options)
Utility method for decorating any callable so that if it returns false, the correct property in the entity is marked as invalid.
callable
$rule The rule to decorate
string|array|null
$name optional The alias for a rule or an array of options
array
$options optional The options containing the error message and field.
\Cake\Datasource\RuleInvoker
_addLinkConstraintRule(mixed $association, ?string $errorField, ?string $message, string $linkStatus, string $ruleName)
Adds a link constraint rule.
\Cake\ORM\Association|string
$association The association to check for links.
string|null
$errorField The name of the property to use for setting possible errors. When absent, the name is inferred from $association
.
string|null
$message The error message to show in case the rule does not pass.
string
$linkStatus The ink status required for the check to pass.
string
$ruleName The alias/name of the rule.
\Cake\Datasource\RuleInvoker
InvalidArgumentException
_checkRules(\Cake\Datasource\EntityInterface $entity, array $options, array $rules)
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
$options optional Extra options to pass to checker functions.
\Cake\Datasource\RuleInvoker[]
$rules optional The list of rules that must be checked.
bool
add(callable $rule, mixed $name, array $options)
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.
string|array|null
$name optional The alias for a rule, or an array of options.
array
$options optional List of extra options to pass to the rule callable as second argument.
$this
addCreate(callable $rule, mixed $name, array $options)
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.
string|array|null
$name optional The alias for a rule or an array of options.
array
$options optional List of extra options to pass to the rule callable as second argument.
$this
addDelete(callable $rule, mixed $name, array $options)
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.
string|array|null
$name optional The alias for a rule, or an array of options.
array
$options optional List of extra options to pass to the rule callable as second argument.
$this
addUpdate(callable $rule, mixed $name, array $options)
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.
string|array|null
$name optional The alias for a rule, or an array of options.
array
$options optional List of extra options to pass to the rule callable as second argument.
$this
check(\Cake\Datasource\EntityInterface $entity, string $mode, array $options)
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
$options optional Extra options to pass to checker functions.
bool
InvalidArgumentException
checkCreate(\Cake\Datasource\EntityInterface $entity, array $options)
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
$options optional Extra options to pass to checker functions.
bool
checkDelete(\Cake\Datasource\EntityInterface $entity, array $options)
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
$options optional Extra options to pass to checker functions.
bool
checkUpdate(\Cake\Datasource\EntityInterface $entity, array $options)
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
$options optional Extra options to pass to checker functions.
bool
existsIn(mixed $field, mixed $table, mixed $message)
Returns a callable that can be used as a rule for checking that the values extracted from the entity to check exist as the primary key in another table.
This is useful for enforcing foreign key integrity checks.
$rules->add($rules->existsIn('author_id', 'Authors', 'Invalid Author')); $rules->add($rules->existsIn('site_id', new SitesTable(), 'Invalid Site'));
Available $options are error 'message' and 'allowNullableNulls' flag. 'message' sets a custom error message. Set 'allowNullableNulls' to true to accept composite foreign keys where one or more nullable columns are null.
string|string[]
$field The field or list of fields to check for existence by primary key lookup in the other table.
\Cake\ORM\Table|\Cake\ORM\Association|string
$table The table name where the fields existence will be checked.
string|array|null
$message optional The error message to show in case the rule does not pass. Can also be an array of options. When an array, the 'message' key can be used to provide a message.
\Cake\Datasource\RuleInvoker
isLinkedTo(mixed $association, ?string $field, ?string $message)
Validates whether links to the given association exist.
$rules->addUpdate($rules->isLinkedTo('Articles', 'article'));
On a Comments
table that has a belongsTo Articles
association, this check would ensure that comments can only be edited as long as they are associated to an existing article.
\Cake\ORM\Association|string
$association The association to check for links.
string|null
$field optional The name of the association property. When supplied, this is the name used to set possible errors. When absent, the name is inferred from $association
.
string|null
$message optional The error message to show in case the rule does not pass.
\Cake\Datasource\RuleInvoker
isNotLinkedTo(mixed $association, ?string $field, ?string $message)
Validates whether links to the given association do not exist.
$rules->addDelete($rules->isNotLinkedTo('Comments', 'comments'));
On a Articles
table that has a hasMany Comments
association, this check would ensure that articles can only be deleted when no associated comments exist.
\Cake\ORM\Association|string
$association The association to check for links.
string|null
$field optional The name of the association property. When supplied, this is the name used to set possible errors. When absent, the name is inferred from $association
.
string|null
$message optional The error message to show in case the rule does not pass.
\Cake\Datasource\RuleInvoker
isUnique(array $fields, mixed $message)
Returns a callable that can be used as a rule for checking the uniqueness of a value in the table.
$rules->add($rules->isUnique(['email'], 'The email should be unique'));
string[]
$fields The list of fields to check for uniqueness.
string|array|null
$message optional The error message to show in case the rule does not pass. Can also be an array of options. When an array, the 'message' key can be used to provide a message.
\Cake\Datasource\RuleInvoker
validCount(string $field, int $count, string $operator, ?string $message)
Validates the count of associated records.
string
$field The field to check the count on.
int
$count optional The expected count.
string
$operator optional The operator for the count comparison.
string|null
$message optional The error message to show in case the rule does not pass.
\Cake\Datasource\RuleInvoker
The list of rules to check during create operations
\Cake\Datasource\RuleInvoker[]
The list of rules to check during delete operations
\Cake\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
\Cake\Datasource\RuleInvoker[]
The list of rules to check during update operations
\Cake\Datasource\RuleInvoker[]
Whether or not 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.1/class-Cake.ORM.RulesChecker.html