Class Validator
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
Constants
- 
          
          intEMPTY_ALL ¶self::EMPTY_STRING | self::EMPTY_ARRAY | self::EMPTY_FILE | self::EMPTY_DATE | self::EMPTY_TIMEA combination of the all EMPTY_* flags 
- 
          
          intEMPTY_ARRAY ¶2A flag for allowEmptyFor() When an empty array is given, it will be recognized as empty. 
- 
          
          intEMPTY_DATE ¶8A flag for allowEmptyFor() When an array is given, if it contains the yearkey, and only empty strings or null values, it will be recognized as empty.
- 
          
          intEMPTY_FILE ¶4A flag for allowEmptyFor() When an array is given, if it has at least the name,type,tmp_nameanderrorkeys, and the value oferroris equal toUPLOAD_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.
- 
          
          intEMPTY_NULL ¶0A flag for allowEmptyFor() When nullis given, it will be recognized as empty.
- 
          
          intEMPTY_STRING ¶1A flag for allowEmptyFor() When an empty string is given, it will be recognized as empty. 
- 
          
          intEMPTY_TIME ¶16A flag for allowEmptyFor() When an array is given, if it contains the hourkey, and only empty strings or null values, it will be recognized as empty.
- 
          
          stringNESTED ¶'_nested'Used to flag nested rules created with addNested() and addNestedMany() 
- 
          
          stringWHEN_CREATE ¶'create'By using 'create' you can make fields required when records are first created. 
- 
          
          stringWHEN_UPDATE ¶'update'By using 'update', you can make fields required when they are updated. 
Property Summary
- 
        $_allowEmptyFlags protectedarrayContains the flags which specify what is empty for each corresponding field. 
- 
        $_allowEmptyMessages protectedarrayContains the validation messages associated with checking the emptiness for each corresponding field. 
- 
        $_defaultProviders protected staticarrayAn associative array of objects or classes used as a default provider list 
- 
        $_fields protectedCake\Validation\ValidationSet[]Holds the ValidationSet objects array 
- 
        $_presenceMessages protectedarrayContains the validation messages associated with checking the presence for each corresponding field. 
- 
        $_providers protectedarrayAn associative array of objects or classes containing methods used for validation 
- 
        $_stopOnFailure protectedboolWhether to apply last flag to generated rule(s). 
- 
        $_useI18n protectedboolWhether or not to use I18n functions for translating default error messages 
Method Summary
- 
          __construct() publicConstructor 
- 
          __debugInfo() publicGet the printable version of this object. 
- 
          _canBeEmpty() protectedReturns whether the field can be left blank according to allowEmpty
- 
          _checkPresence() protectedReturns false if any validation for the passed rule set should be stopped due to the field missing in the data array 
- 
          _convertValidatorToArray() protectedConverts validator to fieldName => $settings array 
- 
          _fieldIsEmpty() protected deprecatedReturns true if the field is empty in the passed data array 
- 
          _processRules() protectedIterates over each rule in the validation set and collects the errors resulting from executing them 
- 
          add() publicAdds 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. 
- 
          addDefaultProvider() public staticAssociates an object to a name so it can be used as a default provider. 
- 
          addNested() publicAdds a nested validator. 
- 
          addNestedMany() publicAdds a nested validator. 
- 
          allowEmpty() public deprecatedAllows a field to be empty. You can also pass array. Using an array will let you provide the following keys: 
- 
          allowEmptyArray() publicAllows a field to be an empty array. 
- 
          allowEmptyDate() publicAllows a field to be an empty date. 
- 
          allowEmptyDateTime() publicAllows a field to be an empty date/time. 
- 
          allowEmptyFile() publicAllows a field to be an empty file. 
- 
          allowEmptyFor() publicLow-level method to indicate that a field can be empty. 
- 
          allowEmptyString() publicAllows a field to be an empty string. 
- 
          allowEmptyTime() publicAllows a field to be an empty time. 
- 
          alphaNumeric() publicAdd an alphanumeric rule to a field. 
- 
          ascii() publicAdd a validation rule to ensure a field contains only ascii bytes 
- 
          asciiAlphaNumeric() publicAdd an ascii-alphanumeric rule to a field. 
- 
          boolean() publicAdd a boolean validation rule to a field. 
