HasFieldValidator.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. namespace Dcat\Admin\Form\Concerns;
  3. use Dcat\Admin\Form;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Facades\Validator;
  6. use Illuminate\Support\Str;
  7. /**
  8. * @property Form $form
  9. */
  10. trait HasFieldValidator
  11. {
  12. /**
  13. * The validation rules for creation.
  14. *
  15. * @var array|\Closure
  16. */
  17. protected $creationRules = [];
  18. /**
  19. * The validation rules for updates.
  20. *
  21. * @var array|\Closure
  22. */
  23. protected $updateRules = [];
  24. /**
  25. * Validation rules.
  26. *
  27. * @var array|\Closure
  28. */
  29. protected $rules = [];
  30. /**
  31. * @var \Closure
  32. */
  33. protected $validator;
  34. /**
  35. * Validation messages.
  36. *
  37. * @var array
  38. */
  39. protected $validationMessages = [];
  40. /**
  41. * Set the update validation rules for the field.
  42. *
  43. * @param array|callable|string $rules
  44. * @param array $messages
  45. *
  46. * @return $this
  47. */
  48. public function updateRules($rules = null, $messages = [])
  49. {
  50. $this->updateRules = $this->mergeRules($rules, $this->updateRules);
  51. $this->setValidationMessages('update', $messages);
  52. return $this;
  53. }
  54. /**
  55. * Set the creation validation rules for the field.
  56. *
  57. * @param array|callable|string $rules
  58. * @param array $messages
  59. *
  60. * @return $this
  61. */
  62. public function creationRules($rules = null, $messages = [])
  63. {
  64. $this->creationRules = $this->mergeRules($rules, $this->creationRules);
  65. $this->setValidationMessages('creation', $messages);
  66. return $this;
  67. }
  68. /**
  69. * Get or set rules.
  70. *
  71. * @param null $rules
  72. * @param array $messages
  73. *
  74. * @return $this
  75. */
  76. public function rules($rules = null, $messages = [])
  77. {
  78. if ($rules instanceof \Closure) {
  79. $this->rules = $rules;
  80. }
  81. $originalRules = is_array($this->rules) ? $this->rules : [];
  82. if (is_array($rules)) {
  83. $this->rules = array_merge($originalRules, $rules);
  84. } elseif (is_string($rules)) {
  85. $this->rules = array_merge($originalRules, array_filter(explode('|', $rules)));
  86. }
  87. $this->setValidationMessages('default', $messages);
  88. return $this;
  89. }
  90. /**
  91. * Get field validation rules.
  92. *
  93. * @return string
  94. */
  95. protected function getRules()
  96. {
  97. if (request()->isMethod('POST')) {
  98. $rules = $this->creationRules ?: $this->rules;
  99. } elseif (request()->isMethod('PUT')) {
  100. $rules = $this->updateRules ?: $this->rules;
  101. } else {
  102. $rules = $this->rules;
  103. }
  104. if ($rules instanceof \Closure) {
  105. $rules = $rules->call($this, $this->form);
  106. }
  107. if (is_string($rules)) {
  108. $rules = array_filter(explode('|', $rules));
  109. }
  110. if (!$this->form) {
  111. return $rules;
  112. }
  113. if (method_exists($this->form, 'getKey') || ! $id = $this->form->getKey()) {
  114. return $rules;
  115. }
  116. if (is_array($rules)) {
  117. foreach ($rules as &$rule) {
  118. if (is_string($rule)) {
  119. $rule = str_replace('{{id}}', $id, $rule);
  120. }
  121. }
  122. }
  123. return $rules;
  124. }
  125. /**
  126. * Format validation rules.
  127. *
  128. * @param array|string $rules
  129. *
  130. * @return array
  131. */
  132. protected function formatRules($rules)
  133. {
  134. if (is_string($rules)) {
  135. $rules = array_filter(explode('|', $rules));
  136. }
  137. return array_filter((array) $rules);
  138. }
  139. /**
  140. * @param string|array|\Closure $input
  141. * @param string|array $original
  142. *
  143. * @return array|\Closure
  144. */
  145. protected function mergeRules($input, $original)
  146. {
  147. if ($input instanceof \Closure) {
  148. $rules = $input;
  149. } else {
  150. if (!empty($original)) {
  151. $original = $this->formatRules($original);
  152. }
  153. $rules = array_merge($original, $this->formatRules($input));
  154. }
  155. return $rules;
  156. }
  157. /**
  158. * @param string $rule
  159. *
  160. * @return $this
  161. */
  162. public function removeUpdateRule($rule)
  163. {
  164. $this->deleteRuleByKeyword($this->updateRules, $rule);
  165. return $this;
  166. }
  167. /**
  168. * @param string $rule
  169. *
  170. * @return $this
  171. */
  172. public function removeCreationRule($rule)
  173. {
  174. $this->deleteRuleByKeyword($this->creationRules, $rule);
  175. return $this;
  176. }
  177. /**
  178. * Remove a specific rule by keyword.
  179. *
  180. * @param string $rule
  181. *
  182. * @return $this
  183. */
  184. public function removeRule($rule)
  185. {
  186. $this->deleteRuleByKeyword($this->rules, $rule);
  187. return $this;
  188. }
  189. /**
  190. * @param $rules
  191. * @param $rule
  192. * @return void
  193. */
  194. protected function deleteRuleByKeyword(&$rules, $rule)
  195. {
  196. if (is_array($rules)) {
  197. array_delete($rules, $rule);
  198. return;
  199. }
  200. if (!is_string($rules)) {
  201. return;
  202. }
  203. $pattern = "/{$rule}[^\|]?(\||$)/";
  204. $rules = preg_replace($pattern, '', $rules, -1);
  205. }
  206. /**
  207. * @param string $rule
  208. *
  209. * @return bool
  210. */
  211. public function hasUpdateRule($rule)
  212. {
  213. return $this->isRuleExists($this->updateRules, $rule);
  214. }
  215. /**
  216. * @param string $rule
  217. *
  218. * @return bool
  219. */
  220. public function hasCreationRule($rule)
  221. {
  222. return $this->isRuleExists($this->creationRules, $rule);
  223. }
  224. /**
  225. * @param string $rule
  226. *
  227. * @return bool
  228. */
  229. public function hasRule($rule)
  230. {
  231. return $this->isRuleExists($this->rules, $rule);
  232. }
  233. /**
  234. * @param $rules
  235. * @param $rule
  236. * @return bool
  237. */
  238. protected function isRuleExists($rules, $rule)
  239. {
  240. if (is_array($rules)) {
  241. return in_array($rule, $rules);
  242. }
  243. if (!is_string($rules)) {
  244. return false;
  245. }
  246. $pattern = "/{$rule}[^\|]?(\||$)/";
  247. return (bool)preg_match($pattern, $rules);
  248. }
  249. /**
  250. * Set field validator.
  251. *
  252. * @param callable $validator
  253. *
  254. * @return $this
  255. */
  256. public function validator(callable $validator)
  257. {
  258. $this->validator = $validator;
  259. return $this;
  260. }
  261. /**
  262. * Get validator for this field.
  263. *
  264. * @param array $input
  265. *
  266. * @return bool|Validator
  267. */
  268. public function getValidator(array $input)
  269. {
  270. if ($this->validator) {
  271. return $this->validator->call($this, $input);
  272. }
  273. $rules = $attributes = [];
  274. if (!$fieldRules = $this->getRules()) {
  275. return false;
  276. }
  277. if (is_string($this->column)) {
  278. if (!Arr::has($input, $this->column)) {
  279. return false;
  280. }
  281. $input = $this->sanitizeInput($input, $this->column);
  282. $rules[$this->column] = $fieldRules;
  283. $attributes[$this->column] = $this->label;
  284. }
  285. if (is_array($this->column)) {
  286. foreach ($this->column as $key => $column) {
  287. if (!array_key_exists($column, $input)) {
  288. continue;
  289. }
  290. $input[$column . $key] = Arr::get($input, $column);
  291. $rules[$column . $key] = $fieldRules;
  292. $attributes[$column . $key] = $this->label . "[$column]";
  293. }
  294. }
  295. return Validator::make($input, $rules, $this->getValidationMessages(), $attributes);
  296. }
  297. /**
  298. * Set validation messages for column.
  299. *
  300. * @param string $key
  301. * @param array $messages
  302. *
  303. * @return $this
  304. */
  305. public function setValidationMessages($key, array $messages)
  306. {
  307. $this->validationMessages[$key] = $messages;
  308. return $this;
  309. }
  310. /**
  311. * Get validation messages for the field.
  312. *
  313. * @return array|mixed
  314. */
  315. public function getValidationMessages()
  316. {
  317. // Default validation message.
  318. $messages = $this->validationMessages['default'] ?? [];
  319. if (request()->isMethod('POST')) {
  320. $messages = $this->validationMessages['creation'] ?? $messages;
  321. } elseif (request()->isMethod('PUT')) {
  322. $messages = $this->validationMessages['update'] ?? $messages;
  323. }
  324. $result = [];
  325. foreach ($messages as $k => $v) {
  326. if (Str::contains($k, '.')) {
  327. $result[$k] = $v;
  328. continue;
  329. }
  330. if (is_string($this->column)) {
  331. $k = $this->column . '.' . $k;
  332. $result[$k] = $v;
  333. continue;
  334. }
  335. foreach ($this->column as $column) {
  336. $result[$column.'.'.$k] = $v;
  337. }
  338. }
  339. return $result;
  340. }
  341. /**
  342. * Set error messages for individual form field.
  343. *
  344. * @see http://1000hz.github.io/bootstrap-validator/
  345. *
  346. * @param string $error
  347. * @param string $key
  348. * @return $this
  349. */
  350. public function setClientValidationError(string $error, string $key = null)
  351. {
  352. $key = $key ? "{$key}-" : '';
  353. return $this->attribute("data-{$key}error", $error);
  354. }
  355. }