Validator.php 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.2.0
  13. * @license https://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 https://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. * A flag for allowEmptyFor()
  39. *
  40. * When an empty string is given, it will be recognized as empty.
  41. *
  42. * @var int
  43. */
  44. const EMPTY_STRING = 1;
  45. /**
  46. * A flag for allowEmptyFor()
  47. *
  48. * When an empty array is given, it will be recognized as empty.
  49. *
  50. * @var int
  51. */
  52. const EMPTY_ARRAY = 2;
  53. /**
  54. * A flag for allowEmptyFor()
  55. *
  56. * When an array is given, if it has at least the `name`, `type`, `tmp_name` and `error` keys,
  57. * and the value of `error` is equal to `UPLOAD_ERR_NO_FILE`, the value will be recognized as
  58. * empty.
  59. *
  60. * @var int
  61. */
  62. const EMPTY_FILE = 4;
  63. /**
  64. * A flag for allowEmptyFor()
  65. *
  66. * When an array is given, if it contains the `year` key, and only empty strings
  67. * or null values, it will be recognized as empty.
  68. *
  69. * @var int
  70. */
  71. const EMPTY_DATE = 8;
  72. /**
  73. * A flag for allowEmptyFor()
  74. *
  75. * When an array is given, if it contains the `hour` key, and only empty strings
  76. * or null values, it will be recognized as empty.
  77. *
  78. * @var int
  79. */
  80. const EMPTY_TIME = 16;
  81. /**
  82. * A combination of the all EMPTY_* flags
  83. *
  84. * @var int
  85. */
  86. const EMPTY_ALL = self::EMPTY_STRING | self::EMPTY_ARRAY | self::EMPTY_FILE | self::EMPTY_DATE | self::EMPTY_TIME;
  87. /**
  88. * Holds the ValidationSet objects array
  89. *
  90. * @var array
  91. */
  92. protected $_fields = [];
  93. /**
  94. * An associative array of objects or classes containing methods
  95. * used for validation
  96. *
  97. * @var array
  98. */
  99. protected $_providers = [];
  100. /**
  101. * An associative array of objects or classes used as a default provider list
  102. *
  103. * @var array
  104. */
  105. protected static $_defaultProviders = [];
  106. /**
  107. * Contains the validation messages associated with checking the presence
  108. * for each corresponding field.
  109. *
  110. * @var array
  111. */
  112. protected $_presenceMessages = [];
  113. /**
  114. * Whether or not to use I18n functions for translating default error messages
  115. *
  116. * @var bool
  117. */
  118. protected $_useI18n = false;
  119. /**
  120. * Contains the validation messages associated with checking the emptiness
  121. * for each corresponding field.
  122. *
  123. * @var array
  124. */
  125. protected $_allowEmptyMessages = [];
  126. /**
  127. * Contains the flags which specify what is empty for each corresponding field.
  128. *
  129. * @var array
  130. */
  131. protected $_allowEmptyFlags = [];
  132. /**
  133. * Constructor
  134. *
  135. */
  136. public function __construct()
  137. {
  138. $this->_useI18n = function_exists('__d');
  139. $this->_providers = self::$_defaultProviders;
  140. }
  141. /**
  142. * Returns an array of fields that have failed validation. On the current model. This method will
  143. * actually run validation rules over data, not just return the messages.
  144. *
  145. * @param array $data The data to be checked for errors
  146. * @param bool $newRecord whether the data to be validated is new or to be updated.
  147. * @return array Array of invalid fields
  148. */
  149. public function errors(array $data, $newRecord = true)
  150. {
  151. $errors = [];
  152. foreach ($this->_fields as $name => $field) {
  153. $keyPresent = array_key_exists($name, $data);
  154. $providers = $this->_providers;
  155. $context = compact('data', 'newRecord', 'field', 'providers');
  156. if (!$keyPresent && !$this->_checkPresence($field, $context)) {
  157. $errors[$name]['_required'] = $this->getRequiredMessage($name);
  158. continue;
  159. }
  160. if (!$keyPresent) {
  161. continue;
  162. }
  163. $canBeEmpty = $this->_canBeEmpty($field, $context);
  164. $flags = static::EMPTY_ALL;
  165. if (isset($this->_allowEmptyFlags[$name])) {
  166. $flags = $this->_allowEmptyFlags[$name];
  167. }
  168. $isEmpty = $this->isEmpty($data[$name], $flags);
  169. if (!$canBeEmpty && $isEmpty) {
  170. $errors[$name]['_empty'] = $this->getNotEmptyMessage($name);
  171. continue;
  172. }
  173. if ($isEmpty) {
  174. continue;
  175. }
  176. $result = $this->_processRules($name, $field, $data, $newRecord);
  177. if ($result) {
  178. $errors[$name] = $result;
  179. }
  180. }
  181. return $errors;
  182. }
  183. /**
  184. * Returns a ValidationSet object containing all validation rules for a field, if
  185. * passed a ValidationSet as second argument, it will replace any other rule set defined
  186. * before
  187. *
  188. * @param string $name [optional] The fieldname to fetch.
  189. * @param \Cake\Validation\ValidationSet|null $set The set of rules for field
  190. * @return \Cake\Validation\ValidationSet
  191. */
  192. public function field($name, ValidationSet $set = null)
  193. {
  194. if (empty($this->_fields[$name])) {
  195. $set = $set ?: new ValidationSet();
  196. $this->_fields[$name] = $set;
  197. }
  198. return $this->_fields[$name];
  199. }
  200. /**
  201. * Check whether or not a validator contains any rules for the given field.
  202. *
  203. * @param string $name The field name to check.
  204. * @return bool
  205. */
  206. public function hasField($name)
  207. {
  208. return isset($this->_fields[$name]);
  209. }
  210. /**
  211. * Associates an object to a name so it can be used as a provider. Providers are
  212. * objects or class names that can contain methods used during validation of for
  213. * deciding whether a validation rule can be applied. All validation methods,
  214. * when called will receive the full list of providers stored in this validator.
  215. *
  216. * @param string $name The name under which the provider should be set.
  217. * @param object|string $object Provider object or class name.
  218. * @return $this
  219. */
  220. public function setProvider($name, $object)
  221. {
  222. $this->_providers[$name] = $object;
  223. return $this;
  224. }
  225. /**
  226. * Returns the provider stored under that name if it exists.
  227. *
  228. * @param string $name The name under which the provider should be set.
  229. * @return object|string|null
  230. * @throws \ReflectionException
  231. */
  232. public function getProvider($name)
  233. {
  234. if (isset($this->_providers[$name])) {
  235. return $this->_providers[$name];
  236. }
  237. if ($name !== 'default') {
  238. return null;
  239. }
  240. $this->_providers[$name] = new RulesProvider();
  241. return $this->_providers[$name];
  242. }
  243. /**
  244. * Returns the default provider stored under that name if it exists.
  245. *
  246. * @param string $name The name under which the provider should be retrieved.
  247. * @return object|string|null
  248. */
  249. public static function getDefaultProvider($name)
  250. {
  251. if (!isset(self::$_defaultProviders[$name])) {
  252. return null;
  253. }
  254. return self::$_defaultProviders[$name];
  255. }
  256. /**
  257. * Associates an object to a name so it can be used as a default provider.
  258. *
  259. * @param string $name The name under which the provider should be set.
  260. * @param object|string $object Provider object or class name.
  261. * @return void
  262. */
  263. public static function addDefaultProvider($name, $object)
  264. {
  265. self::$_defaultProviders[$name] = $object;
  266. }
  267. /**
  268. * Get the list of default providers.
  269. *
  270. * @return string[]
  271. */
  272. public static function getDefaultProviders()
  273. {
  274. return array_keys(self::$_defaultProviders);
  275. }
  276. /**
  277. * Associates an object to a name so it can be used as a provider. Providers are
  278. * objects or class names that can contain methods used during validation of for
  279. * deciding whether a validation rule can be applied. All validation methods,
  280. * when called will receive the full list of providers stored in this validator.
  281. *
  282. * If called with no arguments, it will return the provider stored under that name if
  283. * it exists, otherwise it returns this instance of chaining.
  284. *
  285. * @deprecated 3.4.0 Use setProvider()/getProvider() instead.
  286. * @param string $name The name under which the provider should be set.
  287. * @param null|object|string $object Provider object or class name.
  288. * @return $this|object|string|null
  289. */
  290. public function provider($name, $object = null)
  291. {
  292. deprecationWarning(
  293. 'Validator::provider() is deprecated. ' .
  294. 'Use Validator::setProvider()/getProvider() instead.'
  295. );
  296. if ($object !== null) {
  297. return $this->setProvider($name, $object);
  298. }
  299. return $this->getProvider($name);
  300. }
  301. /**
  302. * Get the list of providers in this validator.
  303. *
  304. * @return string[]
  305. */
  306. public function providers()
  307. {
  308. return array_keys($this->_providers);
  309. }
  310. /**
  311. * Returns whether a rule set is defined for a field or not
  312. *
  313. * @param string $field name of the field to check
  314. * @return bool
  315. */
  316. public function offsetExists($field)
  317. {
  318. return isset($this->_fields[$field]);
  319. }
  320. /**
  321. * Returns the rule set for a field
  322. *
  323. * @param string $field name of the field to check
  324. * @return \Cake\Validation\ValidationSet
  325. */
  326. public function offsetGet($field)
  327. {
  328. return $this->field($field);
  329. }
  330. /**
  331. * Sets the rule set for a field
  332. *
  333. * @param string $field name of the field to set
  334. * @param array|\Cake\Validation\ValidationSet $rules set of rules to apply to field
  335. * @return void
  336. */
  337. public function offsetSet($field, $rules)
  338. {
  339. if (!$rules instanceof ValidationSet) {
  340. $set = new ValidationSet();
  341. foreach ((array)$rules as $name => $rule) {
  342. $set->add($name, $rule);
  343. }
  344. }
  345. $this->_fields[$field] = $rules;
  346. }
  347. /**
  348. * Unsets the rule set for a field
  349. *
  350. * @param string $field name of the field to unset
  351. * @return void
  352. */
  353. public function offsetUnset($field)
  354. {
  355. unset($this->_fields[$field]);
  356. }
  357. /**
  358. * Returns an iterator for each of the fields to be validated
  359. *
  360. * @return \ArrayIterator
  361. */
  362. public function getIterator()
  363. {
  364. return new ArrayIterator($this->_fields);
  365. }
  366. /**
  367. * Returns the number of fields having validation rules
  368. *
  369. * @return int
  370. */
  371. public function count()
  372. {
  373. return count($this->_fields);
  374. }
  375. /**
  376. * Adds a new rule to a field's rule set. If second argument is an array
  377. * then rules list for the field will be replaced with second argument and
  378. * third argument will be ignored.
  379. *
  380. * ### Example:
  381. *
  382. * ```
  383. * $validator
  384. * ->add('title', 'required', ['rule' => 'notBlank'])
  385. * ->add('user_id', 'valid', ['rule' => 'numeric', 'message' => 'Invalid User'])
  386. *
  387. * $validator->add('password', [
  388. * 'size' => ['rule' => ['lengthBetween', 8, 20]],
  389. * 'hasSpecialCharacter' => ['rule' => 'validateSpecialchar', 'message' => 'not valid']
  390. * ]);
  391. * ```
  392. *
  393. * @param string $field The name of the field from which the rule will be added
  394. * @param array|string $name The alias for a single rule or multiple rules array
  395. * @param array|\Cake\Validation\ValidationRule $rule the rule to add
  396. * @return $this
  397. */
  398. public function add($field, $name, $rule = [])
  399. {
  400. $validationSet = $this->field($field);
  401. if (!is_array($name)) {
  402. $rules = [$name => $rule];
  403. } else {
  404. $rules = $name;
  405. }
  406. foreach ($rules as $name => $rule) {
  407. if (is_array($rule)) {
  408. $rule += ['rule' => $name];
  409. }
  410. $validationSet->add($name, $rule);
  411. }
  412. return $this;
  413. }
  414. /**
  415. * Adds a nested validator.
  416. *
  417. * Nesting validators allows you to define validators for array
  418. * types. For example, nested validators are ideal when you want to validate a
  419. * sub-document, or complex array type.
  420. *
  421. * This method assumes that the sub-document has a 1:1 relationship with the parent.
  422. *
  423. * The providers of the parent validator will be synced into the nested validator, when
  424. * errors are checked. This ensures that any validation rule providers connected
  425. * in the parent will have the same values in the nested validator when rules are evaluated.
  426. *
  427. * @param string $field The root field for the nested validator.
  428. * @param \Cake\Validation\Validator $validator The nested validator.
  429. * @param string|null $message The error message when the rule fails.
  430. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  431. * true when the validation rule should be applied.
  432. * @return $this
  433. */
  434. public function addNested($field, Validator $validator, $message = null, $when = null)
  435. {
  436. $extra = array_filter(['message' => $message, 'on' => $when]);
  437. $validationSet = $this->field($field);
  438. $validationSet->add(static::NESTED, $extra + ['rule' => function ($value, $context) use ($validator, $message) {
  439. if (!is_array($value)) {
  440. return false;
  441. }
  442. foreach ($this->providers() as $provider) {
  443. $validator->setProvider($provider, $this->getProvider($provider));
  444. }
  445. $errors = $validator->errors($value, $context['newRecord']);
  446. $message = $message ? [static::NESTED => $message] : [];
  447. return empty($errors) ? true : $errors + $message;
  448. }]);
  449. return $this;
  450. }
  451. /**
  452. * Adds a nested validator.
  453. *
  454. * Nesting validators allows you to define validators for array
  455. * types. For example, nested validators are ideal when you want to validate many
  456. * similar sub-documents or complex array types.
  457. *
  458. * This method assumes that the sub-document has a 1:N relationship with the parent.
  459. *
  460. * The providers of the parent validator will be synced into the nested validator, when
  461. * errors are checked. This ensures that any validation rule providers connected
  462. * in the parent will have the same values in the nested validator when rules are evaluated.
  463. *
  464. * @param string $field The root field for the nested validator.
  465. * @param \Cake\Validation\Validator $validator The nested validator.
  466. * @param string|null $message The error message when the rule fails.
  467. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  468. * true when the validation rule should be applied.
  469. * @return $this
  470. */
  471. public function addNestedMany($field, Validator $validator, $message = null, $when = null)
  472. {
  473. $extra = array_filter(['message' => $message, 'on' => $when]);
  474. $validationSet = $this->field($field);
  475. $validationSet->add(static::NESTED, $extra + ['rule' => function ($value, $context) use ($validator, $message) {
  476. if (!is_array($value)) {
  477. return false;
  478. }
  479. foreach ($this->providers() as $provider) {
  480. $validator->setProvider($provider, $this->getProvider($provider));
  481. }
  482. $errors = [];
  483. foreach ($value as $i => $row) {
  484. if (!is_array($row)) {
  485. return false;
  486. }
  487. $check = $validator->errors($row, $context['newRecord']);
  488. if (!empty($check)) {
  489. $errors[$i] = $check;
  490. }
  491. }
  492. $message = $message ? [static::NESTED => $message] : [];
  493. return empty($errors) ? true : $errors + $message;
  494. }]);
  495. return $this;
  496. }
  497. /**
  498. * Removes a rule from the set by its name
  499. *
  500. * ### Example:
  501. *
  502. * ```
  503. * $validator
  504. * ->remove('title', 'required')
  505. * ->remove('user_id')
  506. * ```
  507. *
  508. * @param string $field The name of the field from which the rule will be removed
  509. * @param string|null $rule the name of the rule to be removed
  510. * @return $this
  511. */
  512. public function remove($field, $rule = null)
  513. {
  514. if ($rule === null) {
  515. unset($this->_fields[$field]);
  516. } else {
  517. $this->field($field)->remove($rule);
  518. }
  519. return $this;
  520. }
  521. /**
  522. * Sets whether a field is required to be present in data array.
  523. * You can also pass array. Using an array will let you provide the following
  524. * keys:
  525. *
  526. * - `mode` individual mode for field
  527. * - `message` individual error message for field
  528. *
  529. * You can also set mode and message for all passed fields, the individual
  530. * setting takes precedence over group settings.
  531. *
  532. * @param string|array $field the name of the field or list of fields.
  533. * @param bool|string|callable $mode Valid values are true, false, 'create', 'update'.
  534. * If a callable is passed then the field will be required only when the callback
  535. * returns true.
  536. * @param string|null $message The message to show if the field presence validation fails.
  537. * @return $this
  538. */
  539. public function requirePresence($field, $mode = true, $message = null)
  540. {
  541. $defaults = [
  542. 'mode' => $mode,
  543. 'message' => $message
  544. ];
  545. if (!is_array($field)) {
  546. $field = $this->_convertValidatorToArray($field, $defaults);
  547. }
  548. foreach ($field as $fieldName => $setting) {
  549. $settings = $this->_convertValidatorToArray($fieldName, $defaults, $setting);
  550. $fieldName = current(array_keys($settings));
  551. $this->field($fieldName)->requirePresence($settings[$fieldName]['mode']);
  552. if ($settings[$fieldName]['message']) {
  553. $this->_presenceMessages[$fieldName] = $settings[$fieldName]['message'];
  554. }
  555. }
  556. return $this;
  557. }
  558. /**
  559. * Allows a field to be empty. You can also pass array.
  560. * Using an array will let you provide the following keys:
  561. *
  562. * - `when` individual when condition for field
  563. * - 'message' individual message for field
  564. *
  565. * You can also set when and message for all passed fields, the individual setting
  566. * takes precedence over group settings.
  567. *
  568. * This is the opposite of notEmpty() which requires a field to not be empty.
  569. * By using $mode equal to 'create' or 'update', you can allow fields to be empty
  570. * when records are first created, or when they are updated.
  571. *
  572. * ### Example:
  573. *
  574. * ```
  575. * // Email can be empty
  576. * $validator->allowEmpty('email');
  577. *
  578. * // Email can be empty on create
  579. * $validator->allowEmpty('email', 'create');
  580. *
  581. * // Email can be empty on update
  582. * $validator->allowEmpty('email', 'update');
  583. *
  584. * // Email and subject can be empty on update
  585. * $validator->allowEmpty(['email', 'subject'], 'update');
  586. *
  587. * // Email can be always empty, subject and content can be empty on update.
  588. * $validator->allowEmpty(
  589. * [
  590. * 'email' => [
  591. * 'when' => true
  592. * ],
  593. * 'content' => [
  594. * 'message' => 'Content cannot be empty'
  595. * ],
  596. * 'subject'
  597. * ],
  598. * 'update'
  599. * );
  600. * ```
  601. *
  602. * It is possible to conditionally allow emptiness on a field by passing a callback
  603. * as a second argument. The callback will receive the validation context array as
  604. * argument:
  605. *
  606. * ```
  607. * $validator->allowEmpty('email', function ($context) {
  608. * return !$context['newRecord'] || $context['data']['role'] === 'admin';
  609. * });
  610. * ```
  611. *
  612. * This method will correctly detect empty file uploads and date/time/datetime fields.
  613. *
  614. * Because this and `notEmpty()` modify the same internal state, the last
  615. * method called will take precedence.
  616. *
  617. * @deprecated 3.7.0 Use allowEmptyString(), allowEmptyArray(), allowEmptyFile(),
  618. * allowEmptyDate(), allowEmptyTime() or allowEmptyDateTime() instead.
  619. * @param string|array $field the name of the field or a list of fields
  620. * @param bool|string|callable $when Indicates when the field is allowed to be empty
  621. * Valid values are true (always), 'create', 'update'. If a callable is passed then
  622. * the field will allowed to be empty only when the callback returns true.
  623. * @param string|null $message The message to show if the field is not
  624. * @return $this
  625. */
  626. public function allowEmpty($field, $when = true, $message = null)
  627. {
  628. $defaults = [
  629. 'when' => $when,
  630. 'message' => $message,
  631. ];
  632. if (!is_array($field)) {
  633. $field = $this->_convertValidatorToArray($field, $defaults);
  634. }
  635. foreach ($field as $fieldName => $setting) {
  636. $settings = $this->_convertValidatorToArray($fieldName, $defaults, $setting);
  637. $fieldName = array_keys($settings)[0];
  638. $this->allowEmptyFor($fieldName, null, $settings[$fieldName]['when'], $settings[$fieldName]['message']);
  639. }
  640. return $this;
  641. }
  642. /**
  643. * Indicate that a field can be empty.
  644. *
  645. * Using an array will let you provide the following keys:
  646. *
  647. * - `flags` individual flags for field
  648. * - `when` individual when condition for field
  649. * - `message` individual message for field
  650. *
  651. * You can also set flags, when and message for all passed fields, the individual
  652. * setting takes precedence over group settings.
  653. *
  654. * ### Example:
  655. *
  656. * ```
  657. * // Email can be empty
  658. * $validator->allowEmptyFor('email', Validator::EMPTY_STRING);
  659. *
  660. * // Email can be empty on create
  661. * $validator->allowEmptyFor('email', Validator::EMPTY_STRING, 'create');
  662. *
  663. * // Email can be empty on update
  664. * $validator->allowEmptyFor('email', Validator::EMPTY_STRING, 'update');
  665. * ```
  666. *
  667. * It is possible to conditionally allow emptiness on a field by passing a callback
  668. * as a second argument. The callback will receive the validation context array as
  669. * argument:
  670. *
  671. * ```
  672. * $validator->allowEmpty('email', Validator::EMPTY_STRING, function ($context) {
  673. * return !$context['newRecord'] || $context['data']['role'] === 'admin';
  674. * });
  675. * ```
  676. *
  677. * If you want to allow other kind of empty data on a field, you need to pass other
  678. * flags:
  679. *
  680. * ```
  681. * $validator->allowEmptyFor('photo', Validator::EMPTY_FILE);
  682. * $validator->allowEmptyFor('published', Validator::EMPTY_STRING | Validator::EMPTY_DATE | Validator::EMPTY_TIME);
  683. * $validator->allowEmptyFor('items', Validator::EMPTY_STRING | Validator::EMPTY_ARRAY);
  684. * ```
  685. *
  686. * You can also use convenience wrappers of this method. The following calls are the
  687. * same as above:
  688. *
  689. * ```
  690. * $validator->allowEmptyFile('photo');
  691. * $validator->allowEmptyDateTime('published');
  692. * $validator->allowEmptyArray('items');
  693. * ```
  694. *
  695. * @param string $field The name of the field.
  696. * @param int|null $flags A bitmask of EMPTY_* flags which specify what is empty
  697. * @param bool|string|callable $when Indicates when the field is allowed to be empty
  698. * Valid values are true, false, 'create', 'update'. If a callable is passed then
  699. * the field will allowed to be empty only when the callback returns true.
  700. * @param string|null $message The message to show if the field is not
  701. * @since 3.7.0
  702. * @return $this
  703. */
  704. public function allowEmptyFor($field, $flags, $when = true, $message = null)
  705. {
  706. $this->field($field)->allowEmpty($when);
  707. if ($message) {
  708. $this->_allowEmptyMessages[$field] = $message;
  709. }
  710. if ($flags !== null) {
  711. $this->_allowEmptyFlags[$field] = $flags;
  712. }
  713. return $this;
  714. }
  715. /**
  716. * Allows a field to be an empty string.
  717. *
  718. * This method is equivalent to calling allowEmptyFor() with EMPTY_STRING flag.
  719. *
  720. * @param string $field The name of the field.
  721. * @param bool|string|callable $when Indicates when the field is allowed to be empty
  722. * Valid values are true, false, 'create', 'update'. If a callable is passed then
  723. * the field will allowed to be empty only when the callback returns true.
  724. * @param string|null $message The message to show if the field is not
  725. * @return $this
  726. * @since 3.7.0
  727. * @see \Cake\Validation\Validator::allowEmptyFor() For detail usage
  728. */
  729. public function allowEmptyString($field, $when = true, $message = null)
  730. {
  731. return $this->allowEmptyFor($field, self::EMPTY_STRING, $when, $message);
  732. }
  733. /**
  734. * Requires a field to be not be an empty string.
  735. *
  736. * Opposite to allowEmptyString()
  737. *
  738. * @param string $field The name of the field.
  739. * @param string|null $message The message to show if the field is empty.
  740. * @param bool|string|callable $when Indicates when the field is not allowed
  741. * to be empty. Valid values are false (never), 'create', 'update'. If a
  742. * callable is passed then the field will be required to be not empty when
  743. * the callback returns true.
  744. * @return $this
  745. * @see \Cake\Validation\Validator::allowEmptyString()
  746. * @since 3.8.0
  747. */
  748. public function notEmptyString($field, $message = null, $when = false)
  749. {
  750. $when = $this->invertWhenClause($when);
  751. return $this->allowEmptyFor($field, self::EMPTY_STRING, $when, $message);
  752. }
  753. /**
  754. * Allows a field to be an empty array.
  755. *
  756. * This method is equivalent to calling allowEmptyFor() with EMPTY_STRING +
  757. * EMPTY_ARRAY flags.
  758. *
  759. * @param string $field The name of the field.
  760. * @param bool|string|callable $when Indicates when the field is allowed to be empty
  761. * Valid values are true, false, 'create', 'update'. If a callable is passed then
  762. * the field will allowed to be empty only when the callback returns true.
  763. * @param string|null $message The message to show if the field is not
  764. * @return $this
  765. * @since 3.7.0
  766. * @see \Cake\Validation\Validator::allowEmptyFor() For detail usage
  767. */
  768. public function allowEmptyArray($field, $when = true, $message = null)
  769. {
  770. return $this->allowEmptyFor($field, self::EMPTY_STRING | self::EMPTY_ARRAY, $when, $message);
  771. }
  772. /**
  773. * Require a field to be a non-empty array
  774. *
  775. * Opposite to allowEmptyArray()
  776. *
  777. * @param string $field The name of the field.
  778. * @param string|null $message The message to show if the field is empty.
  779. * @param bool|string|callable $when Indicates when the field is not allowed
  780. * to be empty. Valid values are false (never), 'create', 'update'. If a
  781. * callable is passed then the field will be required to be not empty when
  782. * the callback returns true.
  783. * @return $this
  784. * @see \Cake\Validation\Validator::allowEmptyArray()
  785. * @since 3.8.0
  786. */
  787. public function notEmptyArray($field, $message = null, $when = false)
  788. {
  789. $when = $this->invertWhenClause($when);
  790. return $this->allowEmptyFor($field, self::EMPTY_STRING | self::EMPTY_ARRAY, $when, $message);
  791. }
  792. /**
  793. * Allows a field to be an empty file.
  794. *
  795. * This method is equivalent to calling allowEmptyFor() with EMPTY_FILE flag.
  796. * File fields will not accept `''`, or `[]` as empty values. Only `null` and a file
  797. * upload with `error` equal to `UPLOAD_ERR_NO_FILE` will be treated as empty.
  798. *
  799. * @param string $field The name of the field.
  800. * @param bool|string|callable $when Indicates when the field is allowed to be empty
  801. * Valid values are true, 'create', 'update'. If a callable is passed then
  802. * the field will allowed to be empty only when the callback returns true.
  803. * @param string|null $message The message to show if the field is not
  804. * @return $this
  805. * @since 3.7.0
  806. * @see \Cake\Validation\Validator::allowEmptyFor() For detail usage
  807. */
  808. public function allowEmptyFile($field, $when = true, $message = null)
  809. {
  810. return $this->allowEmptyFor($field, self::EMPTY_FILE, $when, $message);
  811. }
  812. /**
  813. * Require a field to be a not-empty file.
  814. *
  815. * Opposite to allowEmptyFile()
  816. *
  817. * @param string $field The name of the field.
  818. * @param string|null $message The message to show if the field is empty.
  819. * @param bool|string|callable $when Indicates when the field is not allowed
  820. * to be empty. Valid values are false (never), 'create', 'update'. If a
  821. * callable is passed then the field will be required to be not empty when
  822. * the callback returns true.
  823. * @return $this
  824. * @since 3.8.0
  825. * @see \Cake\Validation\Validator::allowEmptyFile()
  826. */
  827. public function notEmptyFile($field, $message = null, $when = false)
  828. {
  829. $when = $this->invertWhenClause($when);
  830. return $this->allowEmptyFor($field, self::EMPTY_FILE, $when, $message);
  831. }
  832. /**
  833. * Allows a field to be an empty date.
  834. *
  835. * Empty date values are `null`, `''`, `[]` and arrays where all values are `''`
  836. * and the `year` key is present.
  837. *
  838. * @param string $field The name of the field.
  839. * @param bool|string|callable $when Indicates when the field is allowed to be empty
  840. * Valid values are true, false, 'create', 'update'. If a callable is passed then
  841. * the field will allowed to be empty only when the callback returns true.
  842. * @param string|null $message The message to show if the field is not
  843. * @return $this
  844. * @since 3.7.0
  845. * @see \Cake\Validation\Validator::allowEmptyFor() For detail usage
  846. */
  847. public function allowEmptyDate($field, $when = true, $message = null)
  848. {
  849. return $this->allowEmptyFor($field, self::EMPTY_STRING | self::EMPTY_DATE, $when, $message);
  850. }
  851. /**
  852. * Require a non-empty date value
  853. *
  854. * @param string $field The name of the field.
  855. * @param string|null $message The message to show if the field is empty.
  856. * @param bool|string|callable $when Indicates when the field is not allowed
  857. * to be empty. Valid values are false (never), 'create', 'update'. If a
  858. * callable is passed then the field will be required to be not empty when
  859. * the callback returns true.
  860. * @return $this
  861. * @since 3.8.0
  862. * @see \Cake\Validation\Validator::allowEmptyDate() For detail usage
  863. */
  864. public function notEmptyDate($field, $message = null, $when = false)
  865. {
  866. $when = $this->invertWhenClause($when);
  867. return $this->allowEmptyFor($field, self::EMPTY_STRING | self::EMPTY_DATE, $when, $message);
  868. }
  869. /**
  870. * Allows a field to be an empty time.
  871. *
  872. * Empty date values are `null`, `''`, `[]` and arrays where all values are `''`
  873. * and the `hour` key is present.
  874. *
  875. * This method is equivalent to calling allowEmptyFor() with EMPTY_STRING +
  876. * EMPTY_TIME flags.
  877. *
  878. * @param string $field The name of the field.
  879. * @param bool|string|callable $when Indicates when the field is allowed to be empty
  880. * Valid values are true, false, 'create', 'update'. If a callable is passed then
  881. * the field will allowed to be empty only when the callback returns true.
  882. * @param string|null $message The message to show if the field is not
  883. * @return $this
  884. * @since 3.7.0
  885. * @see \Cake\Validation\Validator::allowEmptyFor() For detail usage
  886. */
  887. public function allowEmptyTime($field, $when = true, $message = null)
  888. {
  889. return $this->allowEmptyFor($field, self::EMPTY_STRING | self::EMPTY_TIME, $when, $message);
  890. }
  891. /**
  892. * Require a field to be a non-empty time.
  893. *
  894. * Opposite to allowEmptyTime()
  895. *
  896. * @param string $field The name of the field.
  897. * @param string|null $message The message to show if the field is empty.
  898. * @param bool|string|callable $when Indicates when the field is not allowed
  899. * to be empty. Valid values are false (never), 'create', 'update'. If a
  900. * callable is passed then the field will be required to be not empty when
  901. * the callback returns true.
  902. * @return $this
  903. * @since 3.8.0
  904. * @see \Cake\Validation\Validator::allowEmptyTime()
  905. */
  906. public function notEmptyTime($field, $message = null, $when = false)
  907. {
  908. $when = $this->invertWhenClause($when);
  909. return $this->allowEmptyFor($field, self::EMPTY_STRING | self::EMPTY_TIME, $when, $message);
  910. }
  911. /**
  912. * Allows a field to be an empty date/time.
  913. *
  914. * Empty date values are `null`, `''`, `[]` and arrays where all values are `''`
  915. * and the `year` and `hour` keys are present.
  916. *
  917. * This method is equivalent to calling allowEmptyFor() with EMPTY_STRING +
  918. * EMPTY_DATE + EMPTY_TIME flags.
  919. *
  920. * @param string $field The name of the field.
  921. * @param bool|string|callable $when Indicates when the field is allowed to be empty
  922. * Valid values are true, false, 'create', 'update'. If a callable is passed then
  923. * the field will allowed to be empty only when the callback returns false.
  924. * @param string|null $message The message to show if the field is not
  925. * @return $this
  926. * @since 3.7.0
  927. * @see \Cake\Validation\Validator::allowEmptyFor() For detail usage
  928. */
  929. public function allowEmptyDateTime($field, $when = true, $message = null)
  930. {
  931. return $this->allowEmptyFor($field, self::EMPTY_STRING | self::EMPTY_DATE | self::EMPTY_TIME, $when, $message);
  932. }
  933. /**
  934. * Require a field to be a non empty date/time.
  935. *
  936. * Opposite to allowEmptyDateTime
  937. *
  938. * @param string $field The name of the field.
  939. * @param string|null $message The message to show if the field is empty.
  940. * @param bool|string|callable $when Indicates when the field is not allowed
  941. * to be empty. Valid values are false (never), 'create', 'update'. If a
  942. * callable is passed then the field will be required to be not empty when
  943. * the callback returns true.
  944. * @return $this
  945. * @since 3.8.0
  946. * @see \Cake\Validation\Validator::allowEmptyDateTime()
  947. */
  948. public function notEmptyDateTime($field, $message = null, $when = true)
  949. {
  950. $when = $this->invertWhenClause($when);
  951. return $this->allowEmptyFor($field, self::EMPTY_STRING | self::EMPTY_DATE | self::EMPTY_TIME, $when, $message);
  952. }
  953. /**
  954. * Converts validator to fieldName => $settings array
  955. *
  956. * @param int|string $fieldName name of field
  957. * @param array $defaults default settings
  958. * @param string|array $settings settings from data
  959. * @return array
  960. */
  961. protected function _convertValidatorToArray($fieldName, $defaults = [], $settings = [])
  962. {
  963. if (is_string($settings)) {
  964. $fieldName = $settings;
  965. $settings = [];
  966. }
  967. if (!is_array($settings)) {
  968. throw new InvalidArgumentException(
  969. sprintf('Invalid settings for "%s". Settings must be an array.', $fieldName)
  970. );
  971. }
  972. $settings += $defaults;
  973. return [$fieldName => $settings];
  974. }
  975. /**
  976. * Sets a field to require a non-empty value. You can also pass array.
  977. * Using an array will let you provide the following keys:
  978. *
  979. * - `when` individual when condition for field
  980. * - `message` individual error message for field
  981. *
  982. * You can also set `when` and `message` for all passed fields, the individual setting
  983. * takes precedence over group settings.
  984. *
  985. * This is the opposite of `allowEmpty()` which allows a field to be empty.
  986. * By using $mode equal to 'create' or 'update', you can make fields required
  987. * when records are first created, or when they are updated.
  988. *
  989. * ### Example:
  990. *
  991. * ```
  992. * $message = 'This field cannot be empty';
  993. *
  994. * // Email cannot be empty
  995. * $validator->notEmpty('email');
  996. *
  997. * // Email can be empty on update, but not create
  998. * $validator->notEmpty('email', $message, 'create');
  999. *
  1000. * // Email can be empty on create, but required on update.
  1001. * $validator->notEmpty('email', $message, 'update');
  1002. *
  1003. * // Email and title can be empty on create, but are required on update.
  1004. * $validator->notEmpty(['email', 'title'], $message, 'update');
  1005. *
  1006. * // Email can be empty on create, title must always be not empty
  1007. * $validator->notEmpty(
  1008. * [
  1009. * 'email',
  1010. * 'title' => [
  1011. * 'when' => true,
  1012. * 'message' => 'Title cannot be empty'
  1013. * ]
  1014. * ],
  1015. * $message,
  1016. * 'update'
  1017. * );
  1018. * ```
  1019. *
  1020. * It is possible to conditionally disallow emptiness on a field by passing a callback
  1021. * as the third argument. The callback will receive the validation context array as
  1022. * argument:
  1023. *
  1024. * ```
  1025. * $validator->notEmpty('email', 'Email is required', function ($context) {
  1026. * return $context['newRecord'] && $context['data']['role'] !== 'admin';
  1027. * });
  1028. * ```
  1029. *
  1030. * Because this and `allowEmpty()` modify the same internal state, the last
  1031. * method called will take precedence.
  1032. *
  1033. * @deprecated 3.7.0 Use notEmptyString(), notEmptyArray(), notEmptyFile(),
  1034. * notEmptyDate(), notEmptyTime() or notEmptyDateTime() instead.
  1035. * @param string|array $field the name of the field or list of fields
  1036. * @param string|null $message The message to show if the field is not
  1037. * @param bool|string|callable $when Indicates when the field is not allowed
  1038. * to be empty. Valid values are true (always), 'create', 'update'. If a
  1039. * callable is passed then the field will allowed to be empty only when
  1040. * the callback returns false.
  1041. * @return $this
  1042. */
  1043. public function notEmpty($field, $message = null, $when = false)
  1044. {
  1045. $defaults = [
  1046. 'when' => $when,
  1047. 'message' => $message
  1048. ];
  1049. if (!is_array($field)) {
  1050. $field = $this->_convertValidatorToArray($field, $defaults);
  1051. }
  1052. foreach ($field as $fieldName => $setting) {
  1053. $settings = $this->_convertValidatorToArray($fieldName, $defaults, $setting);
  1054. $fieldName = current(array_keys($settings));
  1055. $whenSetting = $this->invertWhenClause($settings[$fieldName]['when']);
  1056. $this->field($fieldName)->allowEmpty($whenSetting);
  1057. if ($settings[$fieldName]['message']) {
  1058. $this->_allowEmptyMessages[$fieldName] = $settings[$fieldName]['message'];
  1059. }
  1060. }
  1061. return $this;
  1062. }
  1063. /**
  1064. * Invert a when clause for creating notEmpty rules
  1065. *
  1066. * @param bool|string|callable $when Indicates when the field is not allowed
  1067. * to be empty. Valid values are true (always), 'create', 'update'. If a
  1068. * callable is passed then the field will allowed to be empty only when
  1069. * the callback returns false.
  1070. * @return bool|string|callable
  1071. */
  1072. protected function invertWhenClause($when)
  1073. {
  1074. if ($when === 'create' || $when === 'update') {
  1075. return $when === 'create' ? 'update' : 'create';
  1076. } elseif (is_callable($when)) {
  1077. return function ($context) use ($when) {
  1078. return !$when($context);
  1079. };
  1080. }
  1081. return $when;
  1082. }
  1083. /**
  1084. * Add a notBlank rule to a field.
  1085. *
  1086. * @param string $field The field you want to apply the rule to.
  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::notBlank()
  1091. * @return $this
  1092. */
  1093. public function notBlank($field, $message = null, $when = null)
  1094. {
  1095. $extra = array_filter(['on' => $when, 'message' => $message]);
  1096. return $this->add($field, 'notBlank', $extra + [
  1097. 'rule' => 'notBlank',
  1098. ]);
  1099. }
  1100. /**
  1101. * Add an alphanumeric rule to a field.
  1102. *
  1103. * @param string $field The field you want to apply the rule to.
  1104. * @param string|null $message The error message when the rule fails.
  1105. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1106. * true when the validation rule should be applied.
  1107. * @see \Cake\Validation\Validation::alphaNumeric()
  1108. * @return $this
  1109. */
  1110. public function alphaNumeric($field, $message = null, $when = null)
  1111. {
  1112. $extra = array_filter(['on' => $when, 'message' => $message]);
  1113. return $this->add($field, 'alphaNumeric', $extra + [
  1114. 'rule' => 'alphaNumeric',
  1115. ]);
  1116. }
  1117. /**
  1118. * Add an rule that ensures a string length is within a range.
  1119. *
  1120. * @param string $field The field you want to apply the rule to.
  1121. * @param array $range The inclusive minimum and maximum length you want permitted.
  1122. * @param string|null $message The error message when the rule fails.
  1123. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1124. * true when the validation rule should be applied.
  1125. * @see \Cake\Validation\Validation::alphaNumeric()
  1126. * @return $this
  1127. */
  1128. public function lengthBetween($field, array $range, $message = null, $when = null)
  1129. {
  1130. if (count($range) !== 2) {
  1131. throw new InvalidArgumentException('The $range argument requires 2 numbers');
  1132. }
  1133. $extra = array_filter(['on' => $when, 'message' => $message]);
  1134. return $this->add($field, 'lengthBetween', $extra + [
  1135. 'rule' => ['lengthBetween', array_shift($range), array_shift($range)],
  1136. ]);
  1137. }
  1138. /**
  1139. * Add a credit card rule to a field.
  1140. *
  1141. * @param string $field The field you want to apply the rule to.
  1142. * @param string $type The type of cards you want to allow. Defaults to 'all'.
  1143. * You can also supply an array of accepted card types. e.g `['mastercard', 'visa', 'amex']`
  1144. * @param string|null $message The error message when the rule fails.
  1145. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1146. * true when the validation rule should be applied.
  1147. * @see \Cake\Validation\Validation::creditCard()
  1148. * @return $this
  1149. */
  1150. public function creditCard($field, $type = 'all', $message = null, $when = null)
  1151. {
  1152. $extra = array_filter(['on' => $when, 'message' => $message]);
  1153. return $this->add($field, 'creditCard', $extra + [
  1154. 'rule' => ['creditCard', $type, true],
  1155. ]);
  1156. }
  1157. /**
  1158. * Add a greater than comparison rule to a field.
  1159. *
  1160. * @param string $field The field you want to apply the rule to.
  1161. * @param int|float $value The value user data must be greater than.
  1162. * @param string|null $message The error message when the rule fails.
  1163. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1164. * true when the validation rule should be applied.
  1165. * @see \Cake\Validation\Validation::comparison()
  1166. * @return $this
  1167. */
  1168. public function greaterThan($field, $value, $message = null, $when = null)
  1169. {
  1170. $extra = array_filter(['on' => $when, 'message' => $message]);
  1171. return $this->add($field, 'greaterThan', $extra + [
  1172. 'rule' => ['comparison', Validation::COMPARE_GREATER, $value]
  1173. ]);
  1174. }
  1175. /**
  1176. * Add a greater than or equal to comparison rule to a field.
  1177. *
  1178. * @param string $field The field you want to apply the rule to.
  1179. * @param int|float $value The value user data must be greater than or equal to.
  1180. * @param string|null $message The error message when the rule fails.
  1181. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1182. * true when the validation rule should be applied.
  1183. * @see \Cake\Validation\Validation::comparison()
  1184. * @return $this
  1185. */
  1186. public function greaterThanOrEqual($field, $value, $message = null, $when = null)
  1187. {
  1188. $extra = array_filter(['on' => $when, 'message' => $message]);
  1189. return $this->add($field, 'greaterThanOrEqual', $extra + [
  1190. 'rule' => ['comparison', Validation::COMPARE_GREATER_OR_EQUAL, $value]
  1191. ]);
  1192. }
  1193. /**
  1194. * Add a less than comparison rule to a field.
  1195. *
  1196. * @param string $field The field you want to apply the rule to.
  1197. * @param int|float $value The value user data must be less than.
  1198. * @param string|null $message The error message when the rule fails.
  1199. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1200. * true when the validation rule should be applied.
  1201. * @see \Cake\Validation\Validation::comparison()
  1202. * @return $this
  1203. */
  1204. public function lessThan($field, $value, $message = null, $when = null)
  1205. {
  1206. $extra = array_filter(['on' => $when, 'message' => $message]);
  1207. return $this->add($field, 'lessThan', $extra + [
  1208. 'rule' => ['comparison', Validation::COMPARE_LESS, $value]
  1209. ]);
  1210. }
  1211. /**
  1212. * Add a less than or equal comparison rule to a field.
  1213. *
  1214. * @param string $field The field you want to apply the rule to.
  1215. * @param int|float $value The value user data must be less than or equal to.
  1216. * @param string|null $message The error message when the rule fails.
  1217. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1218. * true when the validation rule should be applied.
  1219. * @see \Cake\Validation\Validation::comparison()
  1220. * @return $this
  1221. */
  1222. public function lessThanOrEqual($field, $value, $message = null, $when = null)
  1223. {
  1224. $extra = array_filter(['on' => $when, 'message' => $message]);
  1225. return $this->add($field, 'lessThanOrEqual', $extra + [
  1226. 'rule' => ['comparison', Validation::COMPARE_LESS_OR_EQUAL, $value]
  1227. ]);
  1228. }
  1229. /**
  1230. * Add a equal to comparison rule to a field.
  1231. *
  1232. * @param string $field The field you want to apply the rule to.
  1233. * @param int|float $value The value user data must be equal to.
  1234. * @param string|null $message The error message when the rule fails.
  1235. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1236. * true when the validation rule should be applied.
  1237. * @see \Cake\Validation\Validation::comparison()
  1238. * @return $this
  1239. */
  1240. public function equals($field, $value, $message = null, $when = null)
  1241. {
  1242. $extra = array_filter(['on' => $when, 'message' => $message]);
  1243. return $this->add($field, 'equals', $extra + [
  1244. 'rule' => ['comparison', Validation::COMPARE_EQUAL, $value]
  1245. ]);
  1246. }
  1247. /**
  1248. * Add a not equal to comparison rule to a field.
  1249. *
  1250. * @param string $field The field you want to apply the rule to.
  1251. * @param int|float $value The value user data must be not be equal to.
  1252. * @param string|null $message The error message when the rule fails.
  1253. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1254. * true when the validation rule should be applied.
  1255. * @see \Cake\Validation\Validation::comparison()
  1256. * @return $this
  1257. */
  1258. public function notEquals($field, $value, $message = null, $when = null)
  1259. {
  1260. $extra = array_filter(['on' => $when, 'message' => $message]);
  1261. return $this->add($field, 'notEquals', $extra + [
  1262. 'rule' => ['comparison', Validation::COMPARE_NOT_EQUAL, $value]
  1263. ]);
  1264. }
  1265. /**
  1266. * Add a rule to compare two fields to each other.
  1267. *
  1268. * If both fields have the exact same value the rule will pass.
  1269. *
  1270. * @param string $field The field you want to apply the rule to.
  1271. * @param string $secondField The field you want to compare against.
  1272. * @param string|null $message The error message when the rule fails.
  1273. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1274. * true when the validation rule should be applied.
  1275. * @see \Cake\Validation\Validation::compareFields()
  1276. * @return $this
  1277. */
  1278. public function sameAs($field, $secondField, $message = null, $when = null)
  1279. {
  1280. $extra = array_filter(['on' => $when, 'message' => $message]);
  1281. return $this->add($field, 'sameAs', $extra + [
  1282. 'rule' => ['compareFields', $secondField, Validation::COMPARE_SAME]
  1283. ]);
  1284. }
  1285. /**
  1286. * Add a rule to compare that two fields have different values.
  1287. *
  1288. * @param string $field The field you want to apply the rule to.
  1289. * @param string $secondField The field you want to compare against.
  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::compareFields()
  1294. * @return $this
  1295. * @since 3.6.0
  1296. */
  1297. public function notSameAs($field, $secondField, $message = null, $when = null)
  1298. {
  1299. $extra = array_filter(['on' => $when, 'message' => $message]);
  1300. return $this->add($field, 'notSameAs', $extra + [
  1301. 'rule' => ['compareFields', $secondField, Validation::COMPARE_NOT_SAME]
  1302. ]);
  1303. }
  1304. /**
  1305. * Add a rule to compare one field is equal to another.
  1306. *
  1307. * @param string $field The field you want to apply the rule to.
  1308. * @param string $secondField The field you want to compare against.
  1309. * @param string|null $message The error message when the rule fails.
  1310. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1311. * true when the validation rule should be applied.
  1312. * @see \Cake\Validation\Validation::compareFields()
  1313. * @return $this
  1314. * @since 3.6.0
  1315. */
  1316. public function equalToField($field, $secondField, $message = null, $when = null)
  1317. {
  1318. $extra = array_filter(['on' => $when, 'message' => $message]);
  1319. return $this->add($field, 'equalToField', $extra + [
  1320. 'rule' => ['compareFields', $secondField, Validation::COMPARE_EQUAL]
  1321. ]);
  1322. }
  1323. /**
  1324. * Add a rule to compare one field is not equal to another.
  1325. *
  1326. * @param string $field The field you want to apply the rule to.
  1327. * @param string $secondField The field you want to compare against.
  1328. * @param string|null $message The error message when the rule fails.
  1329. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1330. * true when the validation rule should be applied.
  1331. * @see \Cake\Validation\Validation::compareFields()
  1332. * @return $this
  1333. * @since 3.6.0
  1334. */
  1335. public function notEqualToField($field, $secondField, $message = null, $when = null)
  1336. {
  1337. $extra = array_filter(['on' => $when, 'message' => $message]);
  1338. return $this->add($field, 'notEqualToField', $extra + [
  1339. 'rule' => ['compareFields', $secondField, Validation::COMPARE_NOT_EQUAL]
  1340. ]);
  1341. }
  1342. /**
  1343. * Add a rule to compare one field is greater than another.
  1344. *
  1345. * @param string $field The field you want to apply the rule to.
  1346. * @param string $secondField The field you want to compare against.
  1347. * @param string|null $message The error message when the rule fails.
  1348. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1349. * true when the validation rule should be applied.
  1350. * @see \Cake\Validation\Validation::compareFields()
  1351. * @return $this
  1352. * @since 3.6.0
  1353. */
  1354. public function greaterThanField($field, $secondField, $message = null, $when = null)
  1355. {
  1356. $extra = array_filter(['on' => $when, 'message' => $message]);
  1357. return $this->add($field, 'greaterThanField', $extra + [
  1358. 'rule' => ['compareFields', $secondField, Validation::COMPARE_GREATER]
  1359. ]);
  1360. }
  1361. /**
  1362. * Add a rule to compare one field is greater than or equal to another.
  1363. *
  1364. * @param string $field The field you want to apply the rule to.
  1365. * @param string $secondField The field you want to compare against.
  1366. * @param string|null $message The error message when the rule fails.
  1367. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1368. * true when the validation rule should be applied.
  1369. * @see \Cake\Validation\Validation::compareFields()
  1370. * @return $this
  1371. * @since 3.6.0
  1372. */
  1373. public function greaterThanOrEqualToField($field, $secondField, $message = null, $when = null)
  1374. {
  1375. $extra = array_filter(['on' => $when, 'message' => $message]);
  1376. return $this->add($field, 'greaterThanOrEqualToField', $extra + [
  1377. 'rule' => ['compareFields', $secondField, Validation::COMPARE_GREATER_OR_EQUAL]
  1378. ]);
  1379. }
  1380. /**
  1381. * Add a rule to compare one field is less than another.
  1382. *
  1383. * @param string $field The field you want to apply the rule to.
  1384. * @param string $secondField The field you want to compare against.
  1385. * @param string|null $message The error message when the rule fails.
  1386. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1387. * true when the validation rule should be applied.
  1388. * @see \Cake\Validation\Validation::compareFields()
  1389. * @return $this
  1390. * @since 3.6.0
  1391. */
  1392. public function lessThanField($field, $secondField, $message = null, $when = null)
  1393. {
  1394. $extra = array_filter(['on' => $when, 'message' => $message]);
  1395. return $this->add($field, 'lessThanField', $extra + [
  1396. 'rule' => ['compareFields', $secondField, Validation::COMPARE_LESS]
  1397. ]);
  1398. }
  1399. /**
  1400. * Add a rule to compare one field is less than or equal to another.
  1401. *
  1402. * @param string $field The field you want to apply the rule to.
  1403. * @param string $secondField The field you want to compare against.
  1404. * @param string|null $message The error message when the rule fails.
  1405. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1406. * true when the validation rule should be applied.
  1407. * @see \Cake\Validation\Validation::compareFields()
  1408. * @return $this
  1409. * @since 3.6.0
  1410. */
  1411. public function lessThanOrEqualToField($field, $secondField, $message = null, $when = null)
  1412. {
  1413. $extra = array_filter(['on' => $when, 'message' => $message]);
  1414. return $this->add($field, 'lessThanOrEqualToField', $extra + [
  1415. 'rule' => ['compareFields', $secondField, Validation::COMPARE_LESS_OR_EQUAL]
  1416. ]);
  1417. }
  1418. /**
  1419. * Add a rule to check if a field contains non alpha numeric characters.
  1420. *
  1421. * @param string $field The field you want to apply the rule to.
  1422. * @param int $limit The minimum number of non-alphanumeric fields required.
  1423. * @param string|null $message The error message when the rule fails.
  1424. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1425. * true when the validation rule should be applied.
  1426. * @see \Cake\Validation\Validation::containsNonAlphaNumeric()
  1427. * @return $this
  1428. */
  1429. public function containsNonAlphaNumeric($field, $limit = 1, $message = null, $when = null)
  1430. {
  1431. $extra = array_filter(['on' => $when, 'message' => $message]);
  1432. return $this->add($field, 'containsNonAlphaNumeric', $extra + [
  1433. 'rule' => ['containsNonAlphaNumeric', $limit]
  1434. ]);
  1435. }
  1436. /**
  1437. * Add a date format validation rule to a field.
  1438. *
  1439. * @param string $field The field you want to apply the rule to.
  1440. * @param array $formats A list of accepted date formats.
  1441. * @param string|null $message The error message when the rule fails.
  1442. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1443. * true when the validation rule should be applied.
  1444. * @see \Cake\Validation\Validation::date()
  1445. * @return $this
  1446. */
  1447. public function date($field, $formats = ['ymd'], $message = null, $when = null)
  1448. {
  1449. $extra = array_filter(['on' => $when, 'message' => $message]);
  1450. return $this->add($field, 'date', $extra + [
  1451. 'rule' => ['date', $formats]
  1452. ]);
  1453. }
  1454. /**
  1455. * Add a date time format validation rule to a field.
  1456. *
  1457. * @param string $field The field you want to apply the rule to.
  1458. * @param array $formats A list of accepted date formats.
  1459. * @param string|null $message The error message when the rule fails.
  1460. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1461. * true when the validation rule should be applied.
  1462. * @see \Cake\Validation\Validation::datetime()
  1463. * @return $this
  1464. */
  1465. public function dateTime($field, $formats = ['ymd'], $message = null, $when = null)
  1466. {
  1467. $extra = array_filter(['on' => $when, 'message' => $message]);
  1468. return $this->add($field, 'dateTime', $extra + [
  1469. 'rule' => ['datetime', $formats]
  1470. ]);
  1471. }
  1472. /**
  1473. * Add a time format validation rule to a field.
  1474. *
  1475. * @param string $field The field you want to apply the rule to.
  1476. * @param string|null $message The error message when the rule fails.
  1477. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1478. * true when the validation rule should be applied.
  1479. * @see \Cake\Validation\Validation::time()
  1480. * @return $this
  1481. */
  1482. public function time($field, $message = null, $when = null)
  1483. {
  1484. $extra = array_filter(['on' => $when, 'message' => $message]);
  1485. return $this->add($field, 'time', $extra + [
  1486. 'rule' => 'time'
  1487. ]);
  1488. }
  1489. /**
  1490. * Add a localized time, date or datetime format validation rule to a field.
  1491. *
  1492. * @param string $field The field you want to apply the rule to.
  1493. * @param string $type Parser type, one out of 'date', 'time', and 'datetime'
  1494. * @param string|null $message The error message when the rule fails.
  1495. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1496. * true when the validation rule should be applied.
  1497. * @see \Cake\Validation\Validation::localizedTime()
  1498. * @return $this
  1499. */
  1500. public function localizedTime($field, $type = 'datetime', $message = null, $when = null)
  1501. {
  1502. $extra = array_filter(['on' => $when, 'message' => $message]);
  1503. return $this->add($field, 'localizedTime', $extra + [
  1504. 'rule' => ['localizedTime', $type]
  1505. ]);
  1506. }
  1507. /**
  1508. * Add a boolean validation rule to a field.
  1509. *
  1510. * @param string $field The field you want to apply the rule to.
  1511. * @param string|null $message The error message when the rule fails.
  1512. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1513. * true when the validation rule should be applied.
  1514. * @see \Cake\Validation\Validation::boolean()
  1515. * @return $this
  1516. */
  1517. public function boolean($field, $message = null, $when = null)
  1518. {
  1519. $extra = array_filter(['on' => $when, 'message' => $message]);
  1520. return $this->add($field, 'boolean', $extra + [
  1521. 'rule' => 'boolean'
  1522. ]);
  1523. }
  1524. /**
  1525. * Add a decimal validation rule to a field.
  1526. *
  1527. * @param string $field The field you want to apply the rule to.
  1528. * @param int|null $places The number of decimal places to require.
  1529. * @param string|null $message The error message when the rule fails.
  1530. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1531. * true when the validation rule should be applied.
  1532. * @see \Cake\Validation\Validation::decimal()
  1533. * @return $this
  1534. */
  1535. public function decimal($field, $places = null, $message = null, $when = null)
  1536. {
  1537. $extra = array_filter(['on' => $when, 'message' => $message]);
  1538. return $this->add($field, 'decimal', $extra + [
  1539. 'rule' => ['decimal', $places]
  1540. ]);
  1541. }
  1542. /**
  1543. * Add an email validation rule to a field.
  1544. *
  1545. * @param string $field The field you want to apply the rule to.
  1546. * @param bool $checkMX Whether or not to check the MX records.
  1547. * @param string|null $message The error message when the rule fails.
  1548. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1549. * true when the validation rule should be applied.
  1550. * @see \Cake\Validation\Validation::email()
  1551. * @return $this
  1552. */
  1553. public function email($field, $checkMX = false, $message = null, $when = null)
  1554. {
  1555. $extra = array_filter(['on' => $when, 'message' => $message]);
  1556. return $this->add($field, 'email', $extra + [
  1557. 'rule' => ['email', $checkMX]
  1558. ]);
  1559. }
  1560. /**
  1561. * Add an IP validation rule to a field.
  1562. *
  1563. * This rule will accept both IPv4 and IPv6 addresses.
  1564. *
  1565. * @param string $field The field you want to apply the rule to.
  1566. * @param string|null $message The error message when the rule fails.
  1567. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1568. * true when the validation rule should be applied.
  1569. * @see \Cake\Validation\Validation::ip()
  1570. * @return $this
  1571. */
  1572. public function ip($field, $message = null, $when = null)
  1573. {
  1574. $extra = array_filter(['on' => $when, 'message' => $message]);
  1575. return $this->add($field, 'ip', $extra + [
  1576. 'rule' => 'ip'
  1577. ]);
  1578. }
  1579. /**
  1580. * Add an IPv4 validation rule to a field.
  1581. *
  1582. * @param string $field The field you want to apply the rule to.
  1583. * @param string|null $message The error message when the rule fails.
  1584. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1585. * true when the validation rule should be applied.
  1586. * @see \Cake\Validation\Validation::ip()
  1587. * @return $this
  1588. */
  1589. public function ipv4($field, $message = null, $when = null)
  1590. {
  1591. $extra = array_filter(['on' => $when, 'message' => $message]);
  1592. return $this->add($field, 'ipv4', $extra + [
  1593. 'rule' => ['ip', 'ipv4']
  1594. ]);
  1595. }
  1596. /**
  1597. * Add an IPv6 validation rule to a field.
  1598. *
  1599. * @param string $field The field you want to apply the rule to.
  1600. * @param string|null $message The error message when the rule fails.
  1601. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1602. * true when the validation rule should be applied.
  1603. * @see \Cake\Validation\Validation::ip()
  1604. * @return $this
  1605. */
  1606. public function ipv6($field, $message = null, $when = null)
  1607. {
  1608. $extra = array_filter(['on' => $when, 'message' => $message]);
  1609. return $this->add($field, 'ipv6', $extra + [
  1610. 'rule' => ['ip', 'ipv6']
  1611. ]);
  1612. }
  1613. /**
  1614. * Add a string length validation rule to a field.
  1615. *
  1616. * @param string $field The field you want to apply the rule to.
  1617. * @param int $min The minimum length required.
  1618. * @param string|null $message The error message when the rule fails.
  1619. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1620. * true when the validation rule should be applied.
  1621. * @see \Cake\Validation\Validation::minLength()
  1622. * @return $this
  1623. */
  1624. public function minLength($field, $min, $message = null, $when = null)
  1625. {
  1626. $extra = array_filter(['on' => $when, 'message' => $message]);
  1627. return $this->add($field, 'minLength', $extra + [
  1628. 'rule' => ['minLength', $min]
  1629. ]);
  1630. }
  1631. /**
  1632. * Add a string length validation rule to a field.
  1633. *
  1634. * @param string $field The field you want to apply the rule to.
  1635. * @param int $min The minimum length required.
  1636. * @param string|null $message The error message when the rule fails.
  1637. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1638. * true when the validation rule should be applied.
  1639. * @see \Cake\Validation\Validation::minLengthBytes()
  1640. * @return $this
  1641. */
  1642. public function minLengthBytes($field, $min, $message = null, $when = null)
  1643. {
  1644. $extra = array_filter(['on' => $when, 'message' => $message]);
  1645. return $this->add($field, 'minLengthBytes', $extra + [
  1646. 'rule' => ['minLengthBytes', $min]
  1647. ]);
  1648. }
  1649. /**
  1650. * Add a string length validation rule to a field.
  1651. *
  1652. * @param string $field The field you want to apply the rule to.
  1653. * @param int $max The maximum length allowed.
  1654. * @param string|null $message The error message when the rule fails.
  1655. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1656. * true when the validation rule should be applied.
  1657. * @see \Cake\Validation\Validation::maxLength()
  1658. * @return $this
  1659. */
  1660. public function maxLength($field, $max, $message = null, $when = null)
  1661. {
  1662. $extra = array_filter(['on' => $when, 'message' => $message]);
  1663. return $this->add($field, 'maxLength', $extra + [
  1664. 'rule' => ['maxLength', $max]
  1665. ]);
  1666. }
  1667. /**
  1668. * Add a string length validation rule to a field.
  1669. *
  1670. * @param string $field The field you want to apply the rule to.
  1671. * @param int $max The maximum length allowed.
  1672. * @param string|null $message The error message when the rule fails.
  1673. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1674. * true when the validation rule should be applied.
  1675. * @see \Cake\Validation\Validation::maxLengthBytes()
  1676. * @return $this
  1677. */
  1678. public function maxLengthBytes($field, $max, $message = null, $when = null)
  1679. {
  1680. $extra = array_filter(['on' => $when, 'message' => $message]);
  1681. return $this->add($field, 'maxLengthBytes', $extra + [
  1682. 'rule' => ['maxLengthBytes', $max]
  1683. ]);
  1684. }
  1685. /**
  1686. * Add a numeric value validation rule to a field.
  1687. *
  1688. * @param string $field The field you want to apply the rule to.
  1689. * @param string|null $message The error message when the rule fails.
  1690. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1691. * true when the validation rule should be applied.
  1692. * @see \Cake\Validation\Validation::numeric()
  1693. * @return $this
  1694. */
  1695. public function numeric($field, $message = null, $when = null)
  1696. {
  1697. $extra = array_filter(['on' => $when, 'message' => $message]);
  1698. return $this->add($field, 'numeric', $extra + [
  1699. 'rule' => 'numeric'
  1700. ]);
  1701. }
  1702. /**
  1703. * Add a natural number validation rule to a field.
  1704. *
  1705. * @param string $field The field you want to apply the rule to.
  1706. * @param string|null $message The error message when the rule fails.
  1707. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1708. * true when the validation rule should be applied.
  1709. * @see \Cake\Validation\Validation::naturalNumber()
  1710. * @return $this
  1711. */
  1712. public function naturalNumber($field, $message = null, $when = null)
  1713. {
  1714. $extra = array_filter(['on' => $when, 'message' => $message]);
  1715. return $this->add($field, 'naturalNumber', $extra + [
  1716. 'rule' => ['naturalNumber', false]
  1717. ]);
  1718. }
  1719. /**
  1720. * Add a validation rule to ensure a field is a non negative integer.
  1721. *
  1722. * @param string $field The field you want to apply the rule to.
  1723. * @param string|null $message The error message when the rule fails.
  1724. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1725. * true when the validation rule should be applied.
  1726. * @see \Cake\Validation\Validation::naturalNumber()
  1727. * @return $this
  1728. */
  1729. public function nonNegativeInteger($field, $message = null, $when = null)
  1730. {
  1731. $extra = array_filter(['on' => $when, 'message' => $message]);
  1732. return $this->add($field, 'nonNegativeInteger', $extra + [
  1733. 'rule' => ['naturalNumber', true]
  1734. ]);
  1735. }
  1736. /**
  1737. * Add a validation rule to ensure a field is within a numeric range
  1738. *
  1739. * @param string $field The field you want to apply the rule to.
  1740. * @param array $range The inclusive upper and lower bounds of the valid range.
  1741. * @param string|null $message The error message when the rule fails.
  1742. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1743. * true when the validation rule should be applied.
  1744. * @see \Cake\Validation\Validation::range()
  1745. * @return $this
  1746. */
  1747. public function range($field, array $range, $message = null, $when = null)
  1748. {
  1749. if (count($range) !== 2) {
  1750. throw new InvalidArgumentException('The $range argument requires 2 numbers');
  1751. }
  1752. $extra = array_filter(['on' => $when, 'message' => $message]);
  1753. return $this->add($field, 'range', $extra + [
  1754. 'rule' => ['range', array_shift($range), array_shift($range)]
  1755. ]);
  1756. }
  1757. /**
  1758. * Add a validation rule to ensure a field is a URL.
  1759. *
  1760. * This validator does not require a protocol.
  1761. *
  1762. * @param string $field The field you want to apply the rule to.
  1763. * @param string|null $message The error message when the rule fails.
  1764. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1765. * true when the validation rule should be applied.
  1766. * @see \Cake\Validation\Validation::url()
  1767. * @return $this
  1768. */
  1769. public function url($field, $message = null, $when = null)
  1770. {
  1771. $extra = array_filter(['on' => $when, 'message' => $message]);
  1772. return $this->add($field, 'url', $extra + [
  1773. 'rule' => ['url', false]
  1774. ]);
  1775. }
  1776. /**
  1777. * Add a validation rule to ensure a field is a URL.
  1778. *
  1779. * This validator requires the URL to have a protocol.
  1780. *
  1781. * @param string $field The field you want to apply the rule to.
  1782. * @param string|null $message The error message when the rule fails.
  1783. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1784. * true when the validation rule should be applied.
  1785. * @see \Cake\Validation\Validation::url()
  1786. * @return $this
  1787. */
  1788. public function urlWithProtocol($field, $message = null, $when = null)
  1789. {
  1790. $extra = array_filter(['on' => $when, 'message' => $message]);
  1791. return $this->add($field, 'urlWithProtocol', $extra + [
  1792. 'rule' => ['url', true]
  1793. ]);
  1794. }
  1795. /**
  1796. * Add a validation rule to ensure the field value is within a whitelist.
  1797. *
  1798. * @param string $field The field you want to apply the rule to.
  1799. * @param array $list The list of valid options.
  1800. * @param string|null $message The error message when the rule fails.
  1801. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1802. * true when the validation rule should be applied.
  1803. * @see \Cake\Validation\Validation::inList()
  1804. * @return $this
  1805. */
  1806. public function inList($field, array $list, $message = null, $when = null)
  1807. {
  1808. $extra = array_filter(['on' => $when, 'message' => $message]);
  1809. return $this->add($field, 'inList', $extra + [
  1810. 'rule' => ['inList', $list]
  1811. ]);
  1812. }
  1813. /**
  1814. * Add a validation rule to ensure the field is a UUID
  1815. *
  1816. * @param string $field The field you want to apply the rule to.
  1817. * @param string|null $message The error message when the rule fails.
  1818. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1819. * true when the validation rule should be applied.
  1820. * @see \Cake\Validation\Validation::uuid()
  1821. * @return $this
  1822. */
  1823. public function uuid($field, $message = null, $when = null)
  1824. {
  1825. $extra = array_filter(['on' => $when, 'message' => $message]);
  1826. return $this->add($field, 'uuid', $extra + [
  1827. 'rule' => 'uuid'
  1828. ]);
  1829. }
  1830. /**
  1831. * Add a validation rule to ensure the field is an uploaded file
  1832. *
  1833. * For options see Cake\Validation\Validation::uploadedFile()
  1834. *
  1835. * @param string $field The field you want to apply the rule to.
  1836. * @param array $options An array of options.
  1837. * @param string|null $message The error message when the rule fails.
  1838. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1839. * true when the validation rule should be applied.
  1840. * @see \Cake\Validation\Validation::uploadedFile()
  1841. * @return $this
  1842. */
  1843. public function uploadedFile($field, array $options, $message = null, $when = null)
  1844. {
  1845. $extra = array_filter(['on' => $when, 'message' => $message]);
  1846. return $this->add($field, 'uploadedFile', $extra + [
  1847. 'rule' => ['uploadedFile', $options]
  1848. ]);
  1849. }
  1850. /**
  1851. * Add a validation rule to ensure the field is a lat/long tuple.
  1852. *
  1853. * e.g. `<lat>, <lng>`
  1854. *
  1855. * @param string $field The field you want to apply the rule to.
  1856. * @param string|null $message The error message when the rule fails.
  1857. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1858. * true when the validation rule should be applied.
  1859. * @see \Cake\Validation\Validation::uuid()
  1860. * @return $this
  1861. */
  1862. public function latLong($field, $message = null, $when = null)
  1863. {
  1864. $extra = array_filter(['on' => $when, 'message' => $message]);
  1865. return $this->add($field, 'latLong', $extra + [
  1866. 'rule' => 'geoCoordinate'
  1867. ]);
  1868. }
  1869. /**
  1870. * Add a validation rule to ensure the field is a latitude.
  1871. *
  1872. * @param string $field The field you want to apply the rule to.
  1873. * @param string|null $message The error message when the rule fails.
  1874. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1875. * true when the validation rule should be applied.
  1876. * @see \Cake\Validation\Validation::latitude()
  1877. * @return $this
  1878. */
  1879. public function latitude($field, $message = null, $when = null)
  1880. {
  1881. $extra = array_filter(['on' => $when, 'message' => $message]);
  1882. return $this->add($field, 'latitude', $extra + [
  1883. 'rule' => 'latitude'
  1884. ]);
  1885. }
  1886. /**
  1887. * Add a validation rule to ensure the field is a longitude.
  1888. *
  1889. * @param string $field The field you want to apply the rule to.
  1890. * @param string|null $message The error message when the rule fails.
  1891. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1892. * true when the validation rule should be applied.
  1893. * @see \Cake\Validation\Validation::longitude()
  1894. * @return $this
  1895. */
  1896. public function longitude($field, $message = null, $when = null)
  1897. {
  1898. $extra = array_filter(['on' => $when, 'message' => $message]);
  1899. return $this->add($field, 'longitude', $extra + [
  1900. 'rule' => 'longitude'
  1901. ]);
  1902. }
  1903. /**
  1904. * Add a validation rule to ensure a field contains only ascii bytes
  1905. *
  1906. * @param string $field The field you want to apply the rule to.
  1907. * @param string|null $message The error message when the rule fails.
  1908. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1909. * true when the validation rule should be applied.
  1910. * @see \Cake\Validation\Validation::ascii()
  1911. * @return $this
  1912. */
  1913. public function ascii($field, $message = null, $when = null)
  1914. {
  1915. $extra = array_filter(['on' => $when, 'message' => $message]);
  1916. return $this->add($field, 'ascii', $extra + [
  1917. 'rule' => 'ascii'
  1918. ]);
  1919. }
  1920. /**
  1921. * Add a validation rule to ensure a field contains only BMP utf8 bytes
  1922. *
  1923. * @param string $field The field you want to apply the rule to.
  1924. * @param string|null $message The error message when the rule fails.
  1925. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1926. * true when the validation rule should be applied.
  1927. * @see \Cake\Validation\Validation::utf8()
  1928. * @return $this
  1929. */
  1930. public function utf8($field, $message = null, $when = null)
  1931. {
  1932. $extra = array_filter(['on' => $when, 'message' => $message]);
  1933. return $this->add($field, 'utf8', $extra + [
  1934. 'rule' => ['utf8', ['extended' => false]]
  1935. ]);
  1936. }
  1937. /**
  1938. * Add a validation rule to ensure a field contains only utf8 bytes.
  1939. *
  1940. * This rule will accept 3 and 4 byte UTF8 sequences, which are necessary for emoji.
  1941. *
  1942. * @param string $field The field you want to apply the rule to.
  1943. * @param string|null $message The error message when the rule fails.
  1944. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1945. * true when the validation rule should be applied.
  1946. * @see \Cake\Validation\Validation::utf8()
  1947. * @return $this
  1948. */
  1949. public function utf8Extended($field, $message = null, $when = null)
  1950. {
  1951. $extra = array_filter(['on' => $when, 'message' => $message]);
  1952. return $this->add($field, 'utf8Extended', $extra + [
  1953. 'rule' => ['utf8', ['extended' => true]]
  1954. ]);
  1955. }
  1956. /**
  1957. * Add a validation rule to ensure a field is an integer value.
  1958. *
  1959. * @param string $field The field you want to apply the rule to.
  1960. * @param string|null $message The error message when the rule fails.
  1961. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1962. * true when the validation rule should be applied.
  1963. * @see \Cake\Validation\Validation::isInteger()
  1964. * @return $this
  1965. */
  1966. public function integer($field, $message = null, $when = null)
  1967. {
  1968. $extra = array_filter(['on' => $when, 'message' => $message]);
  1969. return $this->add($field, 'integer', $extra + [
  1970. 'rule' => 'isInteger'
  1971. ]);
  1972. }
  1973. /**
  1974. * Add a validation rule to ensure that a field contains an array.
  1975. *
  1976. * @param string $field The field you want to apply the rule to.
  1977. * @param string|null $message The error message when the rule fails.
  1978. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1979. * true when the validation rule should be applied.
  1980. * @see \Cake\Validation\Validation::isArray()
  1981. * @return $this
  1982. */
  1983. public function isArray($field, $message = null, $when = null)
  1984. {
  1985. $extra = array_filter(['on' => $when, 'message' => $message]);
  1986. return $this->add($field, 'isArray', $extra + [
  1987. 'rule' => 'isArray'
  1988. ]);
  1989. }
  1990. /**
  1991. * Add a validation rule to ensure that a field contains a scalar.
  1992. *
  1993. * @param string $field The field you want to apply the rule to.
  1994. * @param string|null $message The error message when the rule fails.
  1995. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  1996. * true when the validation rule should be applied.
  1997. * @see \Cake\Validation\Validation::isScalar()
  1998. * @return $this
  1999. */
  2000. public function scalar($field, $message = null, $when = null)
  2001. {
  2002. $extra = array_filter(['on' => $when, 'message' => $message]);
  2003. return $this->add($field, 'scalar', $extra + [
  2004. 'rule' => 'isScalar'
  2005. ]);
  2006. }
  2007. /**
  2008. * Add a validation rule to ensure a field is a 6 digits hex color value.
  2009. *
  2010. * @param string $field The field you want to apply the rule to.
  2011. * @param string|null $message The error message when the rule fails.
  2012. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  2013. * true when the validation rule should be applied.
  2014. * @see \Cake\Validation\Validation::hexColor()
  2015. * @return $this
  2016. */
  2017. public function hexColor($field, $message = null, $when = null)
  2018. {
  2019. $extra = array_filter(['on' => $when, 'message' => $message]);
  2020. return $this->add($field, 'hexColor', $extra + [
  2021. 'rule' => 'hexColor',
  2022. ]);
  2023. }
  2024. /**
  2025. * Add a validation rule for a multiple select. Comparison is case sensitive by default.
  2026. *
  2027. * @param string $field The field you want to apply the rule to.
  2028. * @param array $options The options for the validator. Includes the options defined in
  2029. * \Cake\Validation\Validation::multiple() and the `caseInsensitive` parameter.
  2030. * @param string|null $message The error message when the rule fails.
  2031. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  2032. * true when the validation rule should be applied.
  2033. * @see \Cake\Validation\Validation::multiple()
  2034. * @return $this
  2035. */
  2036. public function multipleOptions($field, array $options = [], $message = null, $when = null)
  2037. {
  2038. $extra = array_filter(['on' => $when, 'message' => $message]);
  2039. $caseInsensitive = isset($options['caseInsensitive']) ? $options['caseInsensitive'] : false;
  2040. unset($options['caseInsensitive']);
  2041. return $this->add($field, 'multipleOptions', $extra + [
  2042. 'rule' => ['multiple', $options, $caseInsensitive]
  2043. ]);
  2044. }
  2045. /**
  2046. * Add a validation rule to ensure that a field is an array containing at least
  2047. * the specified amount of elements
  2048. *
  2049. * @param string $field The field you want to apply the rule to.
  2050. * @param int $count The number of elements the array should at least have
  2051. * @param string|null $message The error message when the rule fails.
  2052. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  2053. * true when the validation rule should be applied.
  2054. * @see \Cake\Validation\Validation::numElements()
  2055. * @return $this
  2056. */
  2057. public function hasAtLeast($field, $count, $message = null, $when = null)
  2058. {
  2059. $extra = array_filter(['on' => $when, 'message' => $message]);
  2060. return $this->add($field, 'hasAtLeast', $extra + [
  2061. 'rule' => function ($value) use ($count) {
  2062. if (is_array($value) && isset($value['_ids'])) {
  2063. $value = $value['_ids'];
  2064. }
  2065. return Validation::numElements($value, Validation::COMPARE_GREATER_OR_EQUAL, $count);
  2066. }
  2067. ]);
  2068. }
  2069. /**
  2070. * Add a validation rule to ensure that a field is an array containing at most
  2071. * the specified amount of elements
  2072. *
  2073. * @param string $field The field you want to apply the rule to.
  2074. * @param int $count The number maximum amount of elements the field should have
  2075. * @param string|null $message The error message when the rule fails.
  2076. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  2077. * true when the validation rule should be applied.
  2078. * @see \Cake\Validation\Validation::numElements()
  2079. * @return $this
  2080. */
  2081. public function hasAtMost($field, $count, $message = null, $when = null)
  2082. {
  2083. $extra = array_filter(['on' => $when, 'message' => $message]);
  2084. return $this->add($field, 'hasAtMost', $extra + [
  2085. 'rule' => function ($value) use ($count) {
  2086. if (is_array($value) && isset($value['_ids'])) {
  2087. $value = $value['_ids'];
  2088. }
  2089. return Validation::numElements($value, Validation::COMPARE_LESS_OR_EQUAL, $count);
  2090. }
  2091. ]);
  2092. }
  2093. /**
  2094. * Returns whether or not a field can be left empty for a new or already existing
  2095. * record.
  2096. *
  2097. * @param string $field Field name.
  2098. * @param bool $newRecord whether the data to be validated is new or to be updated.
  2099. * @return bool
  2100. */
  2101. public function isEmptyAllowed($field, $newRecord)
  2102. {
  2103. $providers = $this->_providers;
  2104. $data = [];
  2105. $context = compact('data', 'newRecord', 'field', 'providers');
  2106. return $this->_canBeEmpty($this->field($field), $context);
  2107. }
  2108. /**
  2109. * Returns whether or not a field can be left out for a new or already existing
  2110. * record.
  2111. *
  2112. * @param string $field Field name.
  2113. * @param bool $newRecord Whether the data to be validated is new or to be updated.
  2114. * @return bool
  2115. */
  2116. public function isPresenceRequired($field, $newRecord)
  2117. {
  2118. $providers = $this->_providers;
  2119. $data = [];
  2120. $context = compact('data', 'newRecord', 'field', 'providers');
  2121. return !$this->_checkPresence($this->field($field), $context);
  2122. }
  2123. /**
  2124. * Returns whether or not a field matches against a regular expression.
  2125. *
  2126. * @param string $field Field name.
  2127. * @param string $regex Regular expression.
  2128. * @param string|null $message The error message when the rule fails.
  2129. * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
  2130. * true when the validation rule should be applied.
  2131. * @return $this
  2132. */
  2133. public function regex($field, $regex, $message = null, $when = null)
  2134. {
  2135. $extra = array_filter(['on' => $when, 'message' => $message]);
  2136. return $this->add($field, 'regex', $extra + [
  2137. 'rule' => ['custom', $regex]
  2138. ]);
  2139. }
  2140. /**
  2141. * Gets the required message for a field
  2142. *
  2143. * @param string $field Field name
  2144. * @return string|null
  2145. */
  2146. public function getRequiredMessage($field)
  2147. {
  2148. if (!isset($this->_fields[$field])) {
  2149. return null;
  2150. }
  2151. $defaultMessage = 'This field is required';
  2152. if ($this->_useI18n) {
  2153. $defaultMessage = __d('cake', 'This field is required');
  2154. }
  2155. return isset($this->_presenceMessages[$field])
  2156. ? $this->_presenceMessages[$field]
  2157. : $defaultMessage;
  2158. }
  2159. /**
  2160. * Gets the notEmpty message for a field
  2161. *
  2162. * @param string $field Field name
  2163. * @return string|null
  2164. */
  2165. public function getNotEmptyMessage($field)
  2166. {
  2167. if (!isset($this->_fields[$field])) {
  2168. return null;
  2169. }
  2170. $defaultMessage = 'This field cannot be left empty';
  2171. if ($this->_useI18n) {
  2172. $defaultMessage = __d('cake', 'This field cannot be left empty');
  2173. }
  2174. $notBlankMessage = null;
  2175. foreach ($this->_fields[$field] as $rule) {
  2176. if ($rule->get('rule') === 'notBlank' && $rule->get('message')) {
  2177. return $rule->get('message');
  2178. }
  2179. }
  2180. return isset($this->_allowEmptyMessages[$field])
  2181. ? $this->_allowEmptyMessages[$field]
  2182. : $defaultMessage;
  2183. }
  2184. /**
  2185. * Returns false if any validation for the passed rule set should be stopped
  2186. * due to the field missing in the data array
  2187. *
  2188. * @param \Cake\Validation\ValidationSet $field The set of rules for a field.
  2189. * @param array $context A key value list of data containing the validation context.
  2190. * @return bool
  2191. */
  2192. protected function _checkPresence($field, $context)
  2193. {
  2194. $required = $field->isPresenceRequired();
  2195. if (!is_string($required) && is_callable($required)) {
  2196. return !$required($context);
  2197. }
  2198. $newRecord = $context['newRecord'];
  2199. if (in_array($required, ['create', 'update'], true)) {
  2200. return (
  2201. ($required === 'create' && !$newRecord) ||
  2202. ($required === 'update' && $newRecord)
  2203. );
  2204. }
  2205. return !$required;
  2206. }
  2207. /**
  2208. * Returns whether the field can be left blank according to `allowEmpty`
  2209. *
  2210. * @param \Cake\Validation\ValidationSet $field the set of rules for a field
  2211. * @param array $context a key value list of data containing the validation context.
  2212. * @return bool
  2213. */
  2214. protected function _canBeEmpty($field, $context)
  2215. {
  2216. $allowed = $field->isEmptyAllowed();
  2217. if (!is_string($allowed) && is_callable($allowed)) {
  2218. return $allowed($context);
  2219. }
  2220. $newRecord = $context['newRecord'];
  2221. if (in_array($allowed, ['create', 'update'], true)) {
  2222. $allowed = (
  2223. ($allowed === 'create' && $newRecord) ||
  2224. ($allowed === 'update' && !$newRecord)
  2225. );
  2226. }
  2227. return $allowed;
  2228. }
  2229. /**
  2230. * Returns true if the field is empty in the passed data array
  2231. *
  2232. * @param mixed $data Value to check against.
  2233. * @return bool
  2234. * @deprecated 3.7.0 Use isEmpty() instead
  2235. */
  2236. protected function _fieldIsEmpty($data)
  2237. {
  2238. return $this->isEmpty($data, static::EMPTY_ALL);
  2239. }
  2240. /**
  2241. * Returns true if the field is empty in the passed data array
  2242. *
  2243. * @param mixed $data Value to check against.
  2244. * @param int $flags A bitmask of EMPTY_* flags which specify what is empty
  2245. * @return bool
  2246. */
  2247. protected function isEmpty($data, $flags)
  2248. {
  2249. if ($data === null) {
  2250. return true;
  2251. }
  2252. if ($data === '' && ($flags & self::EMPTY_STRING)) {
  2253. return true;
  2254. }
  2255. $arrayTypes = self::EMPTY_ARRAY | self::EMPTY_DATE | self::EMPTY_TIME;
  2256. if ($data === [] && ($flags & $arrayTypes)) {
  2257. return true;
  2258. }
  2259. if (is_array($data)) {
  2260. if (($flags & self::EMPTY_FILE)
  2261. && isset($data['name'], $data['type'], $data['tmp_name'], $data['error'])
  2262. && (int)$data['error'] === UPLOAD_ERR_NO_FILE
  2263. ) {
  2264. return true;
  2265. }
  2266. $allFieldsAreEmpty = true;
  2267. foreach ($data as $field) {
  2268. if ($field !== null && $field !== '') {
  2269. $allFieldsAreEmpty = false;
  2270. break;
  2271. }
  2272. }
  2273. if ($allFieldsAreEmpty) {
  2274. if (($flags & self::EMPTY_DATE) && isset($data['year'])) {
  2275. return true;
  2276. }
  2277. if (($flags & self::EMPTY_TIME) && isset($data['hour'])) {
  2278. return true;
  2279. }
  2280. }
  2281. }
  2282. return false;
  2283. }
  2284. /**
  2285. * Iterates over each rule in the validation set and collects the errors resulting
  2286. * from executing them
  2287. *
  2288. * @param string $field The name of the field that is being processed
  2289. * @param \Cake\Validation\ValidationSet $rules the list of rules for a field
  2290. * @param array $data the full data passed to the validator
  2291. * @param bool $newRecord whether is it a new record or an existing one
  2292. * @return array
  2293. */
  2294. protected function _processRules($field, ValidationSet $rules, $data, $newRecord)
  2295. {
  2296. $errors = [];
  2297. // Loading default provider in case there is none
  2298. $this->getProvider('default');
  2299. $message = 'The provided value is invalid';
  2300. if ($this->_useI18n) {
  2301. $message = __d('cake', 'The provided value is invalid');
  2302. }
  2303. foreach ($rules as $name => $rule) {
  2304. $result = $rule->process($data[$field], $this->_providers, compact('newRecord', 'data', 'field'));
  2305. if ($result === true) {
  2306. continue;
  2307. }
  2308. $errors[$name] = $message;
  2309. if (is_array($result) && $name === static::NESTED) {
  2310. $errors = $result;
  2311. }
  2312. if (is_string($result)) {
  2313. $errors[$name] = $result;
  2314. }
  2315. if ($rule->isLast()) {
  2316. break;
  2317. }
  2318. }
  2319. return $errors;
  2320. }
  2321. /**
  2322. * Get the printable version of this object.
  2323. *
  2324. * @return array
  2325. */
  2326. public function __debugInfo()
  2327. {
  2328. $fields = [];
  2329. foreach ($this->_fields as $name => $fieldSet) {
  2330. $fields[$name] = [
  2331. 'isPresenceRequired' => $fieldSet->isPresenceRequired(),
  2332. 'isEmptyAllowed' => $fieldSet->isEmptyAllowed(),
  2333. 'rules' => array_keys($fieldSet->rules()),
  2334. ];
  2335. }
  2336. return [
  2337. '_presenceMessages' => $this->_presenceMessages,
  2338. '_allowEmptyMessages' => $this->_allowEmptyMessages,
  2339. '_allowEmptyFlags' => $this->_allowEmptyFlags,
  2340. '_useI18n' => $this->_useI18n,
  2341. '_providers' => array_keys($this->_providers),
  2342. '_fields' => $fields
  2343. ];
  2344. }
  2345. }