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
integer
EMPTY_ALL self::EMPTY_STRING|self::EMPTY_ARRAY|self::EMPTY_FILE|self::EMPTY_DATE|self::EMPTY_TIME integer
EMPTY_ARRAY 2 integer
EMPTY_DATE 8 integer
EMPTY_FILE 4 integer
EMPTY_STRING 1 integer
EMPTY_TIME 16 string
NESTED '_nested' $_allowEmptyFlags protected array$_allowEmptyMessages protected arrayContains the validation messages associated with checking the emptiness for each corresponding field.
array$_fields protected array$_presenceMessages protected arrayContains the validation messages associated with checking the presence for each corresponding field.
$_providers protected arrayAn associative array of objects or classes containing methods used for validation
$_useI18n protected booleanallowEmpty Returns false if any validation for the passed rule set should be stopped due to the field missing in the data array
Iterates over each rule in the validation set and collects the errors resulting from executing them
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.
Allows a field to be empty. You can also pass array. Using an array will let you provide the following keys:
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.
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
Add a validation rule to ensure that a field is an array containing at least the specified amount of elements
Add a validation rule to ensure that a field is an array containing at most the specified amount of elements
Returns whether or not a field can be left empty for a new or already existing record.
Returns whether or not a field can be left out for a new or already existing record.
Sets a field to require a non-empty value. You can also pass array. Using an array will let you provide the following keys:
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.
Sets whether a field is required to be present in data array. You can also pass array. Using an array will let you provide the following keys:
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.
Compatibility shim for the allowEmpty* methods that enable us to support both the $when, $message signature (deprecated) and the $message, $when format which is preferred.
_canBeEmpty( Cake\Validation\ValidationSet $field , array $context )
Returns whether the field can be left blank according to allowEmpty
Cake\Validation\ValidationSet $field $context _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 _convertValidatorToArray( integer|string $fieldName , array $defaults = [] , string|array $settings = [] )
Converts validator to fieldName => $settings array
$fieldName $defaults optional [] $settings optional [] _fieldIsEmpty( mixed $data )
Returns true if the field is empty in the passed data array
$data _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 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 [] addDefaultProvider( string $name , object|string $object )
Associates an object to a name so it can be used as a default provider.
$name $object addNested( string $field , Cake\Validation\Validator $validator , string|null $message = null , string|callable|null $when = null )
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 $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
addNestedMany( string $field , Cake\Validation\Validator $validator , string|null $message = null , string|callable|null $when = null )
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 $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
allowEmpty( string|array $field , boolean|string|callable $when = true , string|null $message = null )
Allows a field to be empty. You can also pass array. Using an array will let you provide the following keys:
when individual when condition for fieldYou can also set when and message for all passed fields, the individual setting takes precedence over group settings.
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.
// Email can be empty
$validator->allowEmpty('email');
// Email can be empty on create
$validator->allowEmpty('email', 'create');
// Email can be empty on update
$validator->allowEmpty('email', 'update');
// Email and subject can be empty on update
$validator->allowEmpty(['email', 'subject'], 'update');
// Email can be always empty, subject and content can be empty on update.
$validator->allowEmpty(
[
'email' => [
'when' => true
],
'content' => [
'message' => 'Content cannot be empty'
],
'subject'
],
'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.
3.7.0 Use allowEmptyString(), allowEmptyArray(), allowEmptyFile(), allowEmptyDate(), allowEmptyTime() or allowEmptyDateTime() instead.
$field $when optional true Indicates when the field is allowed to be empty Valid values are true (always), 'create', 'update'. If a callable is passed then the field will allowed to be empty only when the callback returns true.
$message optional null allowEmptyArray( string $field , string|null $message = null , boolean|string|callable $when = true )
Allows a field to be an empty array.
This method is equivalent to calling allowEmptyFor() with EMPTY_STRING + EMPTY_ARRAY flags.
$field $message optional null $when optional true Indicates when the field is allowed to be empty Valid values are true, false, 'create', 'update'. If a callable is passed then the field will allowed to be empty only when the callback returns true.
allowEmptyDate( string $field , string|null $message = null , boolean|string|callable $when = true )
Allows a field to be an empty date.
Empty date values are null, '', [] and arrays where all values are '' and the year key is present.
$field $message optional null $when optional true Indicates when the field is allowed to be empty Valid values are true, false, 'create', 'update'. If a callable is passed then the field will allowed to be empty only when the callback returns true.
allowEmptyDateTime( string $field , string|null $message = null , boolean|string|callable $when = true )
Allows a field to be an empty date/time.
Empty date values are null, '', [] and arrays where all values are '' and the year and hour keys are present.
This method is equivalent to calling allowEmptyFor() with EMPTY_STRING + EMPTY_DATE + EMPTY_TIME flags.
$field $message optional null $when optional true Indicates when the field is allowed to be empty Valid values are true, false, 'create', 'update'. If a callable is passed then the field will allowed to be empty only when the callback returns false.
allowEmptyFile( string $field , string|null $message = null , boolean|string|callable $when = true )
Allows a field to be an empty file.
This method is equivalent to calling allowEmptyFor() with EMPTY_FILE flag. File fields will not accept '', or [] as empty values. Only null and a file upload with error equal to UPLOAD_ERR_NO_FILE will be treated as empty.
$field $message optional null $when optional true Indicates when the field is allowed to be empty Valid values are true, 'create', 'update'. If a callable is passed then the field will allowed to be empty only when the callback returns true.
allowEmptyFor( string $field , integer|null $flags , boolean|string|callable $when = true , string|null $message = null )
Low-level method to indicate that a field can be empty.
This method should generally not be used and instead you should use:
allowEmptyString()allowEmptyArray()allowEmptyFile()allowEmptyDate()allowEmptyDatetime()allowEmptyTime()Should be used as their APIs are simpler to operate and read.
You can also set flags, when and message for all passed fields, the individual setting takes precedence over group settings.
// Email can be empty
$validator->allowEmptyFor('email', Validator::EMPTY_STRING);
// Email can be empty on create
$validator->allowEmptyFor('email', Validator::EMPTY_STRING, 'create');
// Email can be empty on update
$validator->allowEmptyFor('email', Validator::EMPTY_STRING, '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', Validator::EMPTY_STRING, function ($context) {
return !$context['newRecord'] || $context['data']['role'] === 'admin';
}); If you want to allow other kind of empty data on a field, you need to pass other flags:
$validator->allowEmptyFor('photo', Validator::EMPTY_FILE);
$validator->allowEmptyFor('published', Validator::EMPTY_STRING | Validator::EMPTY_DATE | Validator::EMPTY_TIME);
$validator->allowEmptyFor('items', Validator::EMPTY_STRING | Validator::EMPTY_ARRAY); You can also use convenience wrappers of this method. The following calls are the same as above:
$validator->allowEmptyFile('photo');
$validator->allowEmptyDateTime('published');
$validator->allowEmptyArray('items'); $field $flags $when optional true Indicates when the field is allowed to be empty Valid values are true, false, 'create', 'update'. If a callable is passed then the field will allowed to be empty only when the callback returns true.
$message optional null allowEmptyString( string $field , string|null $message = null , boolean|string|callable $when = true )
Allows a field to be an empty string.
This method is equivalent to calling allowEmptyFor() with EMPTY_STRING flag.
$field $message optional null $when optional true Indicates when the field is allowed to be empty Valid values are true, false, 'create', 'update'. If a callable is passed then the field will allowed to be empty only when the callback returns true.
allowEmptyTime( string $field , string|null $message = null , boolean|string|callable $when = true )
Allows a field to be an empty time.
Empty date values are null, '', [] and arrays where all values are '' and the hour key is present.
This method is equivalent to calling allowEmptyFor() with EMPTY_STRING + EMPTY_TIME flags.
$field $message optional null $when optional true Indicates when the field is allowed to be empty Valid values are true, false, 'create', 'update'. If a callable is passed then the field will allowed to be empty only when the callback returns true.
alphaNumeric( string $field , string|null $message = null , string|callable|null $when = null )
Add an alphanumeric rule to a field.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
ascii( string $field , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure a field contains only ascii bytes
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
boolean( string $field , string|null $message = null , string|callable|null $when = null )
Add a boolean validation rule to a field.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
containsNonAlphaNumeric( string $field , integer $limit = 1 , string|null $message = null , string|callable|null $when = null )
Add a rule to check if a field contains non alpha numeric characters.
$field $limit optional 1 $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
count( )
Returns the number of fields having validation rules
Countable::count() creditCard( string $field , string $type = 'all' , string|null $message = null , string|callable|null $when = null )
Add a credit card rule to a field.
$field $type optional 'all' The type of cards you want to allow. Defaults to 'all'. You can also supply an array of accepted card types. e.g ['mastercard', 'visa', 'amex']
$message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
date( string $field , array $formats = ['ymd'] , string|null $message = null , string|callable|null $when = null )
Add a date format validation rule to a field.
$field $formats optional ['ymd'] $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
dateTime( string $field , array $formats = ['ymd'] , string|null $message = null , string|callable|null $when = null )
Add a date time format validation rule to a field.
$field $formats optional ['ymd'] $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
decimal( string $field , integer|null $places = null , string|null $message = null , string|callable|null $when = null )
Add a decimal validation rule to a field.
$field $places optional null $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
email( string $field , boolean $checkMX = false , string|null $message = null , string|callable|null $when = null )
Add an email validation rule to a field.
$field $checkMX optional false $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
equalToField( string $field , string $secondField , string|null $message = null , string|callable|null $when = null )
Add a rule to compare one field is equal to another.
$field $secondField $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
equals( string $field , integer|float $value , string|null $message = null , string|callable|null $when = null )
Add a equal to comparison rule to a field.
$field $value $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
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 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\ValidationSetgetDefaultProvider( string $name )
Returns the default provider stored under that name if it exists.
$name getDefaultProviders( )
Get the list of default providers.
getIterator( )
Returns an iterator for each of the fields to be validated
IteratorAggregate::getIterator() getNotEmptyMessage( string $field )
Gets the notEmpty message for a field
$field getProvider( string $name )
Returns the provider stored under that name if it exists.
$name getRequiredMessage( string $field )
Gets the required message for a field
$field greaterThan( string $field , integer|float $value , string|null $message = null , string|callable|null $when = null )
Add a greater than comparison rule to a field.
$field $value $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
greaterThanField( string $field , string $secondField , string|null $message = null , string|callable|null $when = null )
Add a rule to compare one field is greater than another.
$field $secondField $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
greaterThanOrEqual( string $field , integer|float $value , string|null $message = null , string|callable|null $when = null )
Add a greater than or equal to comparison rule to a field.
$field $value $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
greaterThanOrEqualToField( string $field , string $secondField , string|null $message = null , string|callable|null $when = null )
Add a rule to compare one field is greater than or equal to another.
$field $secondField $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
hasAtLeast( string $field , integer $count , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure that a field is an array containing at least the specified amount of elements
$field $count $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
hasAtMost( string $field , integer $count , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure that a field is an array containing at most the specified amount of elements
$field $count $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
hasField( string $name )
Check whether or not a validator contains any rules for the given field.
$name hexColor( string $field , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure a field is a 6 digits hex color value.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
inList( string $field , array $list , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure the field value is within a whitelist.
$field $list $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
integer( string $field , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure a field is an integer value.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
invertWhenClause( boolean|string|callable $when )
Invert a when clause for creating notEmpty rules
$when Indicates when the field is not allowed to be empty. Valid values are true (always), 'create', 'update'. If a callable is passed then the field will allowed to be empty only when the callback returns false.
ip( string $field , string|null $message = null , string|callable|null $when = null )
Add an IP validation rule to a field.
This rule will accept both IPv4 and IPv6 addresses.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
ipv4( string $field , string|null $message = null , string|callable|null $when = null )
Add an IPv4 validation rule to a field.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
ipv6( string $field , string|null $message = null , string|callable|null $when = null )
Add an IPv6 validation rule to a field.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
isArray( string $field , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure that a field contains an array.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
isEmpty( mixed $data , integer $flags )
Returns true if the field is empty in the passed data array
$data $flags isEmptyAllowed( string $field , boolean $newRecord )
Returns whether or not a field can be left empty for a new or already existing record.
$field $newRecord isPresenceRequired( string $field , boolean $newRecord )
Returns whether or not a field can be left out for a new or already existing record.
$field $newRecord latLong( string $field , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure the field is a lat/long tuple.
e.g. <lat>, <lng>
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
latitude( string $field , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure the field is a latitude.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
lengthBetween( string $field , array $range , string|null $message = null , string|callable|null $when = null )
Add an rule that ensures a string length is within a range.
$field $range $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
lessThan( string $field , integer|float $value , string|null $message = null , string|callable|null $when = null )
Add a less than comparison rule to a field.
$field $value $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
lessThanField( string $field , string $secondField , string|null $message = null , string|callable|null $when = null )
Add a rule to compare one field is less than another.
$field $secondField $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
lessThanOrEqual( string $field , integer|float $value , string|null $message = null , string|callable|null $when = null )
Add a less than or equal comparison rule to a field.
$field $value $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
lessThanOrEqualToField( string $field , string $secondField , string|null $message = null , string|callable|null $when = null )
Add a rule to compare one field is less than or equal to another.
$field $secondField $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
localizedTime( string $field , string $type = 'datetime' , string|null $message = null , string|callable|null $when = null )
Add a localized time, date or datetime format validation rule to a field.
$field $type optional 'datetime' $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
longitude( string $field , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure the field is a longitude.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
maxLength( string $field , integer $max , string|null $message = null , string|callable|null $when = null )
Add a string length validation rule to a field.
$field $max $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
maxLengthBytes( string $field , integer $max , string|null $message = null , string|callable|null $when = null )
Add a string length validation rule to a field.
$field $max $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
minLength( string $field , integer $min , string|null $message = null , string|callable|null $when = null )
Add a string length validation rule to a field.
$field $min $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
minLengthBytes( string $field , integer $min , string|null $message = null , string|callable|null $when = null )
Add a string length validation rule to a field.
$field $min $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
multipleOptions( string $field , array $options = [] , string|null $message = null , string|callable|null $when = null )
Add a validation rule for a multiple select. Comparison is case sensitive by default.
$field $options optional [] The options for the validator. Includes the options defined in \Cake\Validation\Validation::multiple() and the caseInsensitive parameter.
$message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
naturalNumber( string $field , string|null $message = null , string|callable|null $when = null )
Add a natural number validation rule to a field.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
nonNegativeInteger( string $field , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure a field is a non negative integer.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
notBlank( string $field , string|null $message = null , string|callable|null $when = null )
Add a notBlank rule to a field.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
notEmpty( string|array $field , string|null $message = null , boolean|string|callable $when = false )
Sets a field to require a non-empty value. You can also pass array. Using an array will let you provide the following keys:
when individual when condition for fieldmessage individual error message for fieldYou can also set when and message for all passed fields, the individual setting takes precedence over group settings.
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';
// Email cannot be empty
$validator->notEmpty('email');
// Email can be empty on update, but not create
$validator->notEmpty('email', $message, 'create');
// Email can be empty on create, but required on update.
$validator->notEmpty('email', $message, 'update');
// Email and title can be empty on create, but are required on update.
$validator->notEmpty(['email', 'title'], $message, 'update');
// Email can be empty on create, title must always be not empty
$validator->notEmpty(
[
'email',
'title' => [
'when' => true,
'message' => 'Title cannot be empty'
]
],
$message,
'update'
); 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.
3.7.0 Use notEmptyString(), notEmptyArray(), notEmptyFile(), notEmptyDate(), notEmptyTime() or notEmptyDateTime() instead.
$field $message optional null $when optional false Indicates when the field is not allowed to be empty. Valid values are true (always), 'create', 'update'. If a callable is passed then the field will allowed to be empty only when the callback returns false.
notEmptyArray( string $field , string|null $message = null , boolean|string|callable $when = false )
Require a field to be a non-empty array
Opposite to allowEmptyArray()
$field $message optional null $when optional false Indicates when the field is not allowed to be empty. Valid values are false (never), 'create', 'update'. If a callable is passed then the field will be required to be not empty when the callback returns true.
notEmptyDate( string $field , string|null $message = null , boolean|string|callable $when = false )
Require a non-empty date value
$field $message optional null $when optional false Indicates when the field is not allowed to be empty. Valid values are false (never), 'create', 'update'. If a callable is passed then the field will be required to be not empty when the callback returns true.
notEmptyDateTime( string $field , string|null $message = null , boolean|string|callable $when = false )
Require a field to be a non empty date/time.
Opposite to allowEmptyDateTime
$field $message optional null $when optional false Indicates when the field is not allowed to be empty. Valid values are false (never), 'create', 'update'. If a callable is passed then the field will be required to be not empty when the callback returns true.
notEmptyFile( string $field , string|null $message = null , boolean|string|callable $when = false )
Require a field to be a not-empty file.
Opposite to allowEmptyFile()
$field $message optional null $when optional false Indicates when the field is not allowed to be empty. Valid values are false (never), 'create', 'update'. If a callable is passed then the field will be required to be not empty when the callback returns true.
notEmptyString( string $field , string|null $message = null , boolean|string|callable $when = false )
Requires a field to be not be an empty string.
Opposite to allowEmptyString()
$field $message optional null $when optional false Indicates when the field is not allowed to be empty. Valid values are false (never), 'create', 'update'. If a callable is passed then the field will be required to be not empty when the callback returns true.
notEmptyTime( string $field , string|null $message = null , boolean|string|callable $when = false )
Require a field to be a non-empty time.
Opposite to allowEmptyTime()
$field $message optional null $when optional false Indicates when the field is not allowed to be empty. Valid values are false (never), 'create', 'update'. If a callable is passed then the field will be required to be not empty when the callback returns true.
notEqualToField( string $field , string $secondField , string|null $message = null , string|callable|null $when = null )
Add a rule to compare one field is not equal to another.
$field $secondField $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
notEquals( string $field , integer|float $value , string|null $message = null , string|callable|null $when = null )
Add a not equal to comparison rule to a field.
$field $value $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
notSameAs( string $field , string $secondField , string|null $message = null , string|callable|null $when = null )
Add a rule to compare that two fields have different values.
$field $secondField $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
numeric( string $field , string|null $message = null , string|callable|null $when = null )
Add a numeric value validation rule to a field.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
offsetExists( string $field )
Returns whether a rule set is defined for a field or not
$field ArrayAccess::offsetExists() offsetGet( string $field )
Returns the rule set for a field
$field Cake\Validation\ValidationSetArrayAccess::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 range( string $field , array $range , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure a field is within a numeric range
$field $range $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
regex( string $field , string $regex , string|null $message = null , string|callable|null $when = null )
Returns whether or not a field matches against a regular expression.
$field $regex $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
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 requirePresence( string|array $field , boolean|string|callable $mode = true , string|null $message = null )
Sets whether a field is required to be present in data array. You can also pass array. Using an array will let you provide the following keys:
mode individual mode for fieldmessage individual error message for fieldYou can also set mode and message for all passed fields, the individual setting takes precedence over group settings.
$field $mode optional true Valid values are true, false, 'create', 'update'. If a callable is passed then the field will be required only when the callback returns true.
$message optional null sameAs( string $field , string $secondField , string|null $message = null , string|callable|null $when = null )
Add a rule to compare two fields to each other.
If both fields have the exact same value the rule will pass.
$field $secondField $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
scalar( string $field , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure that a field contains a scalar.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
setProvider( string $name , object|string $object )
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.
$name $object sortMessageAndWhen( mixed $first , mixed $second , string $method )
Compatibility shim for the allowEmpty* methods that enable us to support both the $when, $message signature (deprecated) and the $message, $when format which is preferred.
A deprecation warning will be emitted when a deprecated form is used.
$first $second $method time( string $field , string|null $message = null , string|callable|null $when = null )
Add a time format validation rule to a field.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
uploadedFile( string $field , array $options , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure the field is an uploaded file
For options see Cake\Validation\Validation::uploadedFile()
$field $options $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
url( string $field , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure a field is a URL.
This validator does not require a protocol.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
urlWithProtocol( string $field , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure a field is a URL.
This validator requires the URL to have a protocol.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
utf8( string $field , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure a field contains only BMP utf8 bytes
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
utf8Extended( string $field , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure a field contains only utf8 bytes.
This rule will accept 3 and 4 byte UTF8 sequences, which are necessary for emoji.
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
uuid( string $field , string|null $message = null , string|callable|null $when = null )
Add a validation rule to ensure the field is a UUID
$field $message optional null $when optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
protected array
Contains the flags which specify what is empty for each corresponding field.
[]
protected array
Contains the validation messages associated with checking the emptiness for each corresponding field.
[]
protected static array
An associative array of objects or classes used as a default provider list
[]
protected array
Contains the validation messages associated with checking the presence for each corresponding field.
[]
protected array
An associative array of objects or classes containing methods used for validation
[]
protected boolean
Whether or not to use I18n functions for translating default error messages
false
© 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/3.8/class-Cake.Validation.Validator.html