| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since 2.2.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Validation;
- use ArrayAccess;
- use ArrayIterator;
- use Countable;
- use InvalidArgumentException;
- use IteratorAggregate;
- /**
- * 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
- *
- * @link http://book.cakephp.org/3.0/en/core-libraries/validation.html
- */
- class Validator implements ArrayAccess, IteratorAggregate, Countable
- {
- /**
- * Used to flag nested rules created with addNested() and addNestedMany()
- *
- * @var string
- */
- const NESTED = '_nested';
- /**
- * Holds the ValidationSet objects array
- *
- * @var array
- */
- protected $_fields = [];
- /**
- * An associative array of objects or classes containing methods
- * used for validation
- *
- * @var array
- */
- protected $_providers = [];
- /**
- * Contains the validation messages associated with checking the presence
- * for each corresponding field.
- *
- * @var array
- */
- protected $_presenceMessages = [];
- /**
- * Whether or not to use I18n functions for translating default error messages
- *
- * @var bool
- */
- protected $_useI18n = false;
- /**
- * Contains the validation messages associated with checking the emptiness
- * for each corresponding field.
- *
- * @var array
- */
- protected $_allowEmptyMessages = [];
- /**
- * Constructor
- *
- */
- public function __construct()
- {
- $this->_useI18n = function_exists('__d');
- }
- /**
- * 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.
- *
- * @param array $data The data to be checked for errors
- * @param bool $newRecord whether the data to be validated is new or to be updated.
- * @return array Array of invalid fields
- */
- public function errors(array $data, $newRecord = true)
- {
- $errors = [];
- $requiredMessage = 'This field is required';
- $emptyMessage = 'This field cannot be left empty';
- if ($this->_useI18n) {
- $requiredMessage = __d('cake', 'This field is required');
- $emptyMessage = __d('cake', 'This field cannot be left empty');
- }
- foreach ($this->_fields as $name => $field) {
- $keyPresent = array_key_exists($name, $data);
- $providers = $this->_providers;
- $context = compact('data', 'newRecord', 'field', 'providers');
- if (!$keyPresent && !$this->_checkPresence($field, $context)) {
- $errors[$name]['_required'] = isset($this->_presenceMessages[$name])
- ? $this->_presenceMessages[$name]
- : $requiredMessage;
- continue;
- }
- if (!$keyPresent) {
- continue;
- }
- $canBeEmpty = $this->_canBeEmpty($field, $context);
- $isEmpty = $this->_fieldIsEmpty($data[$name]);
- if (!$canBeEmpty && $isEmpty) {
- $errors[$name]['_empty'] = isset($this->_allowEmptyMessages[$name])
- ? $this->_allowEmptyMessages[$name]
- : $emptyMessage;
- continue;
- }
- if ($isEmpty) {
- continue;
- }
- $result = $this->_processRules($name, $field, $data, $newRecord);
- if ($result) {
- $errors[$name] = $result;
- }
- }
- return $errors;
- }
- /**
- * 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
- *
- * @param string $name [optional] The fieldname to fetch.
- * @param \Cake\Validation\ValidationSet|null $set The set of rules for field
- * @return \Cake\Validation\ValidationSet
- */
- public function field($name, ValidationSet $set = null)
- {
- if (empty($this->_fields[$name])) {
- $set = $set ?: new ValidationSet;
- $this->_fields[$name] = $set;
- }
- return $this->_fields[$name];
- }
- /**
- * Check whether or not a validator contains any rules for the given field.
- *
- * @param string $name The field name to check.
- * @return bool
- */
- public function hasField($name)
- {
- return isset($this->_fields[$name]);
- }
- /**
- * 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.
- *
- * @param string $name The name under which the provider should be set.
- * @param null|object|string $object Provider object or class name.
- * @return $this|object|string|null
- */
- public function provider($name, $object = null)
- {
- if ($object === null) {
- if (isset($this->_providers[$name])) {
- return $this->_providers[$name];
- }
- if ($name === 'default') {
- return $this->_providers[$name] = new RulesProvider;
- }
- return null;
- }
- $this->_providers[$name] = $object;
- return $this;
- }
- /**
- * Get the list of providers in this validator.
- *
- * @return array
- */
- public function providers()
- {
- return array_keys($this->_providers);
- }
- /**
- * Returns whether a rule set is defined for a field or not
- *
- * @param string $field name of the field to check
- * @return bool
- */
- public function offsetExists($field)
- {
- return isset($this->_fields[$field]);
- }
- /**
- * Returns the rule set for a field
- *
- * @param string $field name of the field to check
- * @return \Cake\Validation\ValidationSet
- */
- public function offsetGet($field)
- {
- return $this->field($field);
- }
- /**
- * Sets the rule set for a field
- *
- * @param string $field name of the field to set
- * @param array|\Cake\Validation\ValidationSet $rules set of rules to apply to field
- * @return void
- */
- public function offsetSet($field, $rules)
- {
- if (!$rules instanceof ValidationSet) {
- $set = new ValidationSet;
- foreach ((array)$rules as $name => $rule) {
- $set->add($name, $rule);
- }
- }
- $this->_fields[$field] = $rules;
- }
- /**
- * Unsets the rule set for a field
- *
- * @param string $field name of the field to unset
- * @return void
- */
- public function offsetUnset($field)
- {
- unset($this->_fields[$field]);
- }
- /**
- * Returns an iterator for each of the fields to be validated
- *
- * @return \ArrayIterator
- */
- public function getIterator()
- {
- return new ArrayIterator($this->_fields);
- }
- /**
- * Returns the number of fields having validation rules
- *
- * @return int
- */
- public function count()
- {
- return count($this->_fields);
- }
- /**
- * 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']
- * ]);
- * ```
- *
- * @param string $field The name of the field from which the rule will be removed
- * @param array|string $name The alias for a single rule or multiple rules array
- * @param array|\Cake\Validation\ValidationRule $rule the rule to add
- * @return $this
- */
- public function add($field, $name, $rule = [])
- {
- $field = $this->field($field);
- if (!is_array($name)) {
- $rules = [$name => $rule];
- } else {
- $rules = $name;
- }
- foreach ($rules as $name => $rule) {
- $field->add($name, $rule);
- }
- return $this;
- }
- /**
- * Adds a nested validator.
- *
- * Nesting validators allows you to define validators for array
- * types. For example, nested validators are ideal when you want to validate a
- * sub-document, or complex array type.
- *
- * This method assumes that the sub-document has a 1:1 relationship with the parent.
- *
- * The providers of the parent validator will be synced into the nested validator, when
- * errors are checked. This ensures that any validation rule providers connected
- * in the parent will have the same values in the nested validator when rules are evaluated.
- *
- * @param string $field The root field for the nested validator.
- * @param \Cake\Validation\Validator $validator The nested validator.
- * @return $this
- */
- public function addNested($field, Validator $validator)
- {
- $field = $this->field($field);
- $field->add(static::NESTED, ['rule' => function ($value, $context) use ($validator) {
- if (!is_array($value)) {
- return false;
- }
- foreach ($this->providers() as $provider) {
- $validator->provider($provider, $this->provider($provider));
- }
- $errors = $validator->errors($value, $context['newRecord']);
- return empty($errors) ? true : $errors;
- }]);
- return $this;
- }
- /**
- * Adds a nested validator.
- *
- * Nesting validators allows you to define validators for array
- * types. For example, nested validators are ideal when you want to validate many
- * similar sub-documents or complex array types.
- *
- * This method assumes that the sub-document has a 1:N relationship with the parent.
- *
- * The providers of the parent validator will be synced into the nested validator, when
- * errors are checked. This ensures that any validation rule providers connected
- * in the parent will have the same values in the nested validator when rules are evaluated.
- *
- * @param string $field The root field for the nested validator.
- * @param \Cake\Validation\Validator $validator The nested validator.
- * @return $this
- */
- public function addNestedMany($field, Validator $validator)
- {
- $field = $this->field($field);
- $field->add(static::NESTED, ['rule' => function ($value, $context) use ($validator) {
- if (!is_array($value)) {
- return false;
- }
- foreach ($this->providers() as $provider) {
- $validator->provider($provider, $this->provider($provider));
- }
- $errors = [];
- foreach ($value as $i => $row) {
- if (!is_array($row)) {
- return false;
- }
- $check = $validator->errors($row, $context['newRecord']);
- if (!empty($check)) {
- $errors[$i] = $check;
- }
- }
- return empty($errors) ? true : $errors;
- }]);
- return $this;
- }
- /**
- * Removes a rule from the set by its name
- *
- * ### Example:
- *
- * ```
- * $validator
- * ->remove('title', 'required')
- * ->remove('user_id')
- * ```
- *
- * @param string $field The name of the field from which the rule will be removed
- * @param string|null $rule the name of the rule to be removed
- * @return $this
- */
- public function remove($field, $rule = null)
- {
- if ($rule === null) {
- unset($this->_fields[$field]);
- } else {
- $this->field($field)->remove($rule);
- }
- return $this;
- }
- /**
- * Sets whether a field is required to be present in data array.
- *
- * @param string $field the name of the field
- * @param bool|string|callable $mode Valid values are true, false, 'create', 'update'.
- * If a callable is passed then the field will be required only when the callback
- * returns true.
- * @param string|null $message The message to show if the field presence validation fails.
- * @return $this
- */
- public function requirePresence($field, $mode = true, $message = null)
- {
- $this->field($field)->isPresenceRequired($mode);
- if ($message) {
- $this->_presenceMessages[$field] = $message;
- }
- return $this;
- }
- /**
- * Allows a field to be empty.
- *
- * This is the opposite of notEmpty() which requires a field to not be empty.
- * By using $mode equal to 'create' or 'update', you can allow fields to be empty
- * when records are first created, or when they are updated.
- *
- * ### Example:
- *
- * ```
- * $validator->allowEmpty('email'); // Email can be empty
- * $validator->allowEmpty('email', 'create'); // Email can be empty on create
- * $validator->allowEmpty('email', 'update'); // Email can be empty on update
- * ```
- *
- * It is possible to conditionally allow emptiness on a field by passing a callback
- * as a second argument. The callback will receive the validation context array as
- * argument:
- *
- * ```
- * $validator->allowEmpty('email', function ($context) {
- * return !$context['newRecord'] || $context['data']['role'] === 'admin';
- * });
- * ```
- *
- * This method will correctly detect empty file uploads and date/time/datetime fields.
- *
- * Because this and `notEmpty()` modify the same internal state, the last
- * method called will take precedence.
- *
- * @param string $field the name of the field
- * @param bool|string|callable $when 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.
- * @param string|null $message The message to show if the field is not
- * @return $this
- */
- public function allowEmpty($field, $when = true, $message = null)
- {
- $this->field($field)->isEmptyAllowed($when);
- if ($message) {
- $this->_allowEmptyMessages[$field] = $message;
- }
- return $this;
- }
- /**
- * Sets a field to require a non-empty value.
- *
- * This is the opposite of allowEmpty() which allows a field to be empty.
- * By using $mode equal to 'create' or 'update', you can make fields required
- * when records are first created, or when they are updated.
- *
- * ### Example:
- *
- * ```
- * $message = 'This field cannot be empty';
- * $validator->notEmpty('email'); // Email cannot be empty
- * $validator->notEmpty('email', $message, 'create'); // Email can be empty on update
- * $validator->notEmpty('email', $message, 'update'); // Email can be empty on create
- * ```
- *
- * It is possible to conditionally disallow emptiness on a field by passing a callback
- * as the third argument. The callback will receive the validation context array as
- * argument:
- *
- * ```
- * $validator->notEmpty('email', 'Email is required', function ($context) {
- * return $context['newRecord'] && $context['data']['role'] !== 'admin';
- * });
- * ```
- *
- * Because this and `allowEmpty()` modify the same internal state, the last
- * method called will take precedence.
- *
- * @param string $field the name of the field
- * @param string|null $message The message to show if the field is not
- * @param 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 be empty only when
- * the callback returns false.
- * @return $this
- */
- public function notEmpty($field, $message = null, $when = false)
- {
- if ($when === 'create' || $when === 'update') {
- $when = $when === 'create' ? 'update' : 'create';
- } elseif (is_callable($when)) {
- $when = function ($context) use ($when) {
- return !$when($context);
- };
- }
- $this->field($field)->isEmptyAllowed($when);
- if ($message) {
- $this->_allowEmptyMessages[$field] = $message;
- }
- return $this;
- }
- /**
- * Add a notBlank rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::notBlank()
- * @return $this
- */
- public function notBlank($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'notBlank', $extra + [
- 'rule' => 'notBlank',
- ]);
- }
- /**
- * Add an alphanumeric rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::alphaNumeric()
- * @return $this
- */
- public function alphaNumeric($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'alphaNumeric', $extra + [
- 'rule' => 'alphaNumeric',
- ]);
- }
- /**
- * Add an rule that ensures a string length is within a range.
- *
- * @param string $field The field you want to apply the rule to.
- * @param array $range The inclusive minimum and maximum length you want permitted.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::alphaNumeric()
- * @return $this
- */
- public function lengthBetween($field, array $range, $message = null, $when = null)
- {
- if (count($range) !== 2) {
- throw new InvalidArgumentException('The $range argument requires 2 numbers');
- }
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'lengthBetween', $extra + [
- 'rule' => ['lengthBetween', array_shift($range), array_shift($range)],
- ]);
- }
- /**
- * Add a credit card rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string $type 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']`
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::cc()
- * @return $this
- */
- public function creditCard($field, $type = 'all', $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'creditCard', $extra + [
- 'rule' => ['cc', $type, true],
- ]);
- }
- /**
- * Add a greater than comparison rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param int|float $value The value user data must be greater than.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::comparison()
- * @return $this
- */
- public function greaterThan($field, $value, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'greaterThan', $extra + [
- 'rule' => ['comparison', '>', $value]
- ]);
- }
- /**
- * Add a greater than or equal to comparison rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param int|float $value The value user data must be greater than or equal to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::comparison()
- * @return $this
- */
- public function greaterThanOrEqual($field, $value, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'greaterThanOrEqual', $extra + [
- 'rule' => ['comparison', '>=', $value]
- ]);
- }
- /**
- * Add a less than comparison rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param int|float $value The value user data must be less than.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::comparison()
- * @return $this
- */
- public function lessThan($field, $value, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'lessThan', $extra + [
- 'rule' => ['comparison', '<', $value]
- ]);
- }
- /**
- * Add a less than or equal comparison rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param int|float $value The value user data must be less than or equal to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::comparison()
- * @return $this
- */
- public function lessThanOrEqual($field, $value, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'lessThanOrEqual', $extra + [
- 'rule' => ['comparison', '<=', $value]
- ]);
- }
- /**
- * Add a equal to comparison rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param int|float $value The value user data must be equal to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::comparison()
- * @return $this
- */
- public function equals($field, $value, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'equals', $extra + [
- 'rule' => ['comparison', '=', $value]
- ]);
- }
- /**
- * Add a not equal to comparison rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param int|float $value The value user data must be not be equal to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::comparison()
- * @return $this
- */
- public function notEquals($field, $value, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'notEquals', $extra + [
- 'rule' => ['comparison', '!=', $value]
- ]);
- }
- /**
- * Add a rule to compare two fields to each other.
- *
- * If both fields have the exact same value the rule will pass.
- *
- * @param mixed $field The field you want to apply the rule to.
- * @param mixed $secondField The field you want to compare against.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::compareWith()
- * @return $this
- */
- public function sameAs($field, $secondField, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'sameAs', $extra + [
- 'rule' => ['compareWith', $secondField]
- ]);
- }
- /**
- * Add a rule to check if a field contains non alpha numeric characters.
- *
- * @param string $field The field you want to apply the rule to.
- * @param int $limit The minimum number of non-alphanumeric fields required.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::containsNonAlphaNumeric()
- * @return $this
- */
- public function containsNonAlphaNumeric($field, $limit = 1, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'containsNonAlphaNumeric', $extra + [
- 'rule' => ['containsNonAlphaNumeric', $limit]
- ]);
- }
- /**
- * Add a date format validation rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param array $formats A list of accepted date formats.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::date()
- * @return $this
- */
- public function date($field, $formats = ['ymd'], $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'date', $extra + [
- 'rule' => ['date', $formats]
- ]);
- }
- /**
- * Add a date time format validation rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param array $formats A list of accepted date formats.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::datetime()
- * @return $this
- */
- public function dateTime($field, $formats = ['ymd'], $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'dateTime', $extra + [
- 'rule' => ['datetime', $formats]
- ]);
- }
- /**
- * Add a time format validation rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::time()
- * @return $this
- */
- public function time($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'time', $extra + [
- 'rule' => 'time'
- ]);
- }
- /**
- * Add a localized time, date or datetime format validation rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string $type Parser type, one out of 'date', 'time', and 'datetime'
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::localizedTime()
- * @return $this
- */
- public function localizedTime($field, $type = 'datetime', $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'localizedTime', $extra + [
- 'rule' => ['localizedTime', $type]
- ]);
- }
- /**
- * Add a boolean validation rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::boolean()
- * @return $this
- */
- public function boolean($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'boolean', $extra + [
- 'rule' => 'boolean'
- ]);
- }
- /**
- * Add a decimal validation rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param int|null $places The number of decimal places to require.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::decimal()
- * @return $this
- */
- public function decimal($field, $places = null, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'decimal', $extra + [
- 'rule' => ['decimal', $places]
- ]);
- }
- /**
- * Add an email validation rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param bool $checkMX Whether or not to check the MX records.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::email()
- * @return $this
- */
- public function email($field, $checkMX = false, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'email', $extra + [
- 'rule' => ['email', $checkMX]
- ]);
- }
- /**
- * Add an IP validation rule to a field.
- *
- * This rule will accept both IPv4 and IPv6 addresses.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::ip()
- * @return $this
- */
- public function ip($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'ip', $extra + [
- 'rule' => 'ip'
- ]);
- }
- /**
- * Add an IPv4 validation rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::ip()
- * @return $this
- */
- public function ipv4($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'ipv4', $extra + [
- 'rule' => ['ip', 'ipv4']
- ]);
- }
- /**
- * Add an IPv6 validation rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::ip()
- * @return $this
- */
- public function ipv6($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'ipv6', $extra + [
- 'rule' => ['ip', 'ipv6']
- ]);
- }
- /**
- * Add a string length validation rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param int $min The minimum length required.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::minLength()
- * @return $this
- */
- public function minLength($field, $min, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'minLength', $extra + [
- 'rule' => ['minLength', $min]
- ]);
- }
- /**
- * Add a string length validation rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param int $max The maximum length allowed.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::maxLength()
- * @return $this
- */
- public function maxLength($field, $max, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'maxLength', $extra + [
- 'rule' => ['maxLength', $max]
- ]);
- }
- /**
- * Add a numeric value validation rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::numeric()
- * @return $this
- */
- public function numeric($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'numeric', $extra + [
- 'rule' => 'numeric'
- ]);
- }
- /**
- * Add a natural number validation rule to a field.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::naturalNumber()
- * @return $this
- */
- public function naturalNumber($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'naturalNumber', $extra + [
- 'rule' => ['naturalNumber', false]
- ]);
- }
- /**
- * Add a validation rule to ensure a field is a non negative integer.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::naturalNumber()
- * @return $this
- */
- public function nonNegativeInteger($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'nonNegativeInteger', $extra + [
- 'rule' => ['naturalNumber', true]
- ]);
- }
- /**
- * Add a validation rule to ensure a field is within a numeric range
- *
- * @param string $field The field you want to apply the rule to.
- * @param array $range The inclusive upper and lower bounds of the valid range.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::range()
- * @return $this
- */
- public function range($field, array $range, $message = null, $when = null)
- {
- if (count($range) !== 2) {
- throw new InvalidArgumentException('The $range argument requires 2 numbers');
- }
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'range', $extra + [
- 'rule' => ['range', array_shift($range), array_shift($range)]
- ]);
- }
- /**
- * Add a validation rule to ensure a field is a URL.
- *
- * This validator does not require a protocol.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::url()
- * @return $this
- */
- public function url($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'url', $extra + [
- 'rule' => ['url', false]
- ]);
- }
- /**
- * Add a validation rule to ensure a field is a URL.
- *
- * This validator requires the URL to have a protocol.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::url()
- * @return $this
- */
- public function urlWithProtocol($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'urlWithProtocol', $extra + [
- 'rule' => ['url', true]
- ]);
- }
- /**
- * Add a validation rule to ensure the field value is within a whitelist.
- *
- * @param string $field The field you want to apply the rule to.
- * @param array $list The list of valid options.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::inList()
- * @return $this
- */
- public function inList($field, array $list, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'inList', $extra + [
- 'rule' => ['inList', $list]
- ]);
- }
- /**
- * Add a validation rule to ensure the field is a UUID
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::uuid()
- * @return $this
- */
- public function uuid($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'uuid', $extra + [
- 'rule' => 'uuid'
- ]);
- }
- /**
- * Add a validation rule to ensure the field is an uploaded file
- *
- * For options see Cake\Validation\Validation::uploadedFile()
- *
- * @param string $field The field you want to apply the rule to.
- * @param array $options An array of options.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::uploadedFile()
- * @return $this
- */
- public function uploadedFile($field, array $options, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'uploadedFile', $extra + [
- 'rule' => ['uploadedFile', $options]
- ]);
- }
- /**
- * Add a validation rule to ensure the field is a lat/long tuple.
- *
- * e.g. `<lat>, <lng>`
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::uuid()
- * @return $this
- */
- public function latLong($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'latLong', $extra + [
- 'rule' => 'geoCoordinate'
- ]);
- }
- /**
- * Add a validation rule to ensure the field is a latitude.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::latitude()
- * @return $this
- */
- public function latitude($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'latitude', $extra + [
- 'rule' => 'latitude'
- ]);
- }
- /**
- * Add a validation rule to ensure the field is a longitude.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::longitude()
- * @return $this
- */
- public function longitude($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'longitude', $extra + [
- 'rule' => 'longitude'
- ]);
- }
- /**
- * Add a validation rule to ensure a field contains only ascii bytes
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::ascii()
- * @return $this
- */
- public function ascii($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'ascii', $extra + [
- 'rule' => 'ascii'
- ]);
- }
- /**
- * Add a validation rule to ensure a field contains only BMP utf8 bytes
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::utf8()
- * @return $this
- */
- public function utf8($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'utf8', $extra + [
- 'rule' => ['utf8', ['extended' => false]]
- ]);
- }
- /**
- * 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.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::utf8()
- * @return $this
- */
- public function utf8Extended($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'utf8Extended', $extra + [
- 'rule' => ['utf8', ['extended' => true]]
- ]);
- }
- /**
- * Add a validation rule to ensure a field is an integer value.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::isInteger()
- * @return $this
- */
- public function integer($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'integer', $extra + [
- 'rule' => 'isInteger'
- ]);
- }
- /**
- * Add a validation rule to ensure that a field contains an array.
- *
- * @param string $field The field you want to apply the rule to.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @return $this
- */
- public function isArray($field, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'isArray', $extra + [
- 'rule' => 'isArray'
- ]);
- }
- /**
- * Add a validation rule for a multiple select. Comparison is case sensitive by default.
- *
- * @param string $field The field you want to apply the rule to.
- * @param array $options The options for the validator. Includes the options defined in
- * \Cake\Validation\Validation::multiple() and the `caseInsensitive` parameter.
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::multiple()
- * @return $this
- */
- public function multipleOptions($field, array $options = [], $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- $caseInsensitive = isset($options['caseInsenstive']) ? $options['caseInsensitive'] : false;
- unset($options['caseInsensitive']);
- return $this->add($field, 'multipleOptions', $extra + [
- 'rule' => ['multiple', $options, $caseInsensitive]
- ]);
- }
- /**
- * Add a validation rule to ensure that a field is an array containing at least
- * the specified amount of elements
- *
- * @param string $field The field you want to apply the rule to.
- * @param int $count The number of elements the array should at least have
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::numElements()
- * @return $this
- */
- public function hasAtLeast($field, $count, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'hasAtLeast', $extra + [
- 'rule' => function ($value) use ($count) {
- if (is_array($value) && isset($value['_ids'])) {
- $value = $value['_ids'];
- }
- return Validation::numElements($value, '>=', $count);
- }
- ]);
- }
- /**
- * Add a validation rule to ensure that a field is an array containing at most
- * the specified amount of elements
- *
- * @param string $field The field you want to apply the rule to.
- * @param int $count The number maximim amount of elements the field should have
- * @param string|null $message The error message when the rule fails.
- * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
- * true when the validation rule should be applied.
- * @see \Cake\Validation\Validation::numElements()
- * @return $this
- */
- public function hasAtMost($field, $count, $message = null, $when = null)
- {
- $extra = array_filter(['on' => $when, 'message' => $message]);
- return $this->add($field, 'hasAtMost', $extra + [
- 'rule' => function ($value) use ($count) {
- if (is_array($value) && isset($value['_ids'])) {
- $value = $value['_ids'];
- }
- return Validation::numElements($value, '<=', $count);
- }
- ]);
- }
- /**
- * Returns whether or not a field can be left empty for a new or already existing
- * record.
- *
- * @param string $field Field name.
- * @param bool $newRecord whether the data to be validated is new or to be updated.
- * @return bool
- */
- public function isEmptyAllowed($field, $newRecord)
- {
- $providers = $this->_providers;
- $data = [];
- $context = compact('data', 'newRecord', 'field', 'providers');
- return $this->_canBeEmpty($this->field($field), $context);
- }
- /**
- * Returns whether or not a field can be left out for a new or already existing
- * record.
- *
- * @param string $field Field name.
- * @param bool $newRecord Whether the data to be validated is new or to be updated.
- * @return bool
- */
- public function isPresenceRequired($field, $newRecord)
- {
- $providers = $this->_providers;
- $data = [];
- $context = compact('data', 'newRecord', 'field', 'providers');
- return !$this->_checkPresence($this->field($field), $context);
- }
- /**
- * Returns false if any validation for the passed rule set should be stopped
- * due to the field missing in the data array
- *
- * @param \Cake\Validation\ValidationSet $field The set of rules for a field.
- * @param array $context A key value list of data containing the validation context.
- * @return bool
- */
- protected function _checkPresence($field, $context)
- {
- $required = $field->isPresenceRequired();
- if (!is_string($required) && is_callable($required)) {
- return !$required($context);
- }
- $newRecord = $context['newRecord'];
- if (in_array($required, ['create', 'update'], true)) {
- return (
- ($required === 'create' && !$newRecord) ||
- ($required === 'update' && $newRecord)
- );
- }
- return !$required;
- }
- /**
- * Returns whether the field can be left blank according to `allowEmpty`
- *
- * @param \Cake\Validation\ValidationSet $field the set of rules for a field
- * @param array $context a key value list of data containing the validation context.
- * @return bool
- */
- protected function _canBeEmpty($field, $context)
- {
- $allowed = $field->isEmptyAllowed();
- if (!is_string($allowed) && is_callable($allowed)) {
- return $allowed($context);
- }
- $newRecord = $context['newRecord'];
- if (in_array($allowed, ['create', 'update'], true)) {
- $allowed = (
- ($allowed === 'create' && $newRecord) ||
- ($allowed === 'update' && !$newRecord)
- );
- }
- return $allowed;
- }
- /**
- * Returns true if the field is empty in the passed data array
- *
- * @param mixed $data value to check against
- * @return bool
- */
- protected function _fieldIsEmpty($data)
- {
- if (empty($data) && $data !== '0' && $data !== false && $data !== 0 && $data !== 0.0) {
- return true;
- }
- $isArray = is_array($data);
- if ($isArray && (isset($data['year']) || isset($data['hour']))) {
- $value = implode('', $data);
- return strlen($value) === 0;
- }
- if ($isArray && isset($data['name'], $data['type'], $data['tmp_name'], $data['error'])) {
- return (int)$data['error'] === UPLOAD_ERR_NO_FILE;
- }
- return false;
- }
- /**
- * Iterates over each rule in the validation set and collects the errors resulting
- * from executing them
- *
- * @param string $field The name of the field that is being processed
- * @param \Cake\Validation\ValidationSet $rules the list of rules for a field
- * @param array $data the full data passed to the validator
- * @param bool $newRecord whether is it a new record or an existing one
- * @return array
- */
- protected function _processRules($field, ValidationSet $rules, $data, $newRecord)
- {
- $errors = [];
- // Loading default provider in case there is none
- $this->provider('default');
- $message = 'The provided value is invalid';
- if ($this->_useI18n) {
- $message = __d('cake', 'The provided value is invalid');
- }
- foreach ($rules as $name => $rule) {
- $result = $rule->process($data[$field], $this->_providers, compact('newRecord', 'data', 'field'));
- if ($result === true) {
- continue;
- }
- $errors[$name] = $message;
- if (is_array($result) && $name === static::NESTED) {
- $errors = $result;
- }
- if (is_string($result)) {
- $errors[$name] = $result;
- }
- if ($rule->isLast()) {
- break;
- }
- }
- return $errors;
- }
- /**
- * Get the printable version of this object.
- *
- * @return array
- */
- public function __debugInfo()
- {
- $fields = [];
- foreach ($this->_fields as $name => $fieldSet) {
- $fields[$name] = [
- 'isPresenceRequired' => $fieldSet->isPresenceRequired(),
- 'isEmptyAllowed' => $fieldSet->isEmptyAllowed(),
- 'rules' => array_keys($fieldSet->rules()),
- ];
- }
- return [
- '_presenceMessages' => $this->_presenceMessages,
- '_allowEmptyMessages' => $this->_allowEmptyMessages,
- '_useI18n' => $this->_useI18n,
- '_providers' => array_keys($this->_providers),
- '_fields' => $fields
- ];
- }
- }
|