ORM flavoured rules checker.
Adds ORM related features to the RulesChecker class.
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.
existsIn( string|array $field , object|string $table , string|array|null $message null )
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.
$field The field or list of fields to check for existence by primary key lookup in the other table.
$table $message optional null 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.
isUnique( array $fields , string|array|null $message null )
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'));
$fields $message optional null 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.
validCount( string $field , integer $count 0 , string $operator '>' , string|null $message null )
Validates the count of associated records.
$field $count optional 0 $operator optional '>' $message optional null __construct( array $options [] )
Constructor. Takes the options to be passed to all rules.
$options optional [] _addError( callable $rule , string $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.
$rule $name $options _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 $options optional [] $rules optional [] add( callable $rule , string|null $name null , 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.$rule A callable function or object that will return whether the entity is valid or not.
$name optional null $options optional [] List of extra options to pass to the rule callable as second argument.
addCreate( callable $rule , string|null $name null , 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.$rule A callable function or object that will return whether the entity is valid or not.
$name optional null $options optional [] List of extra options to pass to the rule callable as second argument.
addDelete( callable $rule , string|null $name null , 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.$rule A callable function or object that will return whether the entity is valid or not.
$name optional null $options optional [] List of extra options to pass to the rule callable as second argument.
addUpdate( callable $rule , string|null $name null , 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.$rule A callable function or object that will return whether the entity is valid or not.
$name optional null $options optional [] List of extra options to pass to the rule callable as second argument.
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 $mode $options optional [] 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 $options optional [] 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 $options optional [] 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 $options optional []
© 2005–2017 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/3.4/class-Cake.ORM.RulesChecker.html