- 
          containsNonAlphaNumeric() public deprecatedAdd a rule to check if a field contains non alpha numeric characters. 
- 
          count() publicReturns the number of fields having validation rules 
- 
          creditCard() publicAdd a credit card rule to a field. 
- 
          date() publicAdd a date format validation rule to a field. 
- 
          dateTime() publicAdd a date time format validation rule to a field. 
- 
          decimal() publicAdd a decimal validation rule to a field. 
- 
          email() publicAdd an email validation rule to a field. 
- 
          equalToField() publicAdd a rule to compare one field is equal to another. 
- 
          equals() publicAdd a equal to comparison rule to a field. 
- 
          errors() public deprecatedValidates and returns an array of failed fields and their error messages. 
- 
          field() publicReturns 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 
- 
          getDefaultProvider() public staticReturns the default provider stored under that name if it exists. 
- 
          getDefaultProviders() public staticGet the list of default providers. 
- 
          getIterator() publicReturns an iterator for each of the fields to be validated 
- 
          getNotEmptyMessage() publicGets the notEmpty message for a field 
- 
          getProvider() publicReturns the provider stored under that name if it exists. 
- 
          getRequiredMessage() publicGets the required message for a field 
- 
          greaterThan() publicAdd a greater than comparison rule to a field. 
- 
          greaterThanField() publicAdd a rule to compare one field is greater than another. 
- 
          greaterThanOrEqual() publicAdd a greater than or equal to comparison rule to a field. 
- 
          greaterThanOrEqualToField() publicAdd a rule to compare one field is greater than or equal to another. 
- 
          hasAtLeast() publicAdd a validation rule to ensure that a field is an array containing at least the specified amount of elements 
- 
          hasAtMost() publicAdd a validation rule to ensure that a field is an array containing at most the specified amount of elements 
- 
          hasField() publicCheck whether or not a validator contains any rules for the given field. 
- 
          hexColor() publicAdd a validation rule to ensure a field is a 6 digits hex color value. 
- 
          inList() publicAdd a validation rule to ensure the field value is within an allowed list. 
- 
          integer() publicAdd a validation rule to ensure a field is an integer value. 
- 
          invertWhenClause() protectedInvert a when clause for creating notEmpty rules 
- 
          ip() publicAdd an IP validation rule to a field. 
- 
          ipv4() publicAdd an IPv4 validation rule to a field. 
- 
          ipv6() publicAdd an IPv6 validation rule to a field. 
- 
          isArray() publicAdd a validation rule to ensure that a field contains an array. 
- 
          isEmpty() protectedReturns true if the field is empty in the passed data array 
- 
          isEmptyAllowed() publicReturns whether or not a field can be left empty for a new or already existing record. 
- 
          isPresenceRequired() publicReturns whether or not a field can be left out for a new or already existing record. 
- 
          latLong() publicAdd a validation rule to ensure the field is a lat/long tuple. 
- 
          latitude() publicAdd a validation rule to ensure the field is a latitude. 
- 
          lengthBetween() publicAdd an rule that ensures a string length is within a range. 
- 
          lessThan() publicAdd a less than comparison rule to a field. 
- 
          lessThanField() publicAdd a rule to compare one field is less than another. 
- 
          lessThanOrEqual() publicAdd a less than or equal comparison rule to a field. 
- 
          lessThanOrEqualToField() publicAdd a rule to compare one field is less than or equal to another. 
- 
          localizedTime() publicAdd a localized time, date or datetime format validation rule to a field. 
- 
          longitude() publicAdd a validation rule to ensure the field is a longitude. 
- 
          maxLength() publicAdd a string length validation rule to a field. 
- 
          maxLengthBytes() publicAdd a string length validation rule to a field. 
- 
          minLength() publicAdd a string length validation rule to a field. 
- 
          minLengthBytes() publicAdd a string length validation rule to a field. 
- 
          multipleOptions() publicAdd a validation rule for a multiple select. Comparison is case sensitive by default. 
- 
          naturalNumber() publicAdd a natural number validation rule to a field. 
- 
          nonNegativeInteger() publicAdd a validation rule to ensure a field is a non negative integer. 
- 
          notAlphaNumeric() publicAdd a non-alphanumeric rule to a field. 
- 
          notAsciiAlphaNumeric() publicAdd a non-ascii alphanumeric rule to a field. 
- 
          notBlank() publicAdd a notBlank rule to a field. 
- 
          notEmpty() public deprecatedSets a field to require a non-empty value. You can also pass array. Using an array will let you provide the following keys: 
- 
          notEmptyArray() publicRequire a field to be a non-empty array 
- 
          notEmptyDate() publicRequire a non-empty date value 
- 
          notEmptyDateTime() publicRequire a field to be a non empty date/time. 
- 
          notEmptyFile() publicRequire a field to be a not-empty file. 
- 
          notEmptyString() publicRequires a field to be not be an empty string. 
- 
          notEmptyTime() publicRequire a field to be a non-empty time. 
- 
          notEqualToField() publicAdd a rule to compare one field is not equal to another. 
- 
          notEquals() publicAdd a not equal to comparison rule to a field. 
- 
          notSameAs() publicAdd a rule to compare that two fields have different values. 
- 
          numeric() publicAdd a numeric value validation rule to a field. 
- 
          offsetExists() publicReturns whether a rule set is defined for a field or not 
- 
          offsetGet() publicReturns the rule set for a field 
- 
          offsetSet() publicSets the rule set for a field 
- 
          offsetUnset() publicUnsets the rule set for a field 
- 
          providers() publicGet the list of providers in this validator. 
- 
          range() publicAdd a validation rule to ensure a field is within a numeric range 
- 
          regex() publicReturns whether or not a field matches against a regular expression. 
- 
          remove() publicRemoves a rule from the set by its name 
- 
          requirePresence() publicSets 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: 
- 
          sameAs() publicAdd a rule to compare two fields to each other. 
- 
          scalar() publicAdd a validation rule to ensure that a field contains a scalar. 
- 
          setProvider() publicAssociates 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. 
- 
          setStopOnFailure() publicWhether to stop validation rule evaluation on the first failed rule. 
- 
          time() publicAdd a time format validation rule to a field. 
- 
          uploadedFile() publicAdd a validation rule to ensure the field is an uploaded file 
- 
          url() publicAdd a validation rule to ensure a field is a URL. 
- 
          urlWithProtocol() publicAdd a validation rule to ensure a field is a URL. 
- 
          utf8() publicAdd a validation rule to ensure a field contains only BMP utf8 bytes 
- 
          utf8Extended() publicAdd a validation rule to ensure a field contains only utf8 bytes. 
- 
          uuid() publicAdd a validation rule to ensure the field is a UUID 
- 
          validate() publicValidates and returns an array of failed fields and their error messages. 
Method Detail
_canBeEmpty() ¶ protected
_canBeEmpty(Cake\Validation\ValidationSet $field, array $context): boolReturns whether the field can be left blank according to allowEmpty
Parameters
- 
                Cake\Validation\ValidationSet$field
- the set of rules for a field 
- 
                array$context
- a key value list of data containing the validation context. 
Returns
bool_checkPresence() ¶ protected
_checkPresence(Cake\Validation\ValidationSet $field, array $context): boolReturns false if any validation for the passed rule set should be stopped due to the field missing in the data array
Parameters
- 
                Cake\Validation\ValidationSet$field
- The set of rules for a field. 
- 
                array$context
- A key value list of data containing the validation context. 
Returns
bool_convertValidatorToArray() ¶ protected
_convertValidatorToArray(int|string $fieldName, array $defaults = [], string|array $settings = []): array[]Converts validator to fieldName => $settings array
Parameters
- 
                int|string$fieldName
- name of field 
- 
                array$defaults optional
- default settings 
- 
                string|array$settings optional
- settings from data 
Returns
array[]Throws
InvalidArgumentException_fieldIsEmpty() ¶ protected
_fieldIsEmpty(mixed $data): boolReturns true if the field is empty in the passed data array
Parameters
- 
                mixed$data
- Value to check against. 
Returns
bool_processRules() ¶ protected
_processRules(string $field, Cake\Validation\ValidationSet $rules, array $data, bool $newRecord): arrayIterates over each rule in the validation set and collects the errors resulting from executing them
Parameters
- 
                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 
Returns
arrayadd() ¶ public
add(string $field, array|string $name, array|Cake\Validation\ValidationRule $rule = []): $thisAdds 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.
Example:
     $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']
     ]);Parameters
- 
                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 
- 
                array|Cake\Validation\ValidationRule$rule optional
- the rule to add 
Returns
$thisThrows
InvalidArgumentExceptionIf numeric index cannot be resolved to a string one
addDefaultProvider() ¶ public static
addDefaultProvider(string $name, object|string $object): voidAssociates an object to a name so it can be used as a default provider.
Parameters
- 
                string$name
- The name under which the provider should be set. 
- 
                object|string$object
- Provider object or class name. 
Returns
voidaddNested() ¶ public
addNested(string $field, Cake\Validation\Validator $validator, string|null $message = null, string|callable|null $when = null): $thisAdds 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.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisaddNestedMany() ¶ public
addNestedMany(string $field, Cake\Validation\Validator $validator, string|null $message = null, string|callable|null $when = null): $thisAdds 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.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisallowEmpty() ¶ public
allowEmpty(string|array $field, bool|string|callable $when = true, string|null $message = null): $thisAllows a field to be empty. You can also pass array. Using an array will let you provide the following keys:
- whenindividual when condition for field
- 'message' individual message for field
You 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.
Example:
// 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.
Parameters
- 
                string|array$field
- the name of the field or a list of fields 
- 
                bool|string|callable$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 
Returns
$thisallowEmptyArray() ¶ public
allowEmptyArray(string $field, string|null $message = null, bool|string|callable $when = true): $thisAllows a field to be an empty array.
This method is equivalent to calling allowEmptyFor() with EMPTY_STRING + EMPTY_ARRAY flags.
Parameters
- 
                string$field
- The name of the field. 
- 
                string|null$message optional
- The message to show if the field is not 
- 
                bool|string|callable$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. 
Returns
$thisSee Also
allowEmptyDate() ¶ public
allowEmptyDate(string $field, string|null $message = null, bool|string|callable $when = true): $thisAllows a field to be an empty date.
Empty date values are null, '', [] and arrays where all values are ''
and the year key is present.
Parameters
- 
                string$field
- The name of the field. 
- 
                string|null$message optional
- The message to show if the field is not 
- 
                bool|string|callable$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. 
Returns
$thisSee Also
allowEmptyDateTime() ¶ public
allowEmptyDateTime(string $field, string|null $message = null, bool|string|callable $when = true): $thisAllows 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.
Parameters
- 
                string$field
- The name of the field. 
- 
                string|null$message optional
- The message to show if the field is not 
- 
                bool|string|callable$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. 
Returns
$thisSee Also
allowEmptyFile() ¶ public
allowEmptyFile(string $field, string|null $message = null, bool|string|callable $when = true): $thisAllows 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.
Parameters
- 
                string$field
- The name of the field. 
- 
                string|null$message optional
- The message to show if the field is not 
- 
                bool|string|callable$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. 
Returns
$thisSee Also
allowEmptyFor() ¶ public
allowEmptyFor(string $field, int|null $flags = null, bool|string|callable $when = true, string|null $message = null): $thisLow-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.
Example:
// 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');Parameters
- 
                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 - nullwill be allowed as empty value.
- 
                bool|string|callable$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 
Returns
$thisallowEmptyString() ¶ public
allowEmptyString(string $field, string|null $message = null, bool|string|callable $when = true): $thisAllows a field to be an empty string.
This method is equivalent to calling allowEmptyFor() with EMPTY_STRING flag.
Parameters
- 
                string$field
- The name of the field. 
- 
                string|null$message optional
- The message to show if the field is not 
- 
                bool|string|callable$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. 
Returns
$thisSee Also
allowEmptyTime() ¶ public
allowEmptyTime(string $field, string|null $message = null, bool|string|callable $when = true): $thisAllows 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.
Parameters
- 
                string$field
- The name of the field. 
- 
                string|null$message optional
- The message to show if the field is not 
- 
                bool|string|callable$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. 
Returns
$thisSee Also
alphaNumeric() ¶ public
alphaNumeric(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd an alphanumeric rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
ascii() ¶ public
ascii(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure a field contains only ascii bytes
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
asciiAlphaNumeric() ¶ public
asciiAlphaNumeric(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd an ascii-alphanumeric rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
boolean() ¶ public
boolean(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a boolean validation rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
containsNonAlphaNumeric() ¶ public
containsNonAlphaNumeric(string $field, int $limit = 1, string|null $message = null, string|callable|null $when = null): $thisAdd a rule to check if a field contains non alpha numeric characters.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
creditCard() ¶ public
creditCard(string $field, string $type = 'all', string|null $message = null, string|callable|null $when = null): $thisAdd a credit card rule to a field.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
date() ¶ public
date(string $field, array $formats = ['ymd'], string|null $message = null, string|callable|null $when = null): $thisAdd a date format validation rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                array$formats optional
- A list of accepted date formats. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
dateTime() ¶ public
dateTime(string $field, array $formats = ['ymd'], string|null $message = null, string|callable|null $when = null): $thisAdd a date time format validation rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                array$formats optional
- A list of accepted date formats. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
decimal() ¶ public
decimal(string $field, int|null $places = null, string|null $message = null, string|callable|null $when = null): $thisAdd a decimal validation rule to a field.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
email() ¶ public
email(string $field, bool $checkMX = false, string|null $message = null, string|callable|null $when = null): $thisAdd an email validation rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                bool$checkMX optional
- Whether or not to check the MX records. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
equalToField() ¶ public
equalToField(string $field, string $secondField, string|null $message = null, string|callable|null $when = null): $thisAdd a rule to compare one field is equal to another.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
equals() ¶ public
equals(string $field, int|float $value, string|null $message = null, string|callable|null $when = null): $thisAdd a equal to comparison rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                int|float$value
- The value user data must be equal to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
errors() ¶ public
errors(array $data, bool $newRecord = true): array[]Validates and returns an array of failed fields and their error messages.
Parameters
- 
                array$data
- The data to be checked for errors 
- 
                bool$newRecord optional
- whether the data to be validated is new or to be updated. 
Returns
array[]Array of failed fields
field() ¶ public
field(string $name, Cake\Validation\ValidationSet|null $set = null): Cake\Validation\ValidationSetReturns 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
Parameters
- 
                string$name
- [optional] The fieldname to fetch. 
- 
                Cake\Validation\ValidationSet|null$set optional
- The set of rules for field 
Returns
Cake\Validation\ValidationSetgetDefaultProvider() ¶ public static
getDefaultProvider(string $name): object|string|nullReturns the default provider stored under that name if it exists.
Parameters
- 
                string$name
- The name under which the provider should be retrieved. 
Returns
object|string|nullgetDefaultProviders() ¶ public static
getDefaultProviders(): string[]Get the list of default providers.
Returns
string[]getIterator() ¶ public
getIterator(): Cake\Validation\ValidationSet[]Returns an iterator for each of the fields to be validated
Returns
Cake\Validation\ValidationSet[]getNotEmptyMessage() ¶ public
getNotEmptyMessage(string $field): string|nullGets the notEmpty message for a field
Parameters
- 
                string$field
- Field name 
Returns
string|nullgetProvider() ¶ public
getProvider(string $name): object|string|nullReturns the provider stored under that name if it exists.
Parameters
- 
                string$name
- The name under which the provider should be set. 
Returns
object|string|nullgetRequiredMessage() ¶ public
getRequiredMessage(string $field): string|nullGets the required message for a field
Parameters
- 
                string$field
- Field name 
Returns
string|nullgreaterThan() ¶ public
greaterThan(string $field, int|float $value, string|null $message = null, string|callable|null $when = null): $thisAdd a greater than comparison rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                int|float$value
- The value user data must be greater than. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
greaterThanField() ¶ public
greaterThanField(string $field, string $secondField, string|null $message = null, string|callable|null $when = null): $thisAdd a rule to compare one field is greater than another.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
greaterThanOrEqual() ¶ public
greaterThanOrEqual(string $field, int|float $value, string|null $message = null, string|callable|null $when = null): $thisAdd a greater than or equal to comparison rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                int|float$value
- The value user data must be greater than or equal to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
greaterThanOrEqualToField() ¶ public
greaterThanOrEqualToField(string $field, string $secondField, string|null $message = null, string|callable|null $when = null): $thisAdd a rule to compare one field is greater than or equal to another.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
hasAtLeast() ¶ public
hasAtLeast(string $field, int $count, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure that a field is an array containing at least the specified amount of elements
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
hasAtMost() ¶ public
hasAtMost(string $field, int $count, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure that a field is an array containing at most the specified amount of elements
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
hasField() ¶ public
hasField(string $name): boolCheck whether or not a validator contains any rules for the given field.
Parameters
- 
                string$name
- The field name to check. 
Returns
boolhexColor() ¶ public
hexColor(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure a field is a 6 digits hex color value.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
inList() ¶ public
inList(string $field, array $list, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure the field value is within an allowed list.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
integer() ¶ public
integer(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure a field is an integer value.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
invertWhenClause() ¶ protected
invertWhenClause(bool|string|callable $when): bool|string|callableInvert a when clause for creating notEmpty rules
Parameters
- 
                bool|string|callable$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. 
Returns
bool|string|callableip() ¶ public
ip(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd an IP validation rule to a field.
This rule will accept both IPv4 and IPv6 addresses.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
ipv4() ¶ public
ipv4(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd an IPv4 validation rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
ipv6() ¶ public
ipv6(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd an IPv6 validation rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
isArray() ¶ public
isArray(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure that a field contains an array.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
isEmpty() ¶ protected
isEmpty(mixed $data, int $flags): boolReturns true if the field is empty in the passed data array
Parameters
- 
                mixed$data
- Value to check against. 
- 
                int$flags
- A bitmask of EMPTY_* flags which specify what is empty 
Returns
boolisEmptyAllowed() ¶ public
isEmptyAllowed(string $field, bool $newRecord): boolReturns whether or not a field can be left empty for a new or already existing record.
Parameters
- 
                string$field
- Field name. 
- 
                bool$newRecord
- whether the data to be validated is new or to be updated. 
Returns
boolisPresenceRequired() ¶ public
isPresenceRequired(string $field, bool $newRecord): boolReturns whether or not a field can be left out for a new or already existing record.
Parameters
- 
                string$field
- Field name. 
- 
                bool$newRecord
- Whether the data to be validated is new or to be updated. 
Returns
boollatLong() ¶ public
latLong(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure the field is a lat/long tuple.
e.g. <lat>, <lng>
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
latitude() ¶ public
latitude(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure the field is a latitude.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
lengthBetween() ¶ public
lengthBetween(string $field, array $range, string|null $message = null, string|callable|null $when = null): $thisAdd an rule that ensures a string length is within a range.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisThrows
InvalidArgumentExceptionSee Also
lessThan() ¶ public
lessThan(string $field, int|float $value, string|null $message = null, string|callable|null $when = null): $thisAdd a less than comparison rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                int|float$value
- The value user data must be less than. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
lessThanField() ¶ public
lessThanField(string $field, string $secondField, string|null $message = null, string|callable|null $when = null): $thisAdd a rule to compare one field is less than another.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
lessThanOrEqual() ¶ public
lessThanOrEqual(string $field, int|float $value, string|null $message = null, string|callable|null $when = null): $thisAdd a less than or equal comparison rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                int|float$value
- The value user data must be less than or equal to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
lessThanOrEqualToField() ¶ public
lessThanOrEqualToField(string $field, string $secondField, string|null $message = null, string|callable|null $when = null): $thisAdd a rule to compare one field is less than or equal to another.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
localizedTime() ¶ public
localizedTime(string $field, string $type = 'datetime', string|null $message = null, string|callable|null $when = null): $thisAdd a localized time, date or datetime format validation rule to a field.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
longitude() ¶ public
longitude(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure the field is a longitude.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
maxLength() ¶ public
maxLength(string $field, int $max, string|null $message = null, string|callable|null $when = null): $thisAdd a string length validation rule to a field.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
maxLengthBytes() ¶ public
maxLengthBytes(string $field, int $max, string|null $message = null, string|callable|null $when = null): $thisAdd a string length validation rule to a field.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
minLength() ¶ public
minLength(string $field, int $min, string|null $message = null, string|callable|null $when = null): $thisAdd a string length validation rule to a field.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
minLengthBytes() ¶ public
minLengthBytes(string $field, int $min, string|null $message = null, string|callable|null $when = null): $thisAdd a string length validation rule to a field.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
multipleOptions() ¶ public
multipleOptions(string $field, array $options = [], string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule for a multiple select. Comparison is case sensitive by default.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                array$options optional
- The options for the validator. Includes the options defined in \Cake\Validation\Validation::multiple() and the - caseInsensitiveparameter.
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
naturalNumber() ¶ public
naturalNumber(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a natural number validation rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
nonNegativeInteger() ¶ public
nonNegativeInteger(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure a field is a non negative integer.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
notAlphaNumeric() ¶ public
notAlphaNumeric(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a non-alphanumeric rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
notAsciiAlphaNumeric() ¶ public
notAsciiAlphaNumeric(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a non-ascii alphanumeric rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
notBlank() ¶ public
notBlank(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a notBlank rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
notEmpty() ¶ public
notEmpty(string|array $field, string|null $message = null, bool|string|callable $when = false): $thisSets a field to require a non-empty value. You can also pass array. Using an array will let you provide the following keys:
- whenindividual when condition for field
- messageindividual error message for field
You 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.
Example:
$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.
Parameters
- 
                string|array$field
- the name of the field or list of fields 
- 
                string|null$message optional
- The message to show if the field is not 
- 
                bool|string|callable$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. 
Returns
$thisnotEmptyArray() ¶ public
notEmptyArray(string $field, string|null $message = null, bool|string|callable $when = false): $thisRequire a field to be a non-empty array
Opposite to allowEmptyArray()
Parameters
- 
                string$field
- The name of the field. 
- 
                string|null$message optional
- The message to show if the field is empty. 
- 
                bool|string|callable$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. 
Returns
$thisSee Also
notEmptyDate() ¶ public
notEmptyDate(string $field, string|null $message = null, bool|string|callable $when = false): $thisRequire a non-empty date value
Parameters
- 
                string$field
- The name of the field. 
- 
                string|null$message optional
- The message to show if the field is empty. 
- 
                bool|string|callable$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. 
Returns
$thisSee Also
notEmptyDateTime() ¶ public
notEmptyDateTime(string $field, string|null $message = null, bool|string|callable $when = false): $thisRequire a field to be a non empty date/time.
Opposite to allowEmptyDateTime
Parameters
- 
                string$field
- The name of the field. 
- 
                string|null$message optional
- The message to show if the field is empty. 
- 
                bool|string|callable$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. 
Returns
$thisSee Also
notEmptyFile() ¶ public
notEmptyFile(string $field, string|null $message = null, bool|string|callable $when = false): $thisRequire a field to be a not-empty file.
Opposite to allowEmptyFile()
Parameters
- 
                string$field
- The name of the field. 
- 
                string|null$message optional
- The message to show if the field is empty. 
- 
                bool|string|callable$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. 
Returns
$thisSee Also
notEmptyString() ¶ public
notEmptyString(string $field, string|null $message = null, bool|string|callable $when = false): $thisRequires a field to be not be an empty string.
Opposite to allowEmptyString()
Parameters
- 
                string$field
- The name of the field. 
- 
                string|null$message optional
- The message to show if the field is empty. 
- 
                bool|string|callable$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. 
Returns
$thisSee Also
notEmptyTime() ¶ public
notEmptyTime(string $field, string|null $message = null, bool|string|callable $when = false): $thisRequire a field to be a non-empty time.
Opposite to allowEmptyTime()
Parameters
- 
                string$field
- The name of the field. 
- 
                string|null$message optional
- The message to show if the field is empty. 
- 
                bool|string|callable$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. 
Returns
$thisSee Also
notEqualToField() ¶ public
notEqualToField(string $field, string $secondField, string|null $message = null, string|callable|null $when = null): $thisAdd a rule to compare one field is not equal to another.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
notEquals() ¶ public
notEquals(string $field, int|float $value, string|null $message = null, string|callable|null $when = null): $thisAdd a not equal to comparison rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                int|float$value
- The value user data must be not be equal to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
notSameAs() ¶ public
notSameAs(string $field, string $secondField, string|null $message = null, string|callable|null $when = null): $thisAdd a rule to compare that two fields have different values.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
numeric() ¶ public
numeric(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a numeric value validation rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
offsetExists() ¶ public
offsetExists(string $field): boolReturns whether a rule set is defined for a field or not
Parameters
- 
                string$field
- name of the field to check 
Returns
booloffsetGet() ¶ public
offsetGet(string $field): Cake\Validation\ValidationSetReturns the rule set for a field
Parameters
- 
                string$field
- name of the field to check 
Returns
Cake\Validation\ValidationSetoffsetSet() ¶ public
offsetSet(string $field, array|Cake\Validation\ValidationSet $rules): voidSets the rule set for a field
Parameters
- 
                string$field
- name of the field to set 
- 
                array|Cake\Validation\ValidationSet$rules
- set of rules to apply to field 
Returns
voidoffsetUnset() ¶ public
offsetUnset(string $field): voidUnsets the rule set for a field
Parameters
- 
                string$field
- name of the field to unset 
Returns
voidproviders() ¶ public
providers(): string[]Get the list of providers in this validator.
Returns
string[]range() ¶ public
range(string $field, array $range, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure a field is within a numeric range
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisThrows
InvalidArgumentExceptionSee Also
regex() ¶ public
regex(string $field, string $regex, string|null $message = null, string|callable|null $when = null): $thisReturns whether or not a field matches against a regular expression.
Parameters
- 
                string$field
- Field name. 
- 
                string$regex
- Regular expression. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisremove() ¶ public
remove(string $field, string|null $rule = null): $thisRemoves a rule from the set by its name
Example:
     $validator
         ->remove('title', 'required')
         ->remove('user_id')Parameters
- 
                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 
Returns
$thisrequirePresence() ¶ public
requirePresence(string|array $field, bool|string|callable $mode = true, string|null $message = null): $thisSets 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:
- modeindividual mode for field
- messageindividual error message for field
You can also set mode and message for all passed fields, the individual setting takes precedence over group settings.
Parameters
- 
                string|array$field
- the name of the field or list of fields. 
- 
                bool|string|callable$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. 
Returns
$thissameAs() ¶ public
sameAs(string $field, string $secondField, string|null $message = null, string|callable|null $when = null): $thisAdd a rule to compare two fields to each other.
If both fields have the exact same value the rule will pass.
Parameters
- 
                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. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
scalar() ¶ public
scalar(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure that a field contains a scalar.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
setProvider() ¶ public
setProvider(string $name, object|string $object): $thisAssociates 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.
Parameters
- 
                string$name
- The name under which the provider should be set. 
- 
                object|string$object
- Provider object or class name. 
Returns
$thissetStopOnFailure() ¶ public
setStopOnFailure(bool $stopOnFailure = true): $thisWhether 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.
Parameters
- 
                bool$stopOnFailure optional
- If to apply last flag. 
Returns
$thistime() ¶ public
time(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a time format validation rule to a field.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
uploadedFile() ¶ public
uploadedFile(string $field, array $options, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure the field is an uploaded file
For options see Cake\Validation\Validation::uploadedFile()
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                array$options
- An array of options. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
url() ¶ public
url(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure a field is a URL.
This validator does not require a protocol.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
urlWithProtocol() ¶ public
urlWithProtocol(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure a field is a URL.
This validator requires the URL to have a protocol.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
utf8() ¶ public
utf8(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure a field contains only BMP utf8 bytes
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
utf8Extended() ¶ public
utf8Extended(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd 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.
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
uuid() ¶ public
uuid(string $field, string|null $message = null, string|callable|null $when = null): $thisAdd a validation rule to ensure the field is a UUID
Parameters
- 
                string$field
- The field you want to apply the rule to. 
- 
                string|null$message optional
- The error message when the rule fails. 
- 
                string|callable|null$when optional
- Either 'create' or 'update' or a callable that returns true when the validation rule should be applied. 
Returns
$thisSee Also
validate() ¶ public
validate(array $data, bool $newRecord = true): array[]Validates and returns an array of failed fields and their error messages.
Parameters
- 
                array$data
- The data to be checked for errors 
- 
                bool$newRecord optional
- whether the data to be validated is new or to be updated. 
Returns
array[]Array of failed fields
Property Detail
$_allowEmptyFlags ¶ protected
Contains the flags which specify what is empty for each corresponding field.
Type
array$_allowEmptyMessages ¶ protected
Contains the validation messages associated with checking the emptiness for each corresponding field.
Type
array$_defaultProviders ¶ protected static
An associative array of objects or classes used as a default provider list
Type
array$_presenceMessages ¶ protected
Contains the validation messages associated with checking the presence for each corresponding field.
Type
array$_providers ¶ protected
An associative array of objects or classes containing methods used for validation
Type
array$_useI18n ¶ protected
Whether or not to use I18n functions for translating default error messages
Type
bool