Validator object encapsulates all methods related to data validations for a model It also provides an API to dynamically change validation rules for each model field.
Implements ArrayAccess to easily modify rules in the set
__debugInfo( )
Get the printable version of this object.
array
_canBeEmpty( Cake\Validation\ValidationSet $field , array $context )
Returns whether the field can be left blank according to allowEmpty
Cake\Validation\ValidationSet
$field
$context
boolean
_checkPresence( Cake\Validation\ValidationSet $field , array $context )
Returns false if any validation for the passed rule set should be stopped due to the field missing in the data array
Cake\Validation\ValidationSet
$field
$context
boolean
_fieldIsEmpty( mixed $data )
Returns true if the field is empty in the passed data array
$data
boolean
_processRules( string $field , Cake\Validation\ValidationSet $rules , array $data , boolean $newRecord )
Iterates over each rule in the validation set and collects the errors resulting from executing them
$field
Cake\Validation\ValidationSet
$rules
$data
$newRecord
array
add( string $field , array|string $name , array|Cake\Validation\ValidationRule $rule [] )
Adds a new rule to a field's rule set. If second argument is an array then rules list for the field will be replaced with second argument and third argument will be ignored.
$validator ->add('title', 'required', ['rule' => 'notBlank']) ->add('user_id', 'valid', ['rule' => 'numeric', 'message' => 'Invalid User']) $validator->add('password', [ 'size' => ['rule' => ['lengthBetween', 8, 20]], 'hasSpecialCharacter' => ['rule' => 'validateSpecialchar', 'message' => 'not valid'] ]);
$field
$name
Cake\Validation\ValidationRule
$rule
optional [] mixed
addNested( string $field , Cake\Validation\Validator $validator )
Adds a nested validator.
Nesting validators allows you to define validators for array types. For example, nested validators are ideal when you want to validate a sub-document, or complex array type.
This method assumes that the sub-document has a 1:1 relationship with the parent.
The providers of the parent validator will be synced into the nested validator, when errors are checked. This ensures that any validation rule providers connected in the parent will have the same values in the nested validator when rules are evaluated.
$field
Cake\Validation\Validator
$validator
mixed
addNestedMany( string $field , Cake\Validation\Validator $validator )
Adds a nested validator.
Nesting validators allows you to define validators for array types. For example, nested validators are ideal when you want to validate many similar sub-documents or complex array types.
This method assumes that the sub-document has a 1:N relationship with the parent.
The providers of the parent validator will be synced into the nested validator, when errors are checked. This ensures that any validation rule providers connected in the parent will have the same values in the nested validator when rules are evaluated.
$field
Cake\Validation\Validator
$validator
mixed
allowEmpty( string $field , boolean|string|callable $when true )
Allows a field to be empty.
This is the opposite of notEmpty() which requires a field to not be empty. By using $mode equal to 'create' or 'update', you can allow fields to be empty when records are first created, or when they are updated.
$validator->allowEmpty('email'); // Email can be empty $validator->allowEmpty('email', 'create'); // Email can be empty on create $validator->allowEmpty('email', 'update'); // Email can be empty on update
It is possible to conditionally allow emptiness on a field by passing a callback as a second argument. The callback will receive the validation context array as argument:
$validator->allowEmpty('email', function ($context) { return !$context['newRecord'] || $context['data']['role'] === 'admin'; });
This method will correctly detect empty file uploads and date/time/datetime fields.
Because this and notEmpty()
modify the same internal state, the last method called will take precedence.
$field
$when
optional true mixed
count( )
Returns the number of fields having validation rules
integer
Countable::count()
errors( array $data , boolean $newRecord true )
Returns an array of fields that have failed validation. On the current model. This method will actually run validation rules over data, not just return the messages.
$data
$newRecord
optional true array
field( string $name , Cake\Validation\ValidationSet $set null )
Returns a ValidationSet object containing all validation rules for a field, if passed a ValidationSet as second argument, it will replace any other rule set defined before
$name
Cake\Validation\ValidationSet
$set
optional null Cake\Validation\ValidationSet
getIterator( )
Returns an iterator for each of the fields to be validated
ArrayIterator
IteratorAggregate::getIterator()
hasField( string $name )
Check whether or not a validator contains any rules for the given field.
$name
boolean
isEmptyAllowed( string $field , boolean $newRecord )
Returns whether or not a field can be left empty for a new or already existing record.
$field
$newRecord
boolean
isPresenceRequired( string $field , boolean $newRecord )
Returns whether or not a field can be left out for a new or already existing record.
$field
$newRecord
boolean
notEmpty( string $field , string $message null , boolean|string|callable $when false )
Sets a field to require a non-empty value.
This is the opposite of allowEmpty() which allows a field to be empty. By using $mode equal to 'create' or 'update', you can make fields required when records are first created, or when they are updated.
$message = 'This field cannot be empty'; $validator->notEmpty('email'); // Email cannot be empty $validator->notEmpty('email', $message, 'create'); // Email can be empty on update $validator->notEmpty('email', $message, 'update'); // Email can be empty on create
It is possible to conditionally disallow emptiness on a field by passing a callback as the third argument. The callback will receive the validation context array as argument:
$validator->notEmpty('email', 'Email is required', function ($context) { return $context['newRecord'] && $context['data']['role'] !== 'admin'; });
Because this and allowEmpty()
modify the same internal state, the last method called will take precedence.
$field
$message
optional null $when
optional false mixed
offsetExists( string $field )
Returns whether a rule set is defined for a field or not
$field
boolean
ArrayAccess::offsetExists()
offsetGet( string $field )
Returns the rule set for a field
$field
Cake\Validation\ValidationSet
ArrayAccess::offsetGet()
offsetSet( string $field , array|Cake\Validation\ValidationSet $rules )
Sets the rule set for a field
$field
Cake\Validation\ValidationSet
$rules
ArrayAccess::offsetSet()
offsetUnset( string $field )
Unsets the rule set for a field
$field
ArrayAccess::offsetUnset()
provider( string $name , null|object|string $object null )
Associates an object to a name so it can be used as a provider. Providers are objects or class names that can contain methods used during validation of for deciding whether a validation rule can be applied. All validation methods, when called will receive the full list of providers stored in this validator.
If called with no arguments, it will return the provider stored under that name if it exists, otherwise it returns this instance of chaining.
$name
$object
optional null mixed
remove( string $field , string|null $rule null )
Removes a rule from the set by its name
$validator ->remove('title', 'required') ->remove('user_id')
$field
$rule
optional null mixed
requirePresence( string $field , boolean|string|callable $mode true , string|null $message null )
Sets whether a field is required to be present in data array.
$field
$mode
optional true $message
optional null mixed
string | NESTED Used to flag nested rules created with addNested() and addNestedMany() | '_nested' |
© 2005–2016 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.
http://api.cakephp.org/3.1/class-Cake.Validation.Validator.html