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
- Cake\Validation\Validator implements ArrayAccess, IteratorAggregate, Countable
Link: https://book.cakephp.org/3.0/en/core-libraries/validation.html
Location: Validation/Validator.php
Constants summary
-
string
'_nested'
Properties summary
-
$_allowEmptyMessages
protectedarray
Contains the validation messages associated with checking the emptiness for each corresponding field.
- An associative array of objects or classes used as a default provider list
array
-
$_fields
protectedHolds the ValidationSet objects arrayarray
-
$_presenceMessages
protectedarray
Contains the validation messages associated with checking the presence for each corresponding field.
-
$_providers
protectedarray
An associative array of objects or classes containing methods used for validation
-
$_useI18n
protectedWhether or not to use I18n functions for translating default error messagesboolean
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() protected
Returns 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() protectedReturns true if the field is empty in the passed data array
- _processRules() protected
Iterates over each rule in the validation set and collects the errors resulting from executing them
- add() public
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.
- addNested() publicAdds a nested validator.
- addNestedMany() publicAdds a nested validator.
- allowEmpty() public
Allows a field to be empty. You can also pass array. Using an array will let you provide the following keys:
- alphaNumeric() publicAdd an alphanumeric rule to a field.
- ascii() publicAdd a validation rule to ensure a field contains only ascii bytes
- boolean() publicAdd a boolean validation rule to a field.
- containsNonAlphaNumeric() publicAdd 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.
- equals() publicAdd a equal to comparison rule to a field.
- errors() public
Returns an array of fields that have failed validation. On the current model. This method will actually run validation rules over data, not just return the messages.
- field() public
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.
- getIterator() publicReturns an iterator for each of the fields to be validated
- getProvider() publicReturns the provider stored under that name if it exists.
- greaterThan() publicAdd a greater than comparison rule to a field.
- greaterThanOrEqual() publicAdd a greater than or equal to comparison rule to a field.
- hasAtLeast() public
Add a validation rule to ensure that a field is an array containing at least the specified amount of elements
- hasAtMost() public
Add 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 a whitelist.
- integer() publicAdd a validation rule to ensure a field is an integer value.
- 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.
- isEmptyAllowed() public
Returns whether or not a field can be left empty for a new or already existing record.
- isPresenceRequired() public
Returns 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.
- lessThanOrEqual() publicAdd a less than or equal comparison rule to a field.
- 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.
- notBlank() publicAdd a notBlank rule to a field.
- notEmpty() public
Sets a field to require a non-empty value. You can also pass array. Using an array will let you provide the following keys:
- notEquals() publicAdd a not equal to comparison rule to a field.
- 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
- provider() public
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.
- 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() public
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:
- 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() public
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.
- 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
Method Detail
_canBeEmpty()source protected
_canBeEmpty( Cake\Validation\ValidationSet $field , array $context )
Returns 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
boolean_checkPresence()source protected
_checkPresence( Cake\Validation\ValidationSet $field , array $context )
Returns false if any validation for the passed rule set should be stopped due to the field missing in the data array
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
boolean_convertValidatorToArray()source protected
_convertValidatorToArray( integer|string $fieldName , array $defaults [] , string|array $settings [] )
Converts validator to fieldName => $settings array
Parameters
- integer|string
$fieldName
- name of field
- array
$defaults
optional [] - default settings
- string|array
$settings
optional [] - settings from data
Returns
array_fieldIsEmpty()source protected
_fieldIsEmpty( mixed $data )
Returns true if the field is empty in the passed data array
Parameters
- mixed
$data
- value to check against
Returns
boolean_processRules()source protected
_processRules( string $field , Cake\Validation\ValidationSet $rules , array $data , boolean $newRecord )
Iterates over each rule in the validation set and collects the errors resulting from executing them
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
- boolean
$newRecord
- whether is it a new record or an existing one
Returns
arrayadd()source public
add( string $field , array|string $name , array|Cake\Validation\ValidationRule $rule [] )
Adds a new rule to a field's rule set. If second argument is an array then rules list for the field will be replaced with second argument and third argument will be ignored.
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
$this
addDefaultProvider()source public static
addDefaultProvider( string $name , object|string $object )
Associates 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.
addNested()source public
addNested( string $field , Cake\Validation\Validator $validator )
Adds a nested validator.
Nesting validators allows you to define validators for array types. For example, nested validators are ideal when you want to validate a sub-document, or complex array type.
This method assumes that the sub-document has a 1:1 relationship with the parent.
The providers of the parent validator will be synced into the nested validator, when errors are checked. This ensures that any validation rule providers connected in the parent will have the same values in the nested validator when rules are evaluated.
Parameters
- string
$field
- The root field for the nested validator.
-
Cake\Validation\Validator
$validator
- The nested validator.
Returns
$this
addNestedMany()source public
addNestedMany( string $field , Cake\Validation\Validator $validator )
Adds a nested validator.
Nesting validators allows you to define validators for array types. For example, nested validators are ideal when you want to validate many similar sub-documents or complex array types.
This method assumes that the sub-document has a 1:N relationship with the parent.
The providers of the parent validator will be synced into the nested validator, when errors are checked. This ensures that any validation rule providers connected in the parent will have the same values in the nested validator when rules are evaluated.
Parameters
- string
$field
- The root field for the nested validator.
-
Cake\Validation\Validator
$validator
- The nested validator.
Returns
$this
allowEmpty()source public
allowEmpty( string|array $field , boolean|string|callable $when true , string|null $message null )
Allows a field to be empty. You can also pass array. Using an array will let you provide the following keys:
-
when
individual when condition for 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', 'create'); // Email can be empty on update $validator->allowEmpty('email', 'update'); // Email and subject can be empty on update $validator->allowEmpty(['email', 'subject'], 'update'); // Email can be always empty, subject and content can be empty on update. $validator->allowEmpty( [ 'email' => [ 'when' => true ], 'content' => [ 'message' => 'Content cannot be empty' ], 'subject' ], 'update' );
It is possible to conditionally allow emptiness on a field by passing a callback as a second argument. The callback will receive the validation context array as argument:
$validator->allowEmpty('email', function ($context) { return !$context['newRecord'] || $context['data']['role'] === 'admin'; });
This method will correctly detect empty file uploads and date/time/datetime fields.
Because this and notEmpty()
modify the same internal state, the last method called will take precedence.
Parameters
- string|array
$field
- the name of the field or a list of fields
- boolean|string|callable
$when
optional true Indicates when the field is allowed to be empty Valid values are true (always), 'create', 'update'. If a callable is passed then the field will allowed to be empty only when the callback returns true.
- string|null
$message
optional null - The message to show if the field is not
Returns
$this
alphaNumeric()source public
alphaNumeric( string $field , string|null $message null , string|callable|null $when null )
Add an alphanumeric rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::alphaNumeric()ascii()source public
ascii( string $field , string|null $message null , string|callable|null $when null )
Add 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 null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::ascii()boolean()source public
boolean( string $field , string|null $message null , string|callable|null $when null )
Add a boolean validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::boolean()containsNonAlphaNumeric()source public
containsNonAlphaNumeric( string $field , integer $limit 1 , string|null $message null , string|callable|null $when null )
Add a rule to check if a field contains non alpha numeric characters.
Parameters
- string
$field
- The field you want to apply the rule to.
- integer
$limit
optional 1 - The minimum number of non-alphanumeric fields required.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::containsNonAlphaNumeric()count()source public
count( )
Returns the number of fields having validation rules
Returns
integerImplementation of
Countable::count()
creditCard()source public
creditCard( string $field , string $type 'all' , string|null $message null , string|callable|null $when null )
Add a credit card rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- string
$type
optional 'all' The type of cards you want to allow. Defaults to 'all'. You can also supply an array of accepted card types. e.g
['mastercard', 'visa', 'amex']
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::cc()date()source public
date( string $field , array $formats ['ymd'] , string|null $message null , string|callable|null $when null )
Add a date format validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- array
$formats
optional ['ymd'] - A list of accepted date formats.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::date()dateTime()source public
dateTime( string $field , array $formats ['ymd'] , string|null $message null , string|callable|null $when null )
Add a date time format validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- array
$formats
optional ['ymd'] - A list of accepted date formats.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::datetime()decimal()source public
decimal( string $field , integer|null $places null , string|null $message null , string|callable|null $when null )
Add a decimal validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- integer|null
$places
optional null - The number of decimal places to require.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::decimal()email()source public
email( string $field , boolean $checkMX false , string|null $message null , string|callable|null $when null )
Add an email validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- boolean
$checkMX
optional false - Whether or not to check the MX records.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::email()equals()source public
equals( string $field , integer|float $value , string|null $message null , string|callable|null $when null )
Add a equal to comparison rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- integer|float
$value
- The value user data must be equal to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::comparison()errors()source public
errors( array $data , boolean $newRecord true )
Returns an array of fields that have failed validation. On the current model. This method will actually run validation rules over data, not just return the messages.
Parameters
- array
$data
- The data to be checked for errors
- boolean
$newRecord
optional true - whether the data to be validated is new or to be updated.
Returns
arrayArray of invalid fields
field()source public
field( string $name , Cake\Validation\ValidationSet $set null )
Returns a ValidationSet object containing all validation rules for a field, if passed a ValidationSet as second argument, it will replace any other rule set defined before
Parameters
- string
$name
- [optional] The fieldname to fetch.
-
Cake\Validation\ValidationSet
$set
optional null - The set of rules for field
Returns
Cake\Validation\ValidationSet
getDefaultProvider()source public static
getDefaultProvider( string $name )
Returns 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()source public static
getDefaultProviders( )
Get the list of default providers.
Returns
arraygetIterator()source public
getIterator( )
Returns an iterator for each of the fields to be validated
Returns
ArrayIteratorImplementation of
IteratorAggregate::getIterator()
getProvider()source public
getProvider( string $name )
Returns the provider stored under that name if it exists.
Parameters
- string
$name
- The name under which the provider should be set.
Returns
object|string|nullgreaterThan()source public
greaterThan( string $field , integer|float $value , string|null $message null , string|callable|null $when null )
Add a greater than comparison rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- integer|float
$value
- The value user data must be greater than.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::comparison()greaterThanOrEqual()source public
greaterThanOrEqual( string $field , integer|float $value , string|null $message null , string|callable|null $when null )
Add a greater than or equal to comparison rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- integer|float
$value
- The value user data must be greater than or equal to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::comparison()hasAtLeast()source public
hasAtLeast( string $field , integer $count , string|null $message null , string|callable|null $when null )
Add a validation rule to ensure that a field is an array containing at least the specified amount of elements
Parameters
- string
$field
- The field you want to apply the rule to.
- integer
$count
- The number of elements the array should at least have
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::numElements()hasAtMost()source public
hasAtMost( string $field , integer $count , string|null $message null , string|callable|null $when null )
Add a validation rule to ensure that a field is an array containing at most the specified amount of elements
Parameters
- string
$field
- The field you want to apply the rule to.
- integer
$count
- The number maximum amount of elements the field should have
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::numElements()hasField()source public
hasField( string $name )
Check whether or not a validator contains any rules for the given field.
Parameters
- string
$name
- The field name to check.
Returns
booleanhexColor()source public
hexColor( string $field , string|null $message null , string|callable|null $when null )
Add a validation rule to ensure a field is a 6 digits hex color value.
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::hexColor()inList()source public
inList( string $field , array $list , string|null $message null , string|callable|null $when null )
Add a validation rule to ensure the field value is within a whitelist.
Parameters
- string
$field
- The field you want to apply the rule to.
- array
$list
- The list of valid options.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::inList()integer()source public
integer( string $field , string|null $message null , string|callable|null $when null )
Add 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 null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::isInteger()ip()source public
ip( string $field , string|null $message null , string|callable|null $when null )
Add an IP validation rule to a field.
This rule will accept both IPv4 and IPv6 addresses.
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::ip()ipv4()source public
ipv4( string $field , string|null $message null , string|callable|null $when null )
Add an IPv4 validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::ip()ipv6()source public
ipv6( string $field , string|null $message null , string|callable|null $when null )
Add an IPv6 validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::ip()isArray()source public
isArray( string $field , string|null $message null , string|callable|null $when null )
Add 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 null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::isArray()isEmptyAllowed()source public
isEmptyAllowed( string $field , boolean $newRecord )
Returns whether or not a field can be left empty for a new or already existing record.
Parameters
- string
$field
- Field name.
- boolean
$newRecord
- whether the data to be validated is new or to be updated.
Returns
booleanisPresenceRequired()source public
isPresenceRequired( string $field , boolean $newRecord )
Returns whether or not a field can be left out for a new or already existing record.
Parameters
- string
$field
- Field name.
- boolean
$newRecord
- Whether the data to be validated is new or to be updated.
Returns
booleanlatLong()source public
latLong( string $field , string|null $message null , string|callable|null $when null )
Add a validation rule to ensure the field is a lat/long tuple.
e.g. <lat>, <lng>
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::uuid()latitude()source public
latitude( string $field , string|null $message null , string|callable|null $when null )
Add 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 null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::latitude()lengthBetween()source public
lengthBetween( string $field , array $range , string|null $message null , string|callable|null $when null )
Add an rule that ensures a string length is within a range.
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 null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::alphaNumeric()lessThan()source public
lessThan( string $field , integer|float $value , string|null $message null , string|callable|null $when null )
Add a less than comparison rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- integer|float
$value
- The value user data must be less than.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::comparison()lessThanOrEqual()source public
lessThanOrEqual( string $field , integer|float $value , string|null $message null , string|callable|null $when null )
Add a less than or equal comparison rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- integer|float
$value
- The value user data must be less than or equal to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::comparison()localizedTime()source public
localizedTime( string $field , string $type 'datetime' , string|null $message null , string|callable|null $when null )
Add a localized time, date or datetime format validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- string
$type
optional 'datetime' - Parser type, one out of 'date', 'time', and 'datetime'
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::localizedTime()longitude()source public
longitude( string $field , string|null $message null , string|callable|null $when null )
Add 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 null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::longitude()maxLength()source public
maxLength( string $field , integer $max , string|null $message null , string|callable|null $when null )
Add a string length validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- integer
$max
- The maximum length allowed.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::maxLength()maxLengthBytes()source public
maxLengthBytes( string $field , integer $max , string|null $message null , string|callable|null $when null )
Add a string length validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- integer
$max
- The maximum length allowed.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::maxLengthBytes()minLength()source public
minLength( string $field , integer $min , string|null $message null , string|callable|null $when null )
Add a string length validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- integer
$min
- The minimum length required.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::minLength()minLengthBytes()source public
minLengthBytes( string $field , integer $min , string|null $message null , string|callable|null $when null )
Add a string length validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- integer
$min
- The minimum length required.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::minLengthBytes()multipleOptions()source public
multipleOptions( string $field , array $options [] , string|null $message null , string|callable|null $when null )
Add a validation rule for a multiple select. Comparison is case sensitive by default.
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
caseInsensitive
parameter.- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::multiple()naturalNumber()source public
naturalNumber( string $field , string|null $message null , string|callable|null $when null )
Add a natural number validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::naturalNumber()nonNegativeInteger()source public
nonNegativeInteger( string $field , string|null $message null , string|callable|null $when null )
Add a validation rule to ensure a field is a non negative integer.
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::naturalNumber()notBlank()source public
notBlank( string $field , string|null $message null , string|callable|null $when null )
Add a notBlank rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::notBlank()notEmpty()source public
notEmpty( string|array $field , string|null $message null , boolean|string|callable $when false )
Sets a field to require a non-empty value. You can also pass array. Using an array will let you provide the following keys:
-
when
individual when condition for field -
message
individual 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, 'update'); // Email and title can be empty on create, but are required on update. $validator->notEmpty(['email', 'title'], $message, 'update'); // Email can be empty on create, title must always be not empty $validator->notEmpty( [ 'email', 'title' => [ 'when' => true, 'message' => 'Title cannot be empty' ] ], $message, 'update' );
It is possible to conditionally disallow emptiness on a field by passing a callback as the third argument. The callback will receive the validation context array as argument:
$validator->notEmpty('email', 'Email is required', function ($context) { return $context['newRecord'] && $context['data']['role'] !== 'admin'; });
Because this and allowEmpty()
modify the same internal state, the last method called will take precedence.
Parameters
- string|array
$field
- the name of the field or list of fields
- string|null
$message
optional null - The message to show if the field is not
- boolean|string|callable
$when
optional false Indicates when the field is not allowed to be empty. Valid values are true (always), 'create', 'update'. If a callable is passed then the field will allowed to be empty only when the callback returns false.
Returns
$this
notEquals()source public
notEquals( string $field , integer|float $value , string|null $message null , string|callable|null $when null )
Add a not equal to comparison rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- integer|float
$value
- The value user data must be not be equal to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::comparison()numeric()source public
numeric( string $field , string|null $message null , string|callable|null $when null )
Add a numeric value validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::numeric()offsetExists()source public
offsetExists( string $field )
Returns whether a rule set is defined for a field or not
Parameters
- string
$field
- name of the field to check
Returns
booleanImplementation of
ArrayAccess::offsetExists()
offsetGet()source public
offsetGet( string $field )
Returns the rule set for a field
Parameters
- string
$field
- name of the field to check
Returns
Cake\Validation\ValidationSet
Implementation of
ArrayAccess::offsetGet()
offsetSet()source public
offsetSet( string $field , array|Cake\Validation\ValidationSet $rules )
Sets 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
Implementation of
ArrayAccess::offsetSet()
offsetUnset()source public
offsetUnset( string $field )
Unsets the rule set for a field
Parameters
- string
$field
- name of the field to unset
Implementation of
ArrayAccess::offsetUnset()
provider()source public
provider( string $name , null|object|string $object null )
Associates an object to a name so it can be used as a provider. Providers are objects or class names that can contain methods used during validation of for deciding whether a validation rule can be applied. All validation methods, when called will receive the full list of providers stored in this validator.
If called with no arguments, it will return the provider stored under that name if it exists, otherwise it returns this instance of chaining.
Deprecated
3.4.0 Use setProvider()/getProvider() instead.Parameters
- string
$name
- The name under which the provider should be set.
- null|object|string
$object
optional null - Provider object or class name.
Returns
$this|object|string|null
range()source public
range( string $field , array $range , string|null $message null , string|callable|null $when null )
Add a validation rule to ensure a field is within a numeric range
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 null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::range()regex()source public
regex( string $field , string $regex , string|null $message null , string|callable|null $when null )
Returns whether or not a field matches against a regular expression.
Parameters
- string
$field
- Field name.
- string
$regex
- Regular expression.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
booleanremove()source public
remove( string $field , string|null $rule null )
Removes 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 null - the name of the rule to be removed
Returns
$this
requirePresence()source public
requirePresence( string|array $field , boolean|string|callable $mode true , string|null $message null )
Sets whether a field is required to be present in data array. You can also pass array. Using an array will let you provide the following keys:
-
mode
individual mode for field -
message
individual 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.
- boolean|string|callable
$mode
optional true Valid values are true, false, 'create', 'update'. If a callable is passed then the field will be required only when the callback returns true.
- string|null
$message
optional null - The message to show if the field presence validation fails.
Returns
$this
sameAs()source public
sameAs( mixed $field , mixed $secondField , string|null $message null , string|callable|null $when null )
Add a rule to compare two fields to each other.
If both fields have the exact same value the rule will pass.
Parameters
- mixed
$field
- The field you want to apply the rule to.
- mixed
$secondField
- The field you want to compare against.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::compareWith()scalar()source public
scalar( string $field , string|null $message null , string|callable|null $when null )
Add 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 null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::isScalar()setProvider()source public
setProvider( string $name , object|string $object )
Associates an object to a name so it can be used as a provider. Providers are objects or class names that can contain methods used during validation of for deciding whether a validation rule can be applied. All validation methods, when called will receive the full list of providers stored in this validator.
Parameters
- string
$name
- The name under which the provider should be set.
- object|string
$object
- Provider object or class name.
Returns
$this
time()source public
time( string $field , string|null $message null , string|callable|null $when null )
Add a time format validation rule to a field.
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::time()uploadedFile()source public
uploadedFile( string $field , array $options , string|null $message null , string|callable|null $when null )
Add a validation rule to ensure the field is an uploaded file
For options see Cake\Validation\Validation::uploadedFile()
Parameters
- string
$field
- The field you want to apply the rule to.
- array
$options
- An array of options.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::uploadedFile()url()source public
url( string $field , string|null $message null , string|callable|null $when null )
Add a validation rule to ensure a field is a URL.
This validator does not require a protocol.
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::url()urlWithProtocol()source public
urlWithProtocol( string $field , string|null $message null , string|callable|null $when null )
Add a validation rule to ensure a field is a URL.
This validator requires the URL to have a protocol.
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::url()utf8()source public
utf8( string $field , string|null $message null , string|callable|null $when null )
Add a validation rule to ensure a field contains only BMP utf8 bytes
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::utf8()utf8Extended()source public
utf8Extended( string $field , string|null $message null , string|callable|null $when null )
Add a validation rule to ensure a field contains only utf8 bytes.
This rule will accept 3 and 4 byte UTF8 sequences, which are necessary for emoji.
Parameters
- string
$field
- The field you want to apply the rule to.
- string|null
$message
optional null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::utf8()uuid()source public
uuid( string $field , string|null $message null , string|callable|null $when null )
Add 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 null - The error message when the rule fails.
- string|callable|null
$when
optional null Either 'create' or 'update' or a callable that returns true when the validation rule should be applied.
Returns
$this
See
\Cake\Validation\Validation::uuid()Properties detail
$_allowEmptyMessagessource
protected array
Contains the validation messages associated with checking the emptiness for each corresponding field.
[]
$_defaultProviderssource
protected static array
An associative array of objects or classes used as a default provider list
[]
$_presenceMessagessource
protected array
Contains the validation messages associated with checking the presence for each corresponding field.
[]
$_providerssource
protected array
An associative array of objects or classes containing methods used for validation
[]
$_useI18nsource
protected boolean
Whether or not to use I18n functions for translating default error messages
false
© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.4/class-Cake.Validation.Validator.html