title: EasySwoole Verifier meta:

  • name: description content: EasySwoole Validator|swoole Parameter validator
  • name: keywords content: swoole|swoole extension|swoole framework|EasySwoole Validator|swoole Parameter validator

Validate

EasySwoole provides a built-in validation class. By default, there is a validate method in the controller. If you want to use other methods or tools to do the verification, you can override this method in the subclass controller to implement other methods. Tool for verification

::: warning Validator class: EasySwoole\Validate\Validate :::

Basic use

  1. <?php
  2. use EasySwoole\Validate\Validate;
  3. $data = [
  4. 'name' => 'blank',
  5. 'age' => 25
  6. ];
  7. $valitor = new Validate();
  8. $valitor->addColumn('name', 'The name is not empty')->required('The name is not empty')->lengthMin(10,'Minimum length is not less than 10 digits');
  9. $bool = $valitor->validate($data);
  10. var_dump($bool?"true":$valitor->getError()->__toString());
  11. /* result:
  12. String(26) "Minimum length is not less than 10 digits"
  13. */

Package use in the controller

  1. namespace App\HttpController;
  2. use EasySwoole\Http\Message\Status;
  3. use EasySwoole\Validate\Validate;
  4. use EasySwoole\Http\AbstractInterface\Controller;
  5. class BaseController extends Controller
  6. {
  7. protected function onRequest(?string $action): ?bool
  8. {
  9. $ret = parent::onRequest($action);
  10. if($ret === false){
  11. return false;
  12. }
  13. $v = $this->validateRule($action);
  14. if($v){
  15. $ret = $this->validate($v);
  16. if($ret == false){
  17. $this->writeJson(Status::CODE_BAD_REQUEST,null,"{$v->getError()->getField()}@{$v->getError()->getFieldAlias()}:{$v->getError()->getErrorRuleMsg()}");
  18. return false;
  19. }
  20. }
  21. return true;
  22. }
  23. protected function validateRule(?string $action):?Validate
  24. {
  25. }
  26. }

::: warning We define a base controller with the validateRule method.。 :::

  1. namespace App\HttpController;
  2. use App\HttpController\Api\BaseController;
  3. use EasySwoole\Validate\Validate;
  4. class Common extends BaseController
  5. {
  6. function sms()
  7. {
  8. $phone = $this->request()->getRequestParam('phone');
  9. }
  10. protected function validateRule(?string $action): ?Validate
  11. {
  12. $v = new Validate();
  13. switch ($action){
  14. case 'sms':{
  15. $v->addColumn('phone','phone number')->required('Can not be empty')->length(11,'Wrong length');
  16. $v->addColumn('verifyCode','Verification code')->required('Can not be empty')->length(4,'Wrong length');
  17. break;
  18. }
  19. }
  20. return $v;
  21. }
  22. }

::: warning In the controller method that needs to be verified, we can add the corresponding verification rule to the corresponding action, so that the automatic verification can be realized, so that the controller method can realize the logic with peace of mind. :::

Method list

Get Error:

  1. function getError():?EasySwoole\Validate\Error

Add a rule to the field:

Version 1.1.9 to the present

  • string name field key
  • string alias alias
  • string reset reset rule
  1. public function addColumn(string $name, ?string $alias = null,bool $reset = false):EasySwoole\Validate\Rule

Version 1.1.0 to 1.1.8

  • string name field key
  • string alias alias
  1. public function addColumn(string $name, ?string $alias = null):EasySwoole\Validate\Rule

Version 1.0.1

  • string name field key
  • string errorMsg error message
  • string alias alias
  1. public function addColumn(string $name,?string $errorMsg = null,?string $alias = null):EasySwoole\Validate\Rule

Version 1.0.0

  • string name field key
  • string alias alias
  • string errorMsg error message
  1. public function addColumn(string $name,?string $alias = null,?string $errorMsg = null):EasySwoole\Validate\Rule

Return a Rule object to add a custom rule.

data verification:

  • array data data
  1. Function validate(array $data)

Validation rule class

Currently the rules supported by the validator are as follows

  1. namespace EasySwoole\Validate;
  2. /**
  3. * Verification rule
  4. * Please sort the verification method in alphabetical order for post maintenance
  5. * Class Rule
  6. * @package EasySwoole\Validate
  7. */
  8. class Rule
  9. {
  10. protected $ruleMap = [];
  11. function getRuleMap(): array
  12. {
  13. return $this->ruleMap;
  14. }
  15. /**
  16. * Whether the given URL can communicate successfully
  17. * @param null|string $msg
  18. * @return $this
  19. */
  20. function activeUrl($msg = null)
  21. {
  22. $this->ruleMap['activeUrl'] = [
  23. 'arg' => null,
  24. 'msg' => $msg
  25. ];
  26. return $this;
  27. }
  28. /**
  29. * Whether the given parameter is a letter, ie [a-zA-Z]
  30. * @param null|string $msg
  31. * @return $this
  32. */
  33. function alpha($msg = null)
  34. {
  35. $this->ruleMap['alpha'] = [
  36. 'arg' => null,
  37. 'msg' => $msg
  38. ];
  39. return $this;
  40. }
  41. function alphaNum($msg = null)
  42. {
  43. $this->ruleMap['alphaNum'] = [
  44. 'arg' => null,
  45. 'msg' => $msg
  46. ];
  47. return $this;
  48. }
  49. function alphaDash($msg = null)
  50. {
  51. $this->ruleMap['alphaDash'] = [
  52. 'arg' => null,
  53. 'msg' => $msg
  54. ];
  55. return $this;
  56. }
  57. /**
  58. * Whether the given parameter is between $min $max
  59. * @param integer $min minimum does not contain this value
  60. * @param integer $max maximum does not contain this value
  61. * @param null|string $msg
  62. * @return $this
  63. */
  64. function between($min, $max, $msg = null)
  65. {
  66. $this->ruleMap['between'] = [
  67. 'msg' => $msg,
  68. 'arg' => [
  69. $min, $max
  70. ]
  71. ];
  72. return $this;
  73. }
  74. /**
  75. * Whether the given parameter is a boolean
  76. * @param null|string $msg
  77. * @return $this
  78. */
  79. function bool($msg = null)
  80. {
  81. $this->ruleMap['bool'] = [
  82. 'msg' => $msg,
  83. 'arg' => null
  84. ];
  85. return $this;
  86. }
  87. /**
  88. * Whether the given parameter is in decimal format
  89. * @param null|integer $precision specifies the number of decimal places null is not specified
  90. * @param null $msg
  91. * @return $this
  92. */
  93. function decimal(?int $precision = null, $msg = null)
  94. {
  95. $this->ruleMap['decimal'] = [
  96. 'msg' => $msg,
  97. 'arg' => $precision
  98. ];
  99. return $this;
  100. }
  101. /**
  102. * Whether the given parameter is before a certain date
  103. * @param null|string $date
  104. * @param null|string $msg
  105. * @return $this
  106. */
  107. function dateBefore(?string $date = null, $msg = null)
  108. {
  109. $this->ruleMap['dateBefore'] = [
  110. 'msg' => $msg,
  111. 'arg' => $date
  112. ];
  113. return $this;
  114. }
  115. /**
  116. * Whether the given parameter is after a certain date
  117. * @param null|string $date
  118. * @param null|string $msg
  119. * @return $this
  120. */
  121. function dateAfter(?string $date = null, $msg = null)
  122. {
  123. $this->ruleMap['dateAfter'] = [
  124. 'msg' => $msg,
  125. 'arg' => $date
  126. ];
  127. return $this;
  128. }
  129. /**
  130. * Verify that the values are equal
  131. * @param $compare
  132. * @param null|string $msg
  133. * @return $this
  134. */
  135. function equal($compare, $msg = null)
  136. {
  137. $this->ruleMap['equal'] = [
  138. 'msg' => $msg,
  139. 'arg' => $compare
  140. ];
  141. return $this;
  142. }
  143. /**
  144. * Verify that the value is a floating point number
  145. * @param null|string $msg
  146. * @return $this
  147. */
  148. function float($msg = null)
  149. {
  150. $this->ruleMap['float'] = [
  151. 'arg' => null,
  152. 'msg' => $msg
  153. ];
  154. return $this;
  155. }
  156. /**
  157. * Call custom closure validation
  158. * @param callable $func
  159. * @param null|string $msg
  160. * @return $this
  161. */
  162. function func(callable $func, $msg = null)
  163. {
  164. $this->ruleMap['func'] = [
  165. 'arg' => $func,
  166. 'msg' => $msg
  167. ];
  168. return $this;
  169. }
  170. /**
  171. * Whether the value is in the array
  172. * @param array $array
  173. * @param bool $isStrict
  174. * @param null|string $msg
  175. * @return $this
  176. */
  177. function inArray(array $array, $isStrict = false, $msg = null)
  178. {
  179. $this->ruleMap['inArray'] = [
  180. 'arg' => [ $array, $isStrict ],
  181. 'msg' => $msg
  182. ];
  183. return $this;
  184. }
  185. /**
  186. * Is it an integer value
  187. * @param null|string $msg
  188. * @return $this
  189. */
  190. function integer($msg = null)
  191. {
  192. $this->ruleMap['integer'] = [
  193. 'arg' => null,
  194. 'msg' => $msg
  195. ];
  196. return $this;
  197. }
  198. /**
  199. * Is it a valid IP?
  200. * @param array $array
  201. * @param null $msg
  202. * @return $this
  203. */
  204. function isIp($msg = null)
  205. {
  206. $this->ruleMap['isIp'] = [
  207. 'arg' => null,
  208. 'msg' => $msg
  209. ];
  210. return $this;
  211. }
  212. /**
  213. * Is it not empty
  214. * @param null $msg
  215. * @return $this
  216. */
  217. function notEmpty($msg = null)
  218. {
  219. $this->ruleMap['notEmpty'] = [
  220. 'arg' => null,
  221. 'msg' => $msg
  222. ];
  223. return $this;
  224. }
  225. /**
  226. * Whether a numeric value
  227. * @param null $msg
  228. * @return $this
  229. */
  230. function numeric($msg = null)
  231. {
  232. $this->ruleMap['numeric'] = [
  233. 'arg' => null,
  234. 'msg' => $msg
  235. ];
  236. return $this;
  237. }
  238. /**
  239. * Not in the array
  240. * @param array $array
  241. * @param bool $isStrict
  242. * @param null $msg
  243. * @return $this
  244. */
  245. function notInArray(array $array, $isStrict = false, $msg = null)
  246. {
  247. $this->ruleMap['notInArray'] = [
  248. 'arg' => [ $array, $isStrict ],
  249. 'msg' => $msg
  250. ];
  251. return $this;
  252. }
  253. /**
  254. * Verify the length of an array or string
  255. * @param int $len
  256. * @param null $msg
  257. * @return $this
  258. */
  259. function length(int $len, $msg = null)
  260. {
  261. $this->ruleMap['length'] = [
  262. 'msg' => $msg,
  263. 'arg' => $len
  264. ];
  265. return $this;
  266. }
  267. /**
  268. * Verify the length of an array or string...
  269. * @param int $lengthMax
  270. * @param null $msg
  271. * @return $this
  272. */
  273. function lengthMax(int $lengthMax, $msg = null)
  274. {
  275. $this->ruleMap['lengthMax'] = [
  276. 'msg' => $msg,
  277. 'arg' => $lengthMax
  278. ];
  279. return $this;
  280. }
  281. /**
  282. * Verify that the length of the array or string is reached
  283. * @param int $lengthMin
  284. * @param null $msg
  285. * @return $this
  286. */
  287. function lengthMin(int $lengthMin, $msg = null)
  288. {
  289. $this->ruleMap['lengthMin'] = [
  290. 'msg' => $msg,
  291. 'arg' => $lengthMin
  292. ];
  293. return $this;
  294. }
  295. /**
  296. * Verify that the length of the array or string is within a range
  297. * @param null $msg
  298. * @return $this
  299. */
  300. function betweenLen(int $min, int $max, $msg = null)
  301. {
  302. $this->ruleMap['betweenLen'] = [
  303. 'msg' => $msg,
  304. 'arg' => [
  305. $min,
  306. $max
  307. ]
  308. ];
  309. return $this;
  310. }
  311. /**
  312. * Verification value is not greater than (equal is considered not passed)
  313. * @param int $max
  314. * @param null|string $msg
  315. * @return Rule
  316. */
  317. function max(int $max, ?string $msg = null): Rule
  318. {
  319. $this->ruleMap['max'] = [
  320. 'arg' => $max,
  321. 'msg' => $msg
  322. ];
  323. return $this;
  324. }
  325. /**
  326. * The verification value is not less than (equal is considered not passed)
  327. * @param int $min
  328. * @param null|string $msg
  329. * @return Rule
  330. */
  331. function min(int $min, ?string $msg = null): Rule
  332. {
  333. $this->ruleMap['min'] = [
  334. 'arg' => $min,
  335. 'msg' => $msg
  336. ];
  337. return $this;
  338. }
  339. /**
  340. * The verification value is a legal amount
  341. * 100 | 100.1 | 100.01
  342. * @param integer|null $precision Decimal point
  343. * @param string|null $msg
  344. * @return Rule
  345. */
  346. function money(?int $precision = null, string $msg = null): Rule
  347. {
  348. $this->ruleMap['money'] = [
  349. 'arg' => $precision,
  350. 'msg' => $msg
  351. ];
  352. return $this;
  353. }
  354. /**
  355. * Setting value is optional
  356. * @return $this
  357. */
  358. function optional()
  359. {
  360. $this->ruleMap['optional'] = [
  361. 'arg' => null,
  362. 'msg' => null
  363. ];
  364. return $this;
  365. }
  366. /**
  367. * Regular expression verification
  368. * @param $reg
  369. * @param null $msg
  370. * @return $this
  371. */
  372. function regex($reg, $msg = null)
  373. {
  374. $this->ruleMap['regex'] = [
  375. 'arg' => $reg,
  376. 'msg' => $msg
  377. ];
  378. return $this;
  379. }
  380. /**
  381. * Must have a value
  382. * @param null $msg
  383. * @return $this
  384. */
  385. function required($msg = null)
  386. {
  387. $this->ruleMap['required'] = [
  388. 'arg' => null,
  389. 'msg' => $msg
  390. ];
  391. return $this;
  392. }
  393. /**
  394. * Value is a legal timestamp
  395. * @param null $msg
  396. * @return $this
  397. */
  398. function timestamp($msg = null)
  399. {
  400. $this->ruleMap['timestamp'] = [
  401. 'arg' => null,
  402. 'msg' => $msg
  403. ];
  404. return $this;
  405. }
  406. /**
  407. * Timestamp before a specified date
  408. * @param string $date Pass in any string that can be parsed by strtotime
  409. * @param null $msg
  410. * @return $this
  411. */
  412. function timestampBeforeDate($date, $msg = null)
  413. {
  414. $this->ruleMap['timestampBeforeDate'] = [
  415. 'arg' => $date,
  416. 'msg' => $msg
  417. ];
  418. return $this;
  419. }
  420. /**
  421. * Timestamp after a specified date
  422. * @param string $date Pass in any string that can be parsed by strtotime
  423. * @param null $msg
  424. * @return $this
  425. */
  426. function timestampAfterDate($date, $msg = null)
  427. {
  428. $this->ruleMap['timestampAfterDate'] = [
  429. 'arg' => $date,
  430. 'msg' => $msg
  431. ];
  432. return $this;
  433. }
  434. /**
  435. * Specify a timestamp before a timestamp
  436. * @param string|integer $beforeTimestamp before this timestamp
  437. * @param null $msg
  438. * @return $this
  439. */
  440. function timestampBefore($beforeTimestamp, $msg = null)
  441. {
  442. $this->ruleMap['timestampBefore'] = [
  443. 'arg' => $beforeTimestamp,
  444. 'msg' => $msg
  445. ];
  446. return $this;
  447. }
  448. /**
  449. * Specify the timestamp after a timestamp
  450. * @param string|integer $afterTimestamp after this timestamp
  451. * @param null $msg
  452. * @return $this
  453. */
  454. function timestampAfter($afterTimestamp, $msg = null)
  455. {
  456. $this->ruleMap['timestampAfter'] = [
  457. 'arg' => $afterTimestamp,
  458. 'msg' => $msg
  459. ];
  460. return $this;
  461. }
  462. /**
  463. * Value is a legal link
  464. * @param null $msg
  465. * @return $this
  466. */
  467. function url($msg = null)
  468. {
  469. $this->ruleMap['url'] = [
  470. 'arg' => null,
  471. 'msg' => $msg
  472. ];
  473. return $this;
  474. }
  475. /**
  476. * Value is a legal link
  477. * @param null $msg
  478. * @return $this
  479. */
  480. function allDigital($msg = null)
  481. {
  482. $this->ruleMap['allDigital'] = [
  483. 'arg' => null,
  484. 'msg' => $msg
  485. ];
  486. return $this;
  487. }
  488. }