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
int
self::EMPTY_STRING | self::EMPTY_ARRAY | self::EMPTY_FILE | self::EMPTY_DATE | self::EMPTY_TIME
A combination of the all EMPTY_* flags
int
2
A flag for allowEmptyFor()
When an empty array is given, it will be recognized as empty.
int
8
A flag for allowEmptyFor()
When an array is given, if it contains the year
key, and only empty strings or null values, it will be recognized as empty.
int
4
A flag for allowEmptyFor()
When an array is given, if it has at least the name
, type
, tmp_name
and error
keys, and the value of error
is equal to UPLOAD_ERR_NO_FILE
, the value will be recognized as empty.
When an instance of \Psr\Http\Message\UploadedFileInterface is given the return value of it's getError() method must be equal to UPLOAD_ERR_NO_FILE
.
int
0
A flag for allowEmptyFor()
When null
is given, it will be recognized as empty.
int
1
A flag for allowEmptyFor()
When an empty string is given, it will be recognized as empty.
int
16
A flag for allowEmptyFor()
When an array is given, if it contains the hour
key, and only empty strings or null values, it will be recognized as empty.
string
'_nested'
Used to flag nested rules created with addNested() and addNestedMany()
string
'create'
By using 'create' you can make fields required when records are first created.
string
'update'
By using 'update', you can make fields required when they are updated.
array<string, int>
Contains the flags which specify what is empty for each corresponding field.
array<string, string>
Contains the validation messages associated with checking the emptiness for each corresponding field.
array<string, object|string>
An associative array of objects or classes used as a default provider list
array<string,Cake\Validation\ValidationSet>
Holds the ValidationSet objects array
array<string, string>
Contains the validation messages associated with checking the presence for each corresponding field.
array<string, object|string>
An associative array of objects or classes containing methods used for validation
bool
Whether to apply last flag to generated rule(s).
bool
Whether to use I18n functions for translating default error messages
Constructor
Get the printable version of this object.
Returns whether the field can be left blank according to allowEmpty
Returns false if any validation for the passed rule set should be stopped due to the field missing in the data array
Converts validator to fieldName => $settings array
Returns true if the field is empty in the passed 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.
Associates an object to a name so it can be used as a default provider.
Adds a nested validator.
Adds a nested validator.
Allows a field to be empty. You can also pass array. Using an array will let you provide the following keys:
Allows a field to be an empty array.
Allows a field to be an empty date.
Allows a field to be an empty date/time.
Allows a field to be an empty file.
Low-level method to indicate that a field can be empty.
Allows a field to be an empty string.
Allows a field to be an empty time.
Add an alphanumeric rule to a field.
Add a validation rule to ensure a field contains only ascii bytes
Add an ascii-alphanumeric rule to a field.
Add a boolean validation rule to a field.
Add a rule to check if a field contains non alpha numeric characters.
Returns the number of fields having validation rules
Add a credit card rule to a field.
Add a date format validation rule to a field.
Add a date time format validation rule to a field.
Add a decimal validation rule to a field.
Add an email validation rule to a field.
Add a rule to compare one field is equal to another.
Add a equal to comparison rule to a field.
Validates and returns an array of failed fields and their error 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
Returns the default provider stored under that name if it exists.
Get the list of default providers.
Returns an iterator for each of the fields to be validated
Gets the notEmpty message for a field
Returns the provider stored under that name if it exists.
Gets the required message for a field
Add a greater than comparison rule to a field.
Add a rule to compare one field is greater than another.
Add a greater than or equal to comparison rule to a field.
Add a rule to compare one field is greater than or equal to another.
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
Check whether a validator contains any rules for the given field.
Add a validation rule to ensure a field is a 6 digits hex color value.
Add a validation rule to ensure the field value is within an allowed list.
Add a validation rule to ensure a field is an integer value.
Invert a when clause for creating notEmpty rules
Add an IP validation rule to a field.
Add an IPv4 validation rule to a field.
Add an IPv6 validation rule to a field.
Add a validation rule to ensure that a field contains an array.
Returns true if the field is empty in the passed data array
Returns whether a field can be left empty for a new or already existing record.
Returns whether a field can be left out for a new or already existing record.
Add a validation rule to ensure the field is a lat/long tuple.
Add a validation rule to ensure the field is a latitude.
Add an rule that ensures a string length is within a range.
Add a less than comparison rule to a field.
Add a rule to compare one field is less than another.
Add a less than or equal comparison rule to a field.
Add a rule to compare one field is less than or equal to another.
Add a localized time, date or datetime format validation rule to a field.
Add a validation rule to ensure the field is a longitude.
Add a string length validation rule to a field.
Add a string length validation rule to a field.
Add a string length validation rule to a field.
Add a string length validation rule to a field.
Add a validation rule for a multiple select. Comparison is case sensitive by default.
Add a natural number validation rule to a field.
Add a validation rule to ensure a field is a non negative integer.
Add a non-alphanumeric rule to a field.
Add a non-ascii alphanumeric rule to a field.
Add a notBlank rule to a field.
Sets a field to require a non-empty value. You can also pass array. Using an array will let you provide the following keys:
Require a field to be a non-empty array
Require a non-empty date value
Require a field to be a non empty date/time.
Require a field to be a not-empty file.
Requires a field to not be an empty string.
Require a field to be a non-empty time.
Add a rule to compare one field is not equal to another.
Add a not equal to comparison rule to a field.
Add a rule to compare that two fields have different values.
Add a numeric value validation rule to a field.
Returns whether a rule set is defined for a field or not
Returns the rule set for a field
Sets the rule set for a field
Unsets the rule set for a field
Get the list of providers in this validator.
Add a validation rule to ensure a field is within a numeric range
Returns whether a field matches against a regular expression.
Removes a rule from the set by its name
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:
Add a rule to compare two fields to each other.
Add a validation rule to ensure that a field contains a scalar.
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.
Whether to stop validation rule evaluation on the first failed rule.
Add a time format validation rule to a field.
Add a validation rule to ensure the field is an uploaded file
Add a validation rule to ensure a field is a URL.
Add a validation rule to ensure a field is a URL.
Add a validation rule to ensure a field contains only BMP utf8 bytes
Add a validation rule to ensure a field contains only utf8 bytes.
Add a validation rule to ensure the field is a UUID
Validates and returns an array of failed fields and their error messages.
__construct()
Constructor
__debugInfo(): array<string, mixed>
Get the printable version of this object.
array<string, mixed>
_canBeEmpty(Cake\Validation\ValidationSet $field, array<string, mixed> $context): bool
Returns whether the field can be left blank according to allowEmpty
Cake\Validation\ValidationSet
$field the set of rules for a field
array<string, mixed>
$context a key value list of data containing the validation context.
bool
_checkPresence(Cake\Validation\ValidationSet $field, array<string, mixed> $context): bool
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 The set of rules for a field.
array<string, mixed>
$context A key value list of data containing the validation context.
bool
_convertValidatorToArray(string|int $fieldName, array<string, mixed> $defaults = [], array<string, mixed>|string $settings = []): array<array>
Converts validator to fieldName => $settings array
string|int
$fieldName name of field
array<string, mixed>
$defaults optional default settings
array<string, mixed>|string
$settings optional settings from data
array<array>
InvalidArgumentException
_fieldIsEmpty(mixed $data): bool
Returns true if the field is empty in the passed data array
mixed
$data Value to check against.
bool
_processRules(string $field, Cake\Validation\ValidationSet $rules, array $data, bool $newRecord): array<string, mixed>
Iterates over each rule in the validation set and collects the errors resulting from executing them
string
$field The name of the field that is being processed
Cake\Validation\ValidationSet
$rules the list of rules for a field
array
$data the full data passed to the validator
bool
$newRecord whether is it a new record or an existing one
array<string, mixed>
add(string $field, array|string $name, Cake\Validation\ValidationRule|array $rule = []): $this
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'] ]);
string
$field The name of the field from which the rule will be added
array|string
$name The alias for a single rule or multiple rules array
Cake\Validation\ValidationRule|array
$rule optional the rule to add
$this
InvalidArgumentException
addDefaultProvider(string $name, object|string $object): void
Associates an object to a name so it can be used as a default provider.
string
$name The name under which the provider should be set.
object|string
$object Provider object or class name.
void
addNested(string $field, Cake\Validation\Validator $validator, string|null $message = null, callable|string|null $when = null): $this
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.
string
$field The root field for the nested validator.
Cake\Validation\Validator
$validator The nested validator.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
addNestedMany(string $field, Cake\Validation\Validator $validator, string|null $message = null, callable|string|null $when = null): $this
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.
string
$field The root field for the nested validator.
Cake\Validation\Validator
$validator The nested validator.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
allowEmpty(array|string $field, callable|string|bool $when = true, string|null $message = null): $this
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', Validator::WHEN_CREATE); // Email can be empty on update $validator->allowEmpty('email', Validator::WHEN_UPDATE); // Email and subject can be empty on update $validator->allowEmpty(['email', 'subject'], Validator::WHEN_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' ], Validator::WHEN_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.
array|string
$field the name of the field or a list of fields
callable|string|bool
$when optional 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.
string|null
$message optional The message to show if the field is not
$this
allowEmptyArray(string $field, string|null $message = null, callable|string|bool $when = true): $this
Allows a field to be an empty array.
This method is equivalent to calling allowEmptyFor() with EMPTY_STRING + EMPTY_ARRAY flags.
string
$field The name of the field.
string|null
$message optional The message to show if the field is not
callable|string|bool
$when optional 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.
$this
allowEmptyDate(string $field, string|null $message = null, callable|string|bool $when = true): $this
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.
string
$field The name of the field.
string|null
$message optional The message to show if the field is not
callable|string|bool
$when optional 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.
$this
allowEmptyDateTime(string $field, string|null $message = null, callable|string|bool $when = true): $this
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.
string
$field The name of the field.
string|null
$message optional The message to show if the field is not
callable|string|bool
$when optional 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.
$this
allowEmptyFile(string $field, string|null $message = null, callable|string|bool $when = true): $this
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.
string
$field The name of the field.
string|null
$message optional The message to show if the field is not
callable|string|bool
$when optional 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.
$this
allowEmptyFor(string $field, int|null $flags = null, callable|string|bool $when = true, string|null $message = null): $this
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, Validator::WHEN_CREATE); // Email can be empty on update $validator->allowEmptyFor('email', Validator::EMPTY_STRING, Validator::WHEN_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');
string
$field The name of the field.
int|null
$flags optional A bitmask of EMPTY_* flags which specify what is empty. If no flags/bitmask is provided only null
will be allowed as empty value.
callable|string|bool
$when optional 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.
string|null
$message optional The message to show if the field is not
$this
allowEmptyString(string $field, string|null $message = null, callable|string|bool $when = true): $this
Allows a field to be an empty string.
This method is equivalent to calling allowEmptyFor() with EMPTY_STRING flag.
string
$field The name of the field.
string|null
$message optional The message to show if the field is not
callable|string|bool
$when optional 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.
$this
allowEmptyTime(string $field, string|null $message = null, callable|string|bool $when = true): $this
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.
string
$field The name of the field.
string|null
$message optional The message to show if the field is not
callable|string|bool
$when optional 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.
$this
alphaNumeric(string $field, string|null $message = null, callable|string|null $when = null): $this
Add an alphanumeric rule to a field.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
ascii(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure a field contains only ascii bytes
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
asciiAlphaNumeric(string $field, string|null $message = null, callable|string|null $when = null): $this
Add an ascii-alphanumeric rule to a field.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
boolean(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a boolean validation rule to a field.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
containsNonAlphaNumeric(string $field, int $limit = 1, string|null $message = null, callable|string|null $when = null): $this
Add a rule to check if a field contains non alpha numeric characters.
string
$field The field you want to apply the rule to.
int
$limit optional The minimum number of non-alphanumeric fields required.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
count(): int
Returns the number of fields having validation rules
int
creditCard(string $field, string $type = 'all', string|null $message = null, callable|string|null $when = null): $this
Add a credit card rule to a field.
string
$field The field you want to apply the rule to.
string
$type optional 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']
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
date(string $field, array<string> $formats = ['ymd'], string|null $message = null, callable|string|null $when = null): $this
Add a date format validation rule to a field.
string
$field The field you want to apply the rule to.
array<string>
$formats optional A list of accepted date formats.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
dateTime(string $field, array<string> $formats = ['ymd'], string|null $message = null, callable|string|null $when = null): $this
Add a date time format validation rule to a field.
string
$field The field you want to apply the rule to.
array<string>
$formats optional A list of accepted date formats.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
decimal(string $field, int|null $places = null, string|null $message = null, callable|string|null $when = null): $this
Add a decimal validation rule to a field.
string
$field The field you want to apply the rule to.
int|null
$places optional The number of decimal places to require.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
email(string $field, bool $checkMX = false, string|null $message = null, callable|string|null $when = null): $this
Add an email validation rule to a field.
string
$field The field you want to apply the rule to.
bool
$checkMX optional Whether to check the MX records.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
equalToField(string $field, string $secondField, string|null $message = null, callable|string|null $when = null): $this
Add a rule to compare one field is equal to another.
string
$field The field you want to apply the rule to.
string
$secondField The field you want to compare against.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
equals(string $field, float|int $value, string|null $message = null, callable|string|null $when = null): $this
Add a equal to comparison rule to a field.
string
$field The field you want to apply the rule to.
float|int
$value The value user data must be equal to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
errors(array $data, bool $newRecord = true): array<array>
Validates and returns an array of failed fields and their error messages.
array
$data The data to be checked for errors
bool
$newRecord optional whether the data to be validated is new or to be updated.
array<array>
field(string $name, Cake\Validation\ValidationSet|null $set = null): Cake\Validation\ValidationSet
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
string
$name [optional] The fieldname to fetch.
Cake\Validation\ValidationSet|null
$set optional The set of rules for field
Cake\Validation\ValidationSet
getDefaultProvider(string $name): object|string|null
Returns the default provider stored under that name if it exists.
string
$name The name under which the provider should be retrieved.
object|string|null
getDefaultProviders(): array<string>
Get the list of default providers.
array<string>
getIterator(): Traversable<string,Cake\Validation\ValidationSet>
Returns an iterator for each of the fields to be validated
Traversable<string,Cake\Validation\ValidationSet>
getNotEmptyMessage(string $field): string|null
Gets the notEmpty message for a field
string
$field Field name
string|null
getProvider(string $name): object|string|null
Returns the provider stored under that name if it exists.
string
$name The name under which the provider should be set.
object|string|null
getRequiredMessage(string $field): string|null
Gets the required message for a field
string
$field Field name
string|null
greaterThan(string $field, float|int $value, string|null $message = null, callable|string|null $when = null): $this
Add a greater than comparison rule to a field.
string
$field The field you want to apply the rule to.
float|int
$value The value user data must be greater than.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
greaterThanField(string $field, string $secondField, string|null $message = null, callable|string|null $when = null): $this
Add a rule to compare one field is greater than another.
string
$field The field you want to apply the rule to.
string
$secondField The field you want to compare against.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
greaterThanOrEqual(string $field, float|int $value, string|null $message = null, callable|string|null $when = null): $this
Add a greater than or equal to comparison rule to a field.
string
$field The field you want to apply the rule to.
float|int
$value The value user data must be greater than or equal to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
greaterThanOrEqualToField(string $field, string $secondField, string|null $message = null, callable|string|null $when = null): $this
Add a rule to compare one field is greater than or equal to another.
string
$field The field you want to apply the rule to.
string
$secondField The field you want to compare against.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
hasAtLeast(string $field, int $count, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure that a field is an array containing at least the specified amount of elements
string
$field The field you want to apply the rule to.
int
$count The number of elements the array should at least have
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
hasAtMost(string $field, int $count, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure that a field is an array containing at most the specified amount of elements
string
$field The field you want to apply the rule to.
int
$count The number maximum amount of elements the field should have
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
hasField(string $name): bool
Check whether a validator contains any rules for the given field.
string
$name The field name to check.
bool
hexColor(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure a field is a 6 digits hex color value.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
inList(string $field, array $list, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure the field value is within an allowed list.
string
$field The field you want to apply the rule to.
array
$list The list of valid options.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
integer(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure a field is an integer value.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
invertWhenClause(callable|string|bool $when): callable|string|bool
Invert a when clause for creating notEmpty rules
callable|string|bool
$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.
callable|string|bool
ip(string $field, string|null $message = null, callable|string|null $when = null): $this
Add an IP validation rule to a field.
This rule will accept both IPv4 and IPv6 addresses.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
ipv4(string $field, string|null $message = null, callable|string|null $when = null): $this
Add an IPv4 validation rule to a field.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
ipv6(string $field, string|null $message = null, callable|string|null $when = null): $this
Add an IPv6 validation rule to a field.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
isArray(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure that a field contains an array.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
isEmpty(mixed $data, int $flags): bool
Returns true if the field is empty in the passed data array
mixed
$data Value to check against.
int
$flags A bitmask of EMPTY_* flags which specify what is empty
bool
isEmptyAllowed(string $field, bool $newRecord): bool
Returns whether a field can be left empty for a new or already existing record.
string
$field Field name.
bool
$newRecord whether the data to be validated is new or to be updated.
bool
isPresenceRequired(string $field, bool $newRecord): bool
Returns whether a field can be left out for a new or already existing record.
string
$field Field name.
bool
$newRecord Whether the data to be validated is new or to be updated.
bool
latLong(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure the field is a lat/long tuple.
e.g. <lat>, <lng>
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
latitude(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure the field is a latitude.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
lengthBetween(string $field, array $range, string|null $message = null, callable|string|null $when = null): $this
Add an rule that ensures a string length is within a range.
string
$field The field you want to apply the rule to.
array
$range The inclusive minimum and maximum length you want permitted.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
InvalidArgumentException
lessThan(string $field, float|int $value, string|null $message = null, callable|string|null $when = null): $this
Add a less than comparison rule to a field.
string
$field The field you want to apply the rule to.
float|int
$value The value user data must be less than.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
lessThanField(string $field, string $secondField, string|null $message = null, callable|string|null $when = null): $this
Add a rule to compare one field is less than another.
string
$field The field you want to apply the rule to.
string
$secondField The field you want to compare against.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
lessThanOrEqual(string $field, float|int $value, string|null $message = null, callable|string|null $when = null): $this
Add a less than or equal comparison rule to a field.
string
$field The field you want to apply the rule to.
float|int
$value The value user data must be less than or equal to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
lessThanOrEqualToField(string $field, string $secondField, string|null $message = null, callable|string|null $when = null): $this
Add a rule to compare one field is less than or equal to another.
string
$field The field you want to apply the rule to.
string
$secondField The field you want to compare against.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
localizedTime(string $field, string $type = 'datetime', string|null $message = null, callable|string|null $when = null): $this
Add a localized time, date or datetime format validation rule to a field.
string
$field The field you want to apply the rule to.
string
$type optional Parser type, one out of 'date', 'time', and 'datetime'
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
longitude(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure the field is a longitude.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
maxLength(string $field, int $max, string|null $message = null, callable|string|null $when = null): $this
Add a string length validation rule to a field.
string
$field The field you want to apply the rule to.
int
$max The maximum length allowed.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
maxLengthBytes(string $field, int $max, string|null $message = null, callable|string|null $when = null): $this
Add a string length validation rule to a field.
string
$field The field you want to apply the rule to.
int
$max The maximum length allowed.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
minLength(string $field, int $min, string|null $message = null, callable|string|null $when = null): $this
Add a string length validation rule to a field.
string
$field The field you want to apply the rule to.
int
$min The minimum length required.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
minLengthBytes(string $field, int $min, string|null $message = null, callable|string|null $when = null): $this
Add a string length validation rule to a field.
string
$field The field you want to apply the rule to.
int
$min The minimum length required.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
multipleOptions(string $field, array<string, mixed> $options = [], string|null $message = null, callable|string|null $when = null): $this
Add a validation rule for a multiple select. Comparison is case sensitive by default.
string
$field The field you want to apply the rule to.
array<string, mixed>
$options optional The options for the validator. Includes the options defined in \Cake\Validation\Validation::multiple() and the caseInsensitive
parameter.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
naturalNumber(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a natural number validation rule to a field.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
nonNegativeInteger(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure a field is a non negative integer.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
notAlphaNumeric(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a non-alphanumeric rule to a field.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
notAsciiAlphaNumeric(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a non-ascii alphanumeric rule to a field.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
notBlank(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a notBlank rule to a field.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
notEmpty(array|string $field, string|null $message = null, callable|string|bool $when = false): $this
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, Validator::WHEN_UPDATE); // Email and title can be empty on create, but are required on update. $validator->notEmpty(['email', 'title'], $message, Validator::WHEN_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, Validator::WHEN_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.
array|string
$field the name of the field or list of fields
string|null
$message optional The message to show if the field is not
callable|string|bool
$when optional 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.
$this
notEmptyArray(string $field, string|null $message = null, callable|string|bool $when = false): $this
Require a field to be a non-empty array
Opposite to allowEmptyArray()
string
$field The name of the field.
string|null
$message optional The message to show if the field is empty.
callable|string|bool
$when optional 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.
$this
notEmptyDate(string $field, string|null $message = null, callable|string|bool $when = false): $this
Require a non-empty date value
string
$field The name of the field.
string|null
$message optional The message to show if the field is empty.
callable|string|bool
$when optional 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.
$this
notEmptyDateTime(string $field, string|null $message = null, callable|string|bool $when = false): $this
Require a field to be a non empty date/time.
Opposite to allowEmptyDateTime
string
$field The name of the field.
string|null
$message optional The message to show if the field is empty.
callable|string|bool
$when optional 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.
$this
notEmptyFile(string $field, string|null $message = null, callable|string|bool $when = false): $this
Require a field to be a not-empty file.
Opposite to allowEmptyFile()
string
$field The name of the field.
string|null
$message optional The message to show if the field is empty.
callable|string|bool
$when optional 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.
$this
notEmptyString(string $field, string|null $message = null, callable|string|bool $when = false): $this
Requires a field to not be an empty string.
Opposite to allowEmptyString()
string
$field The name of the field.
string|null
$message optional The message to show if the field is empty.
callable|string|bool
$when optional 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.
$this
notEmptyTime(string $field, string|null $message = null, callable|string|bool $when = false): $this
Require a field to be a non-empty time.
Opposite to allowEmptyTime()
string
$field The name of the field.
string|null
$message optional The message to show if the field is empty.
callable|string|bool
$when optional 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.
$this
notEqualToField(string $field, string $secondField, string|null $message = null, callable|string|null $when = null): $this
Add a rule to compare one field is not equal to another.
string
$field The field you want to apply the rule to.
string
$secondField The field you want to compare against.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
notEquals(string $field, float|int $value, string|null $message = null, callable|string|null $when = null): $this
Add a not equal to comparison rule to a field.
string
$field The field you want to apply the rule to.
float|int
$value The value user data must be not be equal to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
notSameAs(string $field, string $secondField, string|null $message = null, callable|string|null $when = null): $this
Add a rule to compare that two fields have different values.
string
$field The field you want to apply the rule to.
string
$secondField The field you want to compare against.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
numeric(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a numeric value validation rule to a field.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
offsetExists(string $field): bool
Returns whether a rule set is defined for a field or not
string
$field name of the field to check
bool
offsetGet(string $field): Cake\Validation\ValidationSet
Returns the rule set for a field
string
$field name of the field to check
Cake\Validation\ValidationSet
offsetSet(string $field, Cake\Validation\ValidationSet|array $rules): void
Sets the rule set for a field
string
$field name of the field to set
Cake\Validation\ValidationSet|array
$rules set of rules to apply to field
void
offsetUnset(string $field): void
Unsets the rule set for a field
string
$field name of the field to unset
void
providers(): array<string>
Get the list of providers in this validator.
array<string>
range(string $field, array $range, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure a field is within a numeric range
string
$field The field you want to apply the rule to.
array
$range The inclusive upper and lower bounds of the valid range.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
InvalidArgumentException
regex(string $field, string $regex, string|null $message = null, callable|string|null $when = null): $this
Returns whether a field matches against a regular expression.
string
$field Field name.
string
$regex Regular expression.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
remove(string $field, string|null $rule = null): $this
Removes a rule from the set by its name
$validator ->remove('title', 'required') ->remove('user_id')
string
$field The name of the field from which the rule will be removed
string|null
$rule optional the name of the rule to be removed
$this
requirePresence(array|string $field, callable|string|bool $mode = true, string|null $message = null): $this
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.
array|string
$field the name of the field or list of fields.
callable|string|bool
$mode optional Valid values are true, false, 'create', 'update'. If a callable is passed then the field will be required only when the callback returns true.
string|null
$message optional The message to show if the field presence validation fails.
$this
sameAs(string $field, string $secondField, string|null $message = null, callable|string|null $when = null): $this
Add a rule to compare two fields to each other.
If both fields have the exact same value the rule will pass.
string
$field The field you want to apply the rule to.
string
$secondField The field you want to compare against.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
scalar(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure that a field contains a scalar.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
setProvider(string $name, object|string $object): $this
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.
string
$name The name under which the provider should be set.
object|string
$object Provider object or class name.
$this
setStopOnFailure(bool $stopOnFailure = true): $this
Whether to stop validation rule evaluation on the first failed rule.
When enabled the first failing rule per field will cause validation to stop. When disabled all rules will be run even if there are failures.
bool
$stopOnFailure optional If to apply last flag.
$this
time(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a time format validation rule to a field.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
uploadedFile(string $field, array<string, mixed> $options, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure the field is an uploaded file
string
$field The field you want to apply the rule to.
array<string, mixed>
$options An array of options.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
url(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure a field is a URL.
This validator does not require a protocol.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
urlWithProtocol(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure a field is a URL.
This validator requires the URL to have a protocol.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
utf8(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure a field contains only BMP utf8 bytes
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
utf8Extended(string $field, string|null $message = null, callable|string|null $when = null): $this
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.
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
uuid(string $field, string|null $message = null, callable|string|null $when = null): $this
Add a validation rule to ensure the field is a UUID
string
$field The field you want to apply the rule to.
string|null
$message optional The error message when the rule fails.
callable|string|null
$when optional Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
$this
validate(array $data, bool $newRecord = true): array<array>
Validates and returns an array of failed fields and their error messages.
array
$data The data to be checked for errors
bool
$newRecord optional whether the data to be validated is new or to be updated.
array<array>
Contains the flags which specify what is empty for each corresponding field.
array<string, int>
Contains the validation messages associated with checking the emptiness for each corresponding field.
array<string, string>
An associative array of objects or classes used as a default provider list
array<string, object|string>
Holds the ValidationSet objects array
array<string,Cake\Validation\ValidationSet>
Contains the validation messages associated with checking the presence for each corresponding field.
array<string, string>
An associative array of objects or classes containing methods used for validation
array<string, object|string>
Whether to apply last flag to generated rule(s).
bool
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.Validation.Validator.html