Validator.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Validation;
  16. use ArrayAccess;
  17. use ArrayIterator;
  18. use Countable;
  19. use InvalidArgumentException;
  20. use IteratorAggregate;
  21. /**
  22. * Validator object encapsulates all methods related to data validations for a model
  23. * It also provides an API to dynamically change validation rules for each model field.
  24. *
  25. * Implements ArrayAccess to easily modify rules in the set
  26. *
  27. * @link http://book.cakephp.org/3.0/en/core-libraries/validation.html
  28. */
  29. class Validator implements ArrayAccess, IteratorAggregate, Countable
  30. {
  31. /**
  32. * Used to flag nested rules created with addNested() and addNestedMany()
  33. *
  34. * @var string
  35. */
  36. const NESTED = '_nested';
  37. /**
  38. * Holds the ValidationSet objects array
  39. *
  40. * @var array
  41. */
  42. protected $_fields = [];
  43. /**
  44. * An associative array of objects or classes containing methods
  45. * used for validation
  46. *
  47. * @var array
  48. */
  49. protected $_providers = [];
  50. /**
  51. * Contains the validation messages associated with checking the presence
  52. * for each corresponding field.
  53. *
  54. * @var array
  55. */
  56. protected $_presenceMessages = [];
  57. /**
  58. * Whether or not to use I18n functions for translating default error messages
  59. *
  60. * @var bool
  61. */
  62. protected $_useI18n = false;
  63. /**
  64. * Contains the validation messages associated with checking the emptiness
  65. * for each corresponding field.
  66. *
  67. * @var array
  68. */
  69. protected $_allowEmptyMessages = [];
  70. /**
  71. * Constructor
  72. *
  73. */
  74. public function __construct()
  75. {
  76. $this->_useI18n = function_exists('__d');
  77. }
  78. /**
  79. * Returns an array of fields that have failed validation. On the current model. This method will
  80. * actually run validation rules over data, not just return the messages.
  81. *
  82. * @param array $data The data to be checked for errors
  83. * @param bool $newRecord whether the data to be validated is new or to be updated.
  84. * @return array Array of invalid fields
  85. */
  86. public function errors(array $data, $newRecord = true)
  87. {
  88. $errors = [];
  89. $requiredMessage = 'This field is required';
  90. $emptyMessage = 'This field cannot be left empty';
  91. if ($this->_useI18n) {
  92. $requiredMessage = __d('cake', 'This field is required');
  93. $emptyMessage = __d('cake', 'This field cannot be left empty');
  94. }
  95. foreach ($this->_fields as $name => $field) {
  96. $keyPresent = array_key_exists($name, $data);
  97. $providers = $this->_providers;
  98. $context = compact('data', 'newRecord', 'field', 'providers');
  99. if (!$keyPresent && !$this->_checkPresence($field, $context)) {
  100. $errors[$name]['_required'] = isset($this->_presenceMessages[$name])
  101. ? $this->_presenceMessages[$name]
  102. : $requiredMessage;
  103. continue;
  104. }
  105. if (!$keyPresent) {
  106. continue;
  107. }
  108. $canBeEmpty = $this->_canBeEmpty($field, $context);
  109. $isEmpty = $this->_fieldIsEmpty($data[$name]);
  110. if (!$canBeEmpty && $isEmpty) {
  111. $errors[$name]['_empty'] = isset($this->_allowEmptyMessages[$name])
  112. ? $this->_allowEmptyMessages[$name]
  113. : $emptyMessage;
  114. continue;
  115. }
  116. if ($isEmpty) {
  117. continue;
  118. }
  119. $result = $this->_processRules($name, $field, $data, $newRecord);
  120. if ($result) {
  121. $errors[$name] = $result;
  122. }
  123. }
  124. return $errors;
  125. }
  126. /**
  127. * Returns a ValidationSet object containing all validation rules for a field, if
  128. * passed a ValidationSet as second argument, it will replace any other rule set defined
  129. * before
  130. *
  131. * @param string $name [optional] The fieldname to fetch.
  132. * @param \Cake\Validation\ValidationSet|null $set The set of rules for field
  133. * @return \Cake\Validation\ValidationSet
  134. */
  135. public function field($name, ValidationSet $set = null)
  136. {
  137. if (empty($this->_fields[$name])) {
  138. $set = $set ?: new ValidationSet;
  139. $this->_fields[$name] = $set;
  140. }
  141. return $this->_fields[$name];
  142. }
  143. /**
  144. * Check whether or not a validator contains any rules for the given field.
  145. *
  146. * @param string $name The field name to check.
  147. * @return bool
  148. */
  149. public function hasField($name)
  150. {
  151. return isset($this->_fields[$name]);
  152. }
  153. /**
  154. * Associates an object to a name so it can be used as a provider. Providers are
  155. * objects or class names that can contain methods used during validation of for
  156. * deciding whether a validation rule can be applied. All validation methods,
  157. * when called will receive the full list of providers stored in this validator.
  158. *
  159. * If called with no arguments, it will return the provider stored under that name if
  160. * it exists, otherwise it returns this instance of chaining.
  161. *
  162. * @param string $name The name under which the provider should be set.
  163. * @param null|object|string $object Provider object or class name.
  164. * @return $this|object|string|null
  165. */
  166. public function provider($name, $object = null)
  167. {
  168. if ($object === null) {
  169. if (isset($this->_providers[$name])) {
  170. return $this->_providers[$name];
  171. }
  172. if ($name === 'default') {
  173. return $this->_providers[$name] = new RulesProvider;
  174. }
  175. return null;
  176. }
  177. $this->_providers[$name] = $object;
  178. return $this;
  179. }
  180. /**
  181. * Get the list of providers in this validator.
  182. *
  183. * @return array
  184. */
  185. public function providers()
  186. {
  187. return array_keys($this->_providers);
  188. }
  189. /**
  190. * Returns whether a rule set is defined for a field or not
  191. *
  192. * @param string $field name of the field to check
  193. * @return bool
  194. */
  195. public function offsetExists($field)
  196. {
  197. return isset($this->_fields[$field]);
  198. }
  199. /**
  200. * Returns the rule set for a field
  201. *
  202. * @param string $field name of the field to check
  203. * @return \Cake\Validation\ValidationSet
  204. */
  205. public function offsetGet($field)
  206. {
  207. return $this->field($field);
  208. }
  209. /**
  210. * Sets the rule set for a field
  211. *
  212. * @param string $field name of the field to set
  213. * @param array|\Cake\Validation\ValidationSet $rules set of rules to apply to field
  214. * @return void
  215. */
  216. public function offsetSet($field, $rules)
  217. {
  218. if (!$rules instanceof ValidationSet) {
  219. $set = new ValidationSet;
  220. foreach ((array)$rules as $name => $rule) {
  221. $set->add($name, $rule);
  222. }
  223. }
  224. $this->_fields[$field] = $rules;
  225. }
  226. /**
  227. * Unsets the rule set for a field
  228. *
  229. * @param string $field name of the field to unset
  230. * @return void
  231. */
  232. public function offsetUnset($field)
  233. {
  234. unset($this->_fields[$field]);
  235. }
  236. /**
  237. * Returns an iterator for each of the fields to be validated
  238. *
  239. * @return \ArrayIterator
  240. */
  241. public function getIterator()
  242. {
  243. return new ArrayIterator($this->_fields);
  244. }
  245. /**
  246. * Returns the number of fields having validation rules
  247. *
  248. * @return int
  249. */
  250. public function count()
  251. {
  252. return count($this->_fields);
  253. }
  254. /**
  255. * Adds a new rule to a field's rule set. If second argument is an array
  256. * then rules list for the field will be replaced with second argument and
  257. * third argument will be ignored.
  258. *
  259. * ### Example:
  260. *
  261. * ```
  262. * $validator
  263. * ->add('title', 'required', ['rule' => 'notBlank'])
  264. * ->add('user_id', 'valid', ['rule' => 'numeric', 'message' => 'Invalid User'])
  265. *
  266. * $validator->add('password', [
  267. * 'size' => ['rule' => ['lengthBetween', 8, 20]],
  268. * 'hasSpecialCharacter' => ['rule' => 'validateSpecialchar', 'message' => 'not valid']
  269. * ]);
  270. * ```
  271. *
  272. * @param string $field The name of the field from which the rule will be removed
  273. * @param array|string $name The alias for a single rule or multiple rules array
  274. * @param array|\Cake\Validation\ValidationRule $rule the rule to add
  275. * @return $this
  276. */
  277. public function add($field, $name, $rule = [])
  278. {
  279. $field = $this->field($field);
  280. if (!is_array($name)) {
  281. $rules = [$name => $rule];
  282. } else {
  283. $rules = $name;
  284. }
  285. foreach ($rules as $name => $rule) {
  286. $field->add($name, $rule);
  287. }
  288. return $this;
  289. }
  290. /**
  291. * Adds a nested validator.
  292. *
  293. * Nesting validators allows you to define validators for array
  294. * types. For example, nested validators are ideal when you want to validate a
  295. * sub-document, or complex array type.
  296. *
  297. * This method assumes that the sub-document has a 1:1 relationship with the parent.
  298. *
  299. * The providers of the parent validator will be synced into the nested validator, when
  300. * errors are checked. This ensures that any validation rule providers connected
  301. * in the parent will have the same values in the nested validator when rules are evaluated.
  302. *
  303. * @param string $field The root field for the nested validator.
  304. * @param \Cake\Validation\Validator $validator The nested validator.
  305. * @return $this
  306. */
  307. public function addNested($field, Validator $validator)
  308. {
  309. $field = $this->field($field);
  310. $field->add(static::NESTED, ['rule' => function ($value, $context) use ($validator) {
  311. if (!is_array($value)) {
  312. return false;
  313. }
  314. foreach ($this->providers() as $provider) {
  315. $validator->provider($provider, $this->provider($provider));
  316. }
  317. $errors = $validator->errors($value, $context['newRecord']);
  318. return empty($errors) ? true : $errors;
  319. }]);
  320. return $this;
  321. }
  322. /**
  323. * Adds a nested validator.
  324. *
  325. * Nesting validators allows you to define validators for array
  326. * types. For example, nested validators are ideal when you want to validate many
  327. * similar sub-documents or complex array types.
  328. *
  329. * This method assumes that the sub-document has a 1:N relationship with the parent.
  330. *
  331. * The providers of the parent validator will be synced into the nested validator, when
  332. * errors are checked. This ensures that any validation rule providers connected
  333. * in the parent will have the same values in the nested validator when rules are evaluated.
  334. *
  335. * @param string $field The root field for the nested validator.
  336. * @param \Cake\Validation\Validator $validator The nested validator.
  337. * @return $this
  338. */
  339. public function addNestedMany($field, Validator $validator)
  340. {
  341. $field = $this->field($field);
  342. $field->add(static::NESTED, ['rule' => function ($value, $context) use ($validator) {
  343. if (!is_array($value)) {
  344. return false;
  345. }
  346. foreach ($this->providers() as $provider) {
  347. $validator->provider($provider, $this->provider($provider));
  348. }
  349. $errors = [];
  350. foreach ($value as $i => $row) {
  351. if (!is_array($row)) {
  352. return false;
  353. }
  354. $check = $validator->errors($row, $context['newRecord']);
  355. if (!empty($check)) {
  356. $errors[$i] = $check;
  357. }
  358. }
  359. return empty($errors) ? true : $errors;
  360. }]);
  361. return $this;
  362. }
  363. /**
  364. * Removes a rule from the set by its name
  365. *
  366. * ### Example:
  367. *
  368. * ```
  369. * $validator
  370. * ->remove('title', 'required')
  371. * ->remove('user_id')
  372. * ```
  373. *
  374. * @param string $field The name of the field from which the rule will be removed
  375. * @param string|null $rule the name of the rule to be removed
  376. * @return $this
  377. */
  378. public function remove($field, $rule = null)
  379. {
  380. if ($rule === null) {
  381. unset($this->_fields[$field]);
  382. } else {
  383. $this->field($field)->remove($rule);
  384. }
  385. return $this;
  386. }
  387. /**
  388. * Sets whether a field is required to be present in data array.
  389. *
  390. * @param string $field the name of the field
  391. * @param bool|string|callable $mode Valid values are true, false, 'create', 'update'.
  392. * If a callable is passed then the field will be required only when the callback
  393. * returns true.
  394. * @param string|null $message The message to show if the field presence validation fails.
  395. * @return $this
  396. */
  397. public function requirePresence($field, $mode = true, $message = null)
  398. {
  399. $this->field($field)->isPresenceRequired($mode);
  400. if ($message) {
  401. $this->_presenceMessages[$field] = $message;
  402. }
  403. return $this;
  404. }
  405. /**
  406. * Allows a field to be empty.
  407. *
  408. * This is the opposite of notEmpty() which requires a field to not be empty.
  409. * By using $mode equal to 'create' or 'update', you can allow fields to be empty
  410. * when records are first created, or when they are updated.
  411. *
  412. * ### Example:
  413. *
  414. * ```
  415. * $validator->allowEmpty('email'); // Email can be empty
  416. * $validator->allowEmpty('email', 'create'); // Email can be empty on create
  417. * $validator->allowEmpty('email', 'update'); // Email can be empty on update
  418. * ```
  419. *
  420. * It is possible to conditionally allow emptiness on a field by passing a callback
  421. * as a second argument. The callback will receive the validation context array as
  422. * argument:
  423. *
  424. * ```
  425. * $validator->allowEmpty('email', function ($context) {
  426. * return !$context['newRecord'] || $context['data']['role'] === 'admin';
  427. * });
  428. * ```
  429. *
  430. * This method will correctly detect empty file uploads and date/time/datetime fields.
  431. *
  432. * Because this and `notEmpty()` modify the same internal state, the last
  433. * method called will take precedence.
  434. *
  435. * @param string $field the name of the field
  436. * @param bool|string|callable $when Indicates when the field is allowed to be empty
  437. * Valid values are true (always), 'create', 'update'. If a callable is passed then
  438. * the field will allowed to be empty only when the callback returns true.
  439. * @param string|null $message The message to show if the field is not
  440. * @return $this
  441. */
  442. public function allowEmpty($field, $when = true, $message = null)
  443. {
  444. $this->field($field)->isEmptyAllowed($when);
  445. if ($message) {
  446. $this->_allowEmptyMessages[$field] = $message;
  447. }
  448. return $this;
  449. }
  450. /**
  451. * Sets a field to require a non-empty value.
  452. *
  453. * This is the opposite of allowEmpty() which allows a field to be empty.
  454. * By using $mode equal to 'create' or 'update', you can make fields required
  455. * when records are first created, or when they are updated.
  456. *
  457. * ### Example:
  458. *
  459. * ```
  460. * $message = 'This field cannot be empty';
  461. * $validator->notEmpty('email'); // Email cannot be empty
  462. * $validator->notEmpty('email', $message, 'create'); // Email can be empty on update
  463. * $validator->notEmpty('email', $message, 'update'); // Email can be empty on create
  464. * ```
  465. *
  466. * It is possible to conditionally disallow emptiness on a field by passing a callback
  467. * as the third argument. The callback will receive the validation context array as
  468. * argument:
  469. *
  470. * ```
  471. * $validator->notEmpty('email', 'Email is required', function ($context) {
  472. * return $context['newRecord'] && $context['data']['role'] !== 'admin';
  473. * });
  474. * ```
  475. *
  476. * Because this and `allowEmpty()` modify the same internal state, the last
  477. * method called will take precedence.
  478. *
  479. * @param string $field the name of the field
  480. * @param string|null $message The message to show if the field is not
  481. * @param bool|string|callable $when Indicates when the field is not allowed
  482. * to be empty. Valid values are true (always), 'create', 'update'. If a
  483. * callable is passed then the field will allowed be empty only when
  484. * the callback returns false.
  485. * @return $this
  486. */
  487. public function notEmpty($field, $message = null, $when = false)
  488. {
  489. if ($when === 'create' || $when === 'update') {
  490. $when = $when === 'create' ? 'update' : 'create';
  491. } elseif (is_callable($when)) {
  492. $when = function ($context) use ($when) {
  493. return !$when($context);
  494. };
  495. }
  496. $this->field($field)->isEmptyAllowed($when);
  497. if ($message) {
  498. $this->_allowEmptyMessages[$field] = $message;
  499. }
  500. return $this;
  501. }
  502. /**
  503. * Add a notBlank rule to a field.
  504. *
  505. * @param string $field The field you want to apply the rule to.
  506. * @param string|null $message The error message when the rule fails.
  507. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  508. * true when the validation rule should be applied.
  509. * @see \Cake\Validation\Validation::notBlank()
  510. * @return $this
  511. */
  512. public function notBlank($field, $message = null, $when = null)
  513. {
  514. $extra = array_filter(['on' => $when, 'message' => $message]);
  515. return $this->add($field, 'notBlank', $extra + [
  516. 'rule' => 'notBlank',
  517. ]);
  518. }
  519. /**
  520. * Add an alphanumeric rule to a field.
  521. *
  522. * @param string $field The field you want to apply the rule to.
  523. * @param string|null $message The error message when the rule fails.
  524. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  525. * true when the validation rule should be applied.
  526. * @see \Cake\Validation\Validation::alphaNumeric()
  527. * @return $this
  528. */
  529. public function alphaNumeric($field, $message = null, $when = null)
  530. {
  531. $extra = array_filter(['on' => $when, 'message' => $message]);
  532. return $this->add($field, 'alphaNumeric', $extra + [
  533. 'rule' => 'alphaNumeric',
  534. ]);
  535. }
  536. /**
  537. * Add an rule that ensures a string length is within a range.
  538. *
  539. * @param string $field The field you want to apply the rule to.
  540. * @param array $range The inclusive minimum and maximum length you want permitted.
  541. * @param string|null $message The error message when the rule fails.
  542. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  543. * true when the validation rule should be applied.
  544. * @see \Cake\Validation\Validation::alphaNumeric()
  545. * @return $this
  546. */
  547. public function lengthBetween($field, array $range, $message = null, $when = null)
  548. {
  549. if (count($range) !== 2) {
  550. throw new InvalidArgumentException('The $range argument requires 2 numbers');
  551. }
  552. $extra = array_filter(['on' => $when, 'message' => $message]);
  553. return $this->add($field, 'lengthBetween', $extra + [
  554. 'rule' => ['lengthBetween', array_shift($range), array_shift($range)],
  555. ]);
  556. }
  557. /**
  558. * Add a credit card rule to a field.
  559. *
  560. * @param string $field The field you want to apply the rule to.
  561. * @param string $type The type of cards you want to allow. Defaults to 'all'.
  562. * You can also supply an array of accepted card types. e.g `['mastercard', 'visa', 'amex']`
  563. * @param string|null $message The error message when the rule fails.
  564. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  565. * true when the validation rule should be applied.
  566. * @see \Cake\Validation\Validation::cc()
  567. * @return $this
  568. */
  569. public function creditCard($field, $type = 'all', $message = null, $when = null)
  570. {
  571. $extra = array_filter(['on' => $when, 'message' => $message]);
  572. return $this->add($field, 'creditCard', $extra + [
  573. 'rule' => ['cc', $type, true],
  574. ]);
  575. }
  576. /**
  577. * Add a greater than comparison rule to a field.
  578. *
  579. * @param string $field The field you want to apply the rule to.
  580. * @param int|float $value The value user data must be greater than.
  581. * @param string|null $message The error message when the rule fails.
  582. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  583. * true when the validation rule should be applied.
  584. * @see \Cake\Validation\Validation::comparison()
  585. * @return $this
  586. */
  587. public function greaterThan($field, $value, $message = null, $when = null)
  588. {
  589. $extra = array_filter(['on' => $when, 'message' => $message]);
  590. return $this->add($field, 'greaterThan', $extra + [
  591. 'rule' => ['comparison', '>', $value]
  592. ]);
  593. }
  594. /**
  595. * Add a greater than or equal to comparison rule to a field.
  596. *
  597. * @param string $field The field you want to apply the rule to.
  598. * @param int|float $value The value user data must be greater than or equal to.
  599. * @param string|null $message The error message when the rule fails.
  600. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  601. * true when the validation rule should be applied.
  602. * @see \Cake\Validation\Validation::comparison()
  603. * @return $this
  604. */
  605. public function greaterThanOrEqual($field, $value, $message = null, $when = null)
  606. {
  607. $extra = array_filter(['on' => $when, 'message' => $message]);
  608. return $this->add($field, 'greaterThanOrEqual', $extra + [
  609. 'rule' => ['comparison', '>=', $value]
  610. ]);
  611. }
  612. /**
  613. * Add a less than comparison rule to a field.
  614. *
  615. * @param string $field The field you want to apply the rule to.
  616. * @param int|float $value The value user data must be less than.
  617. * @param string|null $message The error message when the rule fails.
  618. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  619. * true when the validation rule should be applied.
  620. * @see \Cake\Validation\Validation::comparison()
  621. * @return $this
  622. */
  623. public function lessThan($field, $value, $message = null, $when = null)
  624. {
  625. $extra = array_filter(['on' => $when, 'message' => $message]);
  626. return $this->add($field, 'lessThan', $extra + [
  627. 'rule' => ['comparison', '<', $value]
  628. ]);
  629. }
  630. /**
  631. * Add a less than or equal comparison rule to a field.
  632. *
  633. * @param string $field The field you want to apply the rule to.
  634. * @param int|float $value The value user data must be less than or equal to.
  635. * @param string|null $message The error message when the rule fails.
  636. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  637. * true when the validation rule should be applied.
  638. * @see \Cake\Validation\Validation::comparison()
  639. * @return $this
  640. */
  641. public function lessThanOrEqual($field, $value, $message = null, $when = null)
  642. {
  643. $extra = array_filter(['on' => $when, 'message' => $message]);
  644. return $this->add($field, 'lessThanOrEqual', $extra + [
  645. 'rule' => ['comparison', '<=', $value]
  646. ]);
  647. }
  648. /**
  649. * Add a equal to comparison rule to a field.
  650. *
  651. * @param string $field The field you want to apply the rule to.
  652. * @param int|float $value The value user data must be equal to.
  653. * @param string|null $message The error message when the rule fails.
  654. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  655. * true when the validation rule should be applied.
  656. * @see \Cake\Validation\Validation::comparison()
  657. * @return $this
  658. */
  659. public function equals($field, $value, $message = null, $when = null)
  660. {
  661. $extra = array_filter(['on' => $when, 'message' => $message]);
  662. return $this->add($field, 'equals', $extra + [
  663. 'rule' => ['comparison', '=', $value]
  664. ]);
  665. }
  666. /**
  667. * Add a not equal to comparison rule to a field.
  668. *
  669. * @param string $field The field you want to apply the rule to.
  670. * @param int|float $value The value user data must be not be equal to.
  671. * @param string|null $message The error message when the rule fails.
  672. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  673. * true when the validation rule should be applied.
  674. * @see \Cake\Validation\Validation::comparison()
  675. * @return $this
  676. */
  677. public function notEquals($field, $value, $message = null, $when = null)
  678. {
  679. $extra = array_filter(['on' => $when, 'message' => $message]);
  680. return $this->add($field, 'notEquals', $extra + [
  681. 'rule' => ['comparison', '!=', $value]
  682. ]);
  683. }
  684. /**
  685. * Add a rule to compare two fields to each other.
  686. *
  687. * If both fields have the exact same value the rule will pass.
  688. *
  689. * @param mixed $field The field you want to apply the rule to.
  690. * @param mixed $secondField The field you want to compare against.
  691. * @param string|null $message The error message when the rule fails.
  692. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  693. * true when the validation rule should be applied.
  694. * @see \Cake\Validation\Validation::compareWith()
  695. * @return $this
  696. */
  697. public function sameAs($field, $secondField, $message = null, $when = null)
  698. {
  699. $extra = array_filter(['on' => $when, 'message' => $message]);
  700. return $this->add($field, 'sameAs', $extra + [
  701. 'rule' => ['compareWith', $secondField]
  702. ]);
  703. }
  704. /**
  705. * Add a rule to check if a field contains non alpha numeric characters.
  706. *
  707. * @param string $field The field you want to apply the rule to.
  708. * @param int $limit The minimum number of non-alphanumeric fields required.
  709. * @param string|null $message The error message when the rule fails.
  710. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  711. * true when the validation rule should be applied.
  712. * @see \Cake\Validation\Validation::containsNonAlphaNumeric()
  713. * @return $this
  714. */
  715. public function containsNonAlphaNumeric($field, $limit = 1, $message = null, $when = null)
  716. {
  717. $extra = array_filter(['on' => $when, 'message' => $message]);
  718. return $this->add($field, 'containsNonAlphaNumeric', $extra + [
  719. 'rule' => ['containsNonAlphaNumeric', $limit]
  720. ]);
  721. }
  722. /**
  723. * Add a date format validation rule to a field.
  724. *
  725. * @param string $field The field you want to apply the rule to.
  726. * @param array $formats A list of accepted date formats.
  727. * @param string|null $message The error message when the rule fails.
  728. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  729. * true when the validation rule should be applied.
  730. * @see \Cake\Validation\Validation::date()
  731. * @return $this
  732. */
  733. public function date($field, $formats = ['ymd'], $message = null, $when = null)
  734. {
  735. $extra = array_filter(['on' => $when, 'message' => $message]);
  736. return $this->add($field, 'date', $extra + [
  737. 'rule' => ['date', $formats]
  738. ]);
  739. }
  740. /**
  741. * Add a date time format validation rule to a field.
  742. *
  743. * @param string $field The field you want to apply the rule to.
  744. * @param array $formats A list of accepted date formats.
  745. * @param string|null $message The error message when the rule fails.
  746. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  747. * true when the validation rule should be applied.
  748. * @see \Cake\Validation\Validation::datetime()
  749. * @return $this
  750. */
  751. public function dateTime($field, $formats = ['ymd'], $message = null, $when = null)
  752. {
  753. $extra = array_filter(['on' => $when, 'message' => $message]);
  754. return $this->add($field, 'dateTime', $extra + [
  755. 'rule' => ['datetime', $formats]
  756. ]);
  757. }
  758. /**
  759. * Add a time format validation rule to a field.
  760. *
  761. * @param string $field The field you want to apply the rule to.
  762. * @param string|null $message The error message when the rule fails.
  763. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  764. * true when the validation rule should be applied.
  765. * @see \Cake\Validation\Validation::time()
  766. * @return $this
  767. */
  768. public function time($field, $message = null, $when = null)
  769. {
  770. $extra = array_filter(['on' => $when, 'message' => $message]);
  771. return $this->add($field, 'time', $extra + [
  772. 'rule' => 'time'
  773. ]);
  774. }
  775. /**
  776. * Add a localized time, date or datetime format validation rule to a field.
  777. *
  778. * @param string $field The field you want to apply the rule to.
  779. * @param string $type Parser type, one out of 'date', 'time', and 'datetime'
  780. * @param string|null $message The error message when the rule fails.
  781. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  782. * true when the validation rule should be applied.
  783. * @see \Cake\Validation\Validation::localizedTime()
  784. * @return $this
  785. */
  786. public function localizedTime($field, $type = 'datetime', $message = null, $when = null)
  787. {
  788. $extra = array_filter(['on' => $when, 'message' => $message]);
  789. return $this->add($field, 'localizedTime', $extra + [
  790. 'rule' => ['localizedTime', $type]
  791. ]);
  792. }
  793. /**
  794. * Add a boolean validation rule to a field.
  795. *
  796. * @param string $field The field you want to apply the rule to.
  797. * @param string|null $message The error message when the rule fails.
  798. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  799. * true when the validation rule should be applied.
  800. * @see \Cake\Validation\Validation::boolean()
  801. * @return $this
  802. */
  803. public function boolean($field, $message = null, $when = null)
  804. {
  805. $extra = array_filter(['on' => $when, 'message' => $message]);
  806. return $this->add($field, 'boolean', $extra + [
  807. 'rule' => 'boolean'
  808. ]);
  809. }
  810. /**
  811. * Add a decimal validation rule to a field.
  812. *
  813. * @param string $field The field you want to apply the rule to.
  814. * @param int|null $places The number of decimal places to require.
  815. * @param string|null $message The error message when the rule fails.
  816. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  817. * true when the validation rule should be applied.
  818. * @see \Cake\Validation\Validation::decimal()
  819. * @return $this
  820. */
  821. public function decimal($field, $places = null, $message = null, $when = null)
  822. {
  823. $extra = array_filter(['on' => $when, 'message' => $message]);
  824. return $this->add($field, 'decimal', $extra + [
  825. 'rule' => ['decimal', $places]
  826. ]);
  827. }
  828. /**
  829. * Add an email validation rule to a field.
  830. *
  831. * @param string $field The field you want to apply the rule to.
  832. * @param bool $checkMX Whether or not to check the MX records.
  833. * @param string|null $message The error message when the rule fails.
  834. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  835. * true when the validation rule should be applied.
  836. * @see \Cake\Validation\Validation::email()
  837. * @return $this
  838. */
  839. public function email($field, $checkMX = false, $message = null, $when = null)
  840. {
  841. $extra = array_filter(['on' => $when, 'message' => $message]);
  842. return $this->add($field, 'email', $extra + [
  843. 'rule' => ['email', $checkMX]
  844. ]);
  845. }
  846. /**
  847. * Add an IP validation rule to a field.
  848. *
  849. * This rule will accept both IPv4 and IPv6 addresses.
  850. *
  851. * @param string $field The field you want to apply the rule to.
  852. * @param string|null $message The error message when the rule fails.
  853. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  854. * true when the validation rule should be applied.
  855. * @see \Cake\Validation\Validation::ip()
  856. * @return $this
  857. */
  858. public function ip($field, $message = null, $when = null)
  859. {
  860. $extra = array_filter(['on' => $when, 'message' => $message]);
  861. return $this->add($field, 'ip', $extra + [
  862. 'rule' => 'ip'
  863. ]);
  864. }
  865. /**
  866. * Add an IPv4 validation rule to a field.
  867. *
  868. * @param string $field The field you want to apply the rule to.
  869. * @param string|null $message The error message when the rule fails.
  870. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  871. * true when the validation rule should be applied.
  872. * @see \Cake\Validation\Validation::ip()
  873. * @return $this
  874. */
  875. public function ipv4($field, $message = null, $when = null)
  876. {
  877. $extra = array_filter(['on' => $when, 'message' => $message]);
  878. return $this->add($field, 'ipv4', $extra + [
  879. 'rule' => ['ip', 'ipv4']
  880. ]);
  881. }
  882. /**
  883. * Add an IPv6 validation rule to a field.
  884. *
  885. * @param string $field The field you want to apply the rule to.
  886. * @param string|null $message The error message when the rule fails.
  887. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  888. * true when the validation rule should be applied.
  889. * @see \Cake\Validation\Validation::ip()
  890. * @return $this
  891. */
  892. public function ipv6($field, $message = null, $when = null)
  893. {
  894. $extra = array_filter(['on' => $when, 'message' => $message]);
  895. return $this->add($field, 'ipv6', $extra + [
  896. 'rule' => ['ip', 'ipv6']
  897. ]);
  898. }
  899. /**
  900. * Add a string length validation rule to a field.
  901. *
  902. * @param string $field The field you want to apply the rule to.
  903. * @param int $min The minimum length required.
  904. * @param string|null $message The error message when the rule fails.
  905. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  906. * true when the validation rule should be applied.
  907. * @see \Cake\Validation\Validation::minLength()
  908. * @return $this
  909. */
  910. public function minLength($field, $min, $message = null, $when = null)
  911. {
  912. $extra = array_filter(['on' => $when, 'message' => $message]);
  913. return $this->add($field, 'minLength', $extra + [
  914. 'rule' => ['minLength', $min]
  915. ]);
  916. }
  917. /**
  918. * Add a string length validation rule to a field.
  919. *
  920. * @param string $field The field you want to apply the rule to.
  921. * @param int $max The maximum length allowed.
  922. * @param string|null $message The error message when the rule fails.
  923. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  924. * true when the validation rule should be applied.
  925. * @see \Cake\Validation\Validation::maxLength()
  926. * @return $this
  927. */
  928. public function maxLength($field, $max, $message = null, $when = null)
  929. {
  930. $extra = array_filter(['on' => $when, 'message' => $message]);
  931. return $this->add($field, 'maxLength', $extra + [
  932. 'rule' => ['maxLength', $max]
  933. ]);
  934. }
  935. /**
  936. * Add a numeric value validation rule to a field.
  937. *
  938. * @param string $field The field you want to apply the rule to.
  939. * @param string|null $message The error message when the rule fails.
  940. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  941. * true when the validation rule should be applied.
  942. * @see \Cake\Validation\Validation::numeric()
  943. * @return $this
  944. */
  945. public function numeric($field, $message = null, $when = null)
  946. {
  947. $extra = array_filter(['on' => $when, 'message' => $message]);
  948. return $this->add($field, 'numeric', $extra + [
  949. 'rule' => 'numeric'
  950. ]);
  951. }
  952. /**
  953. * Add a natural number validation rule to a field.
  954. *
  955. * @param string $field The field you want to apply the rule to.
  956. * @param string|null $message The error message when the rule fails.
  957. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  958. * true when the validation rule should be applied.
  959. * @see \Cake\Validation\Validation::naturalNumber()
  960. * @return $this
  961. */
  962. public function naturalNumber($field, $message = null, $when = null)
  963. {
  964. $extra = array_filter(['on' => $when, 'message' => $message]);
  965. return $this->add($field, 'naturalNumber', $extra + [
  966. 'rule' => ['naturalNumber', false]
  967. ]);
  968. }
  969. /**
  970. * Add a validation rule to ensure a field is a non negative integer.
  971. *
  972. * @param string $field The field you want to apply the rule to.
  973. * @param string|null $message The error message when the rule fails.
  974. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  975. * true when the validation rule should be applied.
  976. * @see \Cake\Validation\Validation::naturalNumber()
  977. * @return $this
  978. */
  979. public function nonNegativeInteger($field, $message = null, $when = null)
  980. {
  981. $extra = array_filter(['on' => $when, 'message' => $message]);
  982. return $this->add($field, 'nonNegativeInteger', $extra + [
  983. 'rule' => ['naturalNumber', true]
  984. ]);
  985. }
  986. /**
  987. * Add a validation rule to ensure a field is within a numeric range
  988. *
  989. * @param string $field The field you want to apply the rule to.
  990. * @param array $range The inclusive upper and lower bounds of the valid range.
  991. * @param string|null $message The error message when the rule fails.
  992. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  993. * true when the validation rule should be applied.
  994. * @see \Cake\Validation\Validation::range()
  995. * @return $this
  996. */
  997. public function range($field, array $range, $message = null, $when = null)
  998. {
  999. if (count($range) !== 2) {
  1000. throw new InvalidArgumentException('The $range argument requires 2 numbers');
  1001. }
  1002. $extra = array_filter(['on' => $when, 'message' => $message]);
  1003. return $this->add($field, 'range', $extra + [
  1004. 'rule' => ['range', array_shift($range), array_shift($range)]
  1005. ]);
  1006. }
  1007. /**
  1008. * Add a validation rule to ensure a field is a URL.
  1009. *
  1010. * This validator does not require a protocol.
  1011. *
  1012. * @param string $field The field you want to apply the rule to.
  1013. * @param string|null $message The error message when the rule fails.
  1014. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1015. * true when the validation rule should be applied.
  1016. * @see \Cake\Validation\Validation::url()
  1017. * @return $this
  1018. */
  1019. public function url($field, $message = null, $when = null)
  1020. {
  1021. $extra = array_filter(['on' => $when, 'message' => $message]);
  1022. return $this->add($field, 'url', $extra + [
  1023. 'rule' => ['url', false]
  1024. ]);
  1025. }
  1026. /**
  1027. * Add a validation rule to ensure a field is a URL.
  1028. *
  1029. * This validator requires the URL to have a protocol.
  1030. *
  1031. * @param string $field The field you want to apply the rule to.
  1032. * @param string|null $message The error message when the rule fails.
  1033. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1034. * true when the validation rule should be applied.
  1035. * @see \Cake\Validation\Validation::url()
  1036. * @return $this
  1037. */
  1038. public function urlWithProtocol($field, $message = null, $when = null)
  1039. {
  1040. $extra = array_filter(['on' => $when, 'message' => $message]);
  1041. return $this->add($field, 'urlWithProtocol', $extra + [
  1042. 'rule' => ['url', true]
  1043. ]);
  1044. }
  1045. /**
  1046. * Add a validation rule to ensure the field value is within a whitelist.
  1047. *
  1048. * @param string $field The field you want to apply the rule to.
  1049. * @param array $list The list of valid options.
  1050. * @param string|null $message The error message when the rule fails.
  1051. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1052. * true when the validation rule should be applied.
  1053. * @see \Cake\Validation\Validation::inList()
  1054. * @return $this
  1055. */
  1056. public function inList($field, array $list, $message = null, $when = null)
  1057. {
  1058. $extra = array_filter(['on' => $when, 'message' => $message]);
  1059. return $this->add($field, 'inList', $extra + [
  1060. 'rule' => ['inList', $list]
  1061. ]);
  1062. }
  1063. /**
  1064. * Add a validation rule to ensure the field is a UUID
  1065. *
  1066. * @param string $field The field you want to apply the rule to.
  1067. * @param string|null $message The error message when the rule fails.
  1068. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1069. * true when the validation rule should be applied.
  1070. * @see \Cake\Validation\Validation::uuid()
  1071. * @return $this
  1072. */
  1073. public function uuid($field, $message = null, $when = null)
  1074. {
  1075. $extra = array_filter(['on' => $when, 'message' => $message]);
  1076. return $this->add($field, 'uuid', $extra + [
  1077. 'rule' => 'uuid'
  1078. ]);
  1079. }
  1080. /**
  1081. * Add a validation rule to ensure the field is an uploaded file
  1082. *
  1083. * For options see Cake\Validation\Validation::uploadedFile()
  1084. *
  1085. * @param string $field The field you want to apply the rule to.
  1086. * @param array $options An array of options.
  1087. * @param string|null $message The error message when the rule fails.
  1088. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1089. * true when the validation rule should be applied.
  1090. * @see \Cake\Validation\Validation::uploadedFile()
  1091. * @return $this
  1092. */
  1093. public function uploadedFile($field, array $options, $message = null, $when = null)
  1094. {
  1095. $extra = array_filter(['on' => $when, 'message' => $message]);
  1096. return $this->add($field, 'uploadedFile', $extra + [
  1097. 'rule' => ['uploadedFile', $options]
  1098. ]);
  1099. }
  1100. /**
  1101. * Add a validation rule to ensure the field is a lat/long tuple.
  1102. *
  1103. * e.g. `<lat>, <lng>`
  1104. *
  1105. * @param string $field The field you want to apply the rule to.
  1106. * @param string|null $message The error message when the rule fails.
  1107. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1108. * true when the validation rule should be applied.
  1109. * @see \Cake\Validation\Validation::uuid()
  1110. * @return $this
  1111. */
  1112. public function latLong($field, $message = null, $when = null)
  1113. {
  1114. $extra = array_filter(['on' => $when, 'message' => $message]);
  1115. return $this->add($field, 'latLong', $extra + [
  1116. 'rule' => 'geoCoordinate'
  1117. ]);
  1118. }
  1119. /**
  1120. * Add a validation rule to ensure the field is a latitude.
  1121. *
  1122. * @param string $field The field you want to apply the rule to.
  1123. * @param string|null $message The error message when the rule fails.
  1124. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1125. * true when the validation rule should be applied.
  1126. * @see \Cake\Validation\Validation::latitude()
  1127. * @return $this
  1128. */
  1129. public function latitude($field, $message = null, $when = null)
  1130. {
  1131. $extra = array_filter(['on' => $when, 'message' => $message]);
  1132. return $this->add($field, 'latitude', $extra + [
  1133. 'rule' => 'latitude'
  1134. ]);
  1135. }
  1136. /**
  1137. * Add a validation rule to ensure the field is a longitude.
  1138. *
  1139. * @param string $field The field you want to apply the rule to.
  1140. * @param string|null $message The error message when the rule fails.
  1141. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1142. * true when the validation rule should be applied.
  1143. * @see \Cake\Validation\Validation::longitude()
  1144. * @return $this
  1145. */
  1146. public function longitude($field, $message = null, $when = null)
  1147. {
  1148. $extra = array_filter(['on' => $when, 'message' => $message]);
  1149. return $this->add($field, 'longitude', $extra + [
  1150. 'rule' => 'longitude'
  1151. ]);
  1152. }
  1153. /**
  1154. * Add a validation rule to ensure a field contains only ascii bytes
  1155. *
  1156. * @param string $field The field you want to apply the rule to.
  1157. * @param string|null $message The error message when the rule fails.
  1158. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1159. * true when the validation rule should be applied.
  1160. * @see \Cake\Validation\Validation::ascii()
  1161. * @return $this
  1162. */
  1163. public function ascii($field, $message = null, $when = null)
  1164. {
  1165. $extra = array_filter(['on' => $when, 'message' => $message]);
  1166. return $this->add($field, 'ascii', $extra + [
  1167. 'rule' => 'ascii'
  1168. ]);
  1169. }
  1170. /**
  1171. * Add a validation rule to ensure a field contains only BMP utf8 bytes
  1172. *
  1173. * @param string $field The field you want to apply the rule to.
  1174. * @param string|null $message The error message when the rule fails.
  1175. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1176. * true when the validation rule should be applied.
  1177. * @see \Cake\Validation\Validation::utf8()
  1178. * @return $this
  1179. */
  1180. public function utf8($field, $message = null, $when = null)
  1181. {
  1182. $extra = array_filter(['on' => $when, 'message' => $message]);
  1183. return $this->add($field, 'utf8', $extra + [
  1184. 'rule' => ['utf8', ['extended' => false]]
  1185. ]);
  1186. }
  1187. /**
  1188. * Add a validation rule to ensure a field contains only utf8 bytes.
  1189. *
  1190. * This rule will accept 3 and 4 byte UTF8 sequences, which are necessary for emoji.
  1191. *
  1192. * @param string $field The field you want to apply the rule to.
  1193. * @param string|null $message The error message when the rule fails.
  1194. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1195. * true when the validation rule should be applied.
  1196. * @see \Cake\Validation\Validation::utf8()
  1197. * @return $this
  1198. */
  1199. public function utf8Extended($field, $message = null, $when = null)
  1200. {
  1201. $extra = array_filter(['on' => $when, 'message' => $message]);
  1202. return $this->add($field, 'utf8Extended', $extra + [
  1203. 'rule' => ['utf8', ['extended' => true]]
  1204. ]);
  1205. }
  1206. /**
  1207. * Add a validation rule to ensure a field is an integer value.
  1208. *
  1209. * @param string $field The field you want to apply the rule to.
  1210. * @param string|null $message The error message when the rule fails.
  1211. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1212. * true when the validation rule should be applied.
  1213. * @see \Cake\Validation\Validation::isInteger()
  1214. * @return $this
  1215. */
  1216. public function integer($field, $message = null, $when = null)
  1217. {
  1218. $extra = array_filter(['on' => $when, 'message' => $message]);
  1219. return $this->add($field, 'integer', $extra + [
  1220. 'rule' => 'isInteger'
  1221. ]);
  1222. }
  1223. /**
  1224. * Add a validation rule to ensure that a field contains an array.
  1225. *
  1226. * @param string $field The field you want to apply the rule to.
  1227. * @param string|null $message The error message when the rule fails.
  1228. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1229. * true when the validation rule should be applied.
  1230. * @return $this
  1231. */
  1232. public function isArray($field, $message = null, $when = null)
  1233. {
  1234. $extra = array_filter(['on' => $when, 'message' => $message]);
  1235. return $this->add($field, 'isArray', $extra + [
  1236. 'rule' => 'isArray'
  1237. ]);
  1238. }
  1239. /**
  1240. * Add a validation rule for a multiple select. Comparison is case sensitive by default.
  1241. *
  1242. * @param string $field The field you want to apply the rule to.
  1243. * @param array $options The options for the validator. Includes the options defined in
  1244. * \Cake\Validation\Validation::multiple() and the `caseInsensitive` parameter.
  1245. * @param string|null $message The error message when the rule fails.
  1246. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1247. * true when the validation rule should be applied.
  1248. * @see \Cake\Validation\Validation::multiple()
  1249. * @return $this
  1250. */
  1251. public function multipleOptions($field, array $options = [], $message = null, $when = null)
  1252. {
  1253. $extra = array_filter(['on' => $when, 'message' => $message]);
  1254. $caseInsensitive = isset($options['caseInsenstive']) ? $options['caseInsensitive'] : false;
  1255. unset($options['caseInsensitive']);
  1256. return $this->add($field, 'multipleOptions', $extra + [
  1257. 'rule' => ['multiple', $options, $caseInsensitive]
  1258. ]);
  1259. }
  1260. /**
  1261. * Add a validation rule to ensure that a field is an array containing at least
  1262. * the specified amount of elements
  1263. *
  1264. * @param string $field The field you want to apply the rule to.
  1265. * @param int $count The number of elements the array should at least have
  1266. * @param string|null $message The error message when the rule fails.
  1267. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1268. * true when the validation rule should be applied.
  1269. * @see \Cake\Validation\Validation::numElements()
  1270. * @return $this
  1271. */
  1272. public function hasAtLeast($field, $count, $message = null, $when = null)
  1273. {
  1274. $extra = array_filter(['on' => $when, 'message' => $message]);
  1275. return $this->add($field, 'hasAtLeast', $extra + [
  1276. 'rule' => function ($value) use ($count) {
  1277. if (is_array($value) && isset($value['_ids'])) {
  1278. $value = $value['_ids'];
  1279. }
  1280. return Validation::numElements($value, '>=', $count);
  1281. }
  1282. ]);
  1283. }
  1284. /**
  1285. * Add a validation rule to ensure that a field is an array containing at most
  1286. * the specified amount of elements
  1287. *
  1288. * @param string $field The field you want to apply the rule to.
  1289. * @param int $count The number maximim amount of elements the field should have
  1290. * @param string|null $message The error message when the rule fails.
  1291. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1292. * true when the validation rule should be applied.
  1293. * @see \Cake\Validation\Validation::numElements()
  1294. * @return $this
  1295. */
  1296. public function hasAtMost($field, $count, $message = null, $when = null)
  1297. {
  1298. $extra = array_filter(['on' => $when, 'message' => $message]);
  1299. return $this->add($field, 'hasAtMost', $extra + [
  1300. 'rule' => function ($value) use ($count) {
  1301. if (is_array($value) && isset($value['_ids'])) {
  1302. $value = $value['_ids'];
  1303. }
  1304. return Validation::numElements($value, '<=', $count);
  1305. }
  1306. ]);
  1307. }
  1308. /**
  1309. * Returns whether or not a field can be left empty for a new or already existing
  1310. * record.
  1311. *
  1312. * @param string $field Field name.
  1313. * @param bool $newRecord whether the data to be validated is new or to be updated.
  1314. * @return bool
  1315. */
  1316. public function isEmptyAllowed($field, $newRecord)
  1317. {
  1318. $providers = $this->_providers;
  1319. $data = [];
  1320. $context = compact('data', 'newRecord', 'field', 'providers');
  1321. return $this->_canBeEmpty($this->field($field), $context);
  1322. }
  1323. /**
  1324. * Returns whether or not a field can be left out for a new or already existing
  1325. * record.
  1326. *
  1327. * @param string $field Field name.
  1328. * @param bool $newRecord Whether the data to be validated is new or to be updated.
  1329. * @return bool
  1330. */
  1331. public function isPresenceRequired($field, $newRecord)
  1332. {
  1333. $providers = $this->_providers;
  1334. $data = [];
  1335. $context = compact('data', 'newRecord', 'field', 'providers');
  1336. return !$this->_checkPresence($this->field($field), $context);
  1337. }
  1338. /**
  1339. * Returns false if any validation for the passed rule set should be stopped
  1340. * due to the field missing in the data array
  1341. *
  1342. * @param \Cake\Validation\ValidationSet $field The set of rules for a field.
  1343. * @param array $context A key value list of data containing the validation context.
  1344. * @return bool
  1345. */
  1346. protected function _checkPresence($field, $context)
  1347. {
  1348. $required = $field->isPresenceRequired();
  1349. if (!is_string($required) && is_callable($required)) {
  1350. return !$required($context);
  1351. }
  1352. $newRecord = $context['newRecord'];
  1353. if (in_array($required, ['create', 'update'], true)) {
  1354. return (
  1355. ($required === 'create' && !$newRecord) ||
  1356. ($required === 'update' && $newRecord)
  1357. );
  1358. }
  1359. return !$required;
  1360. }
  1361. /**
  1362. * Returns whether the field can be left blank according to `allowEmpty`
  1363. *
  1364. * @param \Cake\Validation\ValidationSet $field the set of rules for a field
  1365. * @param array $context a key value list of data containing the validation context.
  1366. * @return bool
  1367. */
  1368. protected function _canBeEmpty($field, $context)
  1369. {
  1370. $allowed = $field->isEmptyAllowed();
  1371. if (!is_string($allowed) && is_callable($allowed)) {
  1372. return $allowed($context);
  1373. }
  1374. $newRecord = $context['newRecord'];
  1375. if (in_array($allowed, ['create', 'update'], true)) {
  1376. $allowed = (
  1377. ($allowed === 'create' && $newRecord) ||
  1378. ($allowed === 'update' && !$newRecord)
  1379. );
  1380. }
  1381. return $allowed;
  1382. }
  1383. /**
  1384. * Returns true if the field is empty in the passed data array
  1385. *
  1386. * @param mixed $data value to check against
  1387. * @return bool
  1388. */
  1389. protected function _fieldIsEmpty($data)
  1390. {
  1391. if (empty($data) && $data !== '0' && $data !== false && $data !== 0 && $data !== 0.0) {
  1392. return true;
  1393. }
  1394. $isArray = is_array($data);
  1395. if ($isArray && (isset($data['year']) || isset($data['hour']))) {
  1396. $value = implode('', $data);
  1397. return strlen($value) === 0;
  1398. }
  1399. if ($isArray && isset($data['name'], $data['type'], $data['tmp_name'], $data['error'])) {
  1400. return (int)$data['error'] === UPLOAD_ERR_NO_FILE;
  1401. }
  1402. return false;
  1403. }
  1404. /**
  1405. * Iterates over each rule in the validation set and collects the errors resulting
  1406. * from executing them
  1407. *
  1408. * @param string $field The name of the field that is being processed
  1409. * @param \Cake\Validation\ValidationSet $rules the list of rules for a field
  1410. * @param array $data the full data passed to the validator
  1411. * @param bool $newRecord whether is it a new record or an existing one
  1412. * @return array
  1413. */
  1414. protected function _processRules($field, ValidationSet $rules, $data, $newRecord)
  1415. {
  1416. $errors = [];
  1417. // Loading default provider in case there is none
  1418. $this->provider('default');
  1419. $message = 'The provided value is invalid';
  1420. if ($this->_useI18n) {
  1421. $message = __d('cake', 'The provided value is invalid');
  1422. }
  1423. foreach ($rules as $name => $rule) {
  1424. $result = $rule->process($data[$field], $this->_providers, compact('newRecord', 'data', 'field'));
  1425. if ($result === true) {
  1426. continue;
  1427. }
  1428. $errors[$name] = $message;
  1429. if (is_array($result) && $name === static::NESTED) {
  1430. $errors = $result;
  1431. }
  1432. if (is_string($result)) {
  1433. $errors[$name] = $result;
  1434. }
  1435. if ($rule->isLast()) {
  1436. break;
  1437. }
  1438. }
  1439. return $errors;
  1440. }
  1441. /**
  1442. * Get the printable version of this object.
  1443. *
  1444. * @return array
  1445. */
  1446. public function __debugInfo()
  1447. {
  1448. $fields = [];
  1449. foreach ($this->_fields as $name => $fieldSet) {
  1450. $fields[$name] = [
  1451. 'isPresenceRequired' => $fieldSet->isPresenceRequired(),
  1452. 'isEmptyAllowed' => $fieldSet->isEmptyAllowed(),
  1453. 'rules' => array_keys($fieldSet->rules()),
  1454. ];
  1455. }
  1456. return [
  1457. '_presenceMessages' => $this->_presenceMessages,
  1458. '_allowEmptyMessages' => $this->_allowEmptyMessages,
  1459. '_useI18n' => $this->_useI18n,
  1460. '_providers' => array_keys($this->_providers),
  1461. '_fields' => $fields
  1462. ];
  1463. }
  1464. }