HasFieldValidator.php 9.2 KB

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