HasFieldValidator.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 string|\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. if (is_array($rules)) {
  82. $thisRuleArr = array_filter(explode('|', $this->rules));
  83. $this->rules = array_merge($thisRuleArr, $rules);
  84. } elseif (is_string($rules)) {
  85. $rules = array_filter(explode('|', "{$this->rules}|$rules"));
  86. $this->rules = implode('|', $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 (!$id = $this->form->getKey()) {
  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. * @return void
  194. */
  195. protected function deleteRuleByKeyword(&$rules, $rule)
  196. {
  197. if (is_array($rules)) {
  198. array_delete($rules, $rule);
  199. return;
  200. }
  201. if (!is_string($rules)) {
  202. return;
  203. }
  204. $pattern = "/{$rule}[^\|]?(\||$)/";
  205. $rules = preg_replace($pattern, '', $rules, -1);
  206. }
  207. /**
  208. * @param string $rule
  209. *
  210. * @return bool
  211. */
  212. public function hasUpdateRule($rule)
  213. {
  214. return $this->isRuleExists($this->updateRules, $rule);
  215. }
  216. /**
  217. * @param string $rule
  218. *
  219. * @return bool
  220. */
  221. public function hasCreationRule($rule)
  222. {
  223. return $this->isRuleExists($this->creationRules, $rule);
  224. }
  225. /**
  226. * @param string $rule
  227. *
  228. * @return bool
  229. */
  230. public function hasRule($rule)
  231. {
  232. return $this->isRuleExists($this->rules, $rule);
  233. }
  234. /**
  235. * @param $rules
  236. * @param $rule
  237. * @return bool
  238. */
  239. protected function isRuleExists($rules, $rule)
  240. {
  241. if (is_array($rules)) {
  242. return in_array($rule, $rules);
  243. }
  244. if (!is_string($rules)) {
  245. return false;
  246. }
  247. $pattern = "/{$rule}[^\|]?(\||$)/";
  248. return (bool)preg_match($pattern, $rules);
  249. }
  250. /**
  251. * Set field validator.
  252. *
  253. * @param callable $validator
  254. *
  255. * @return $this
  256. */
  257. public function validator(callable $validator)
  258. {
  259. $this->validator = $validator;
  260. return $this;
  261. }
  262. /**
  263. * Get validator for this field.
  264. *
  265. * @param array $input
  266. *
  267. * @return bool|Validator
  268. */
  269. public function getValidator(array $input)
  270. {
  271. if ($this->validator) {
  272. return $this->validator->call($this, $input);
  273. }
  274. $rules = $attributes = [];
  275. if (!$fieldRules = $this->getRules()) {
  276. return false;
  277. }
  278. if (is_string($this->column)) {
  279. if (!Arr::has($input, $this->column)) {
  280. return false;
  281. }
  282. $input = $this->sanitizeInput($input, $this->column);
  283. $rules[$this->column] = $fieldRules;
  284. $attributes[$this->column] = $this->label;
  285. }
  286. if (is_array($this->column)) {
  287. foreach ($this->column as $key => $column) {
  288. if (!array_key_exists($column, $input)) {
  289. continue;
  290. }
  291. $input[$column . $key] = Arr::get($input, $column);
  292. $rules[$column . $key] = $fieldRules;
  293. $attributes[$column . $key] = $this->label . "[$column]";
  294. }
  295. }
  296. return Validator::make($input, $rules, $this->getValidationMessages(), $attributes);
  297. }
  298. /**
  299. * Set validation messages for column.
  300. *
  301. * @param string $key
  302. * @param array $messages
  303. *
  304. * @return $this
  305. */
  306. public function setValidationMessages($key, array $messages)
  307. {
  308. $this->validationMessages[$key] = $messages;
  309. return $this;
  310. }
  311. /**
  312. * Get validation messages for the field.
  313. *
  314. * @return array|mixed
  315. */
  316. public function getValidationMessages()
  317. {
  318. // Default validation message.
  319. $messages = $this->validationMessages['default'] ?? [];
  320. if (request()->isMethod('POST')) {
  321. $messages = $this->validationMessages['creation'] ?? $messages;
  322. } elseif (request()->isMethod('PUT')) {
  323. $messages = $this->validationMessages['update'] ?? $messages;
  324. }
  325. $result = [];
  326. foreach ($messages as $k => $v) {
  327. if (Str::contains($k, '.')) {
  328. $result[$k] = $v;
  329. continue;
  330. }
  331. if (is_string($this->column)) {
  332. $k = $this->column . '.' . $k;
  333. $result[$k] = $v;
  334. continue;
  335. }
  336. foreach ($this->column as $column) {
  337. $result[$column.'.'.$k] = $v;
  338. }
  339. }
  340. return $result;
  341. }
  342. }