Embeds.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Contracts\FieldsCollection;
  4. use Dcat\Admin\Form\EmbeddedForm;
  5. use Dcat\Admin\Form\Field;
  6. use Dcat\Admin\Form\ResolveField;
  7. use Dcat\Admin\Support\Helper;
  8. use Illuminate\Support\Arr;
  9. use Illuminate\Support\Facades\Validator;
  10. use Illuminate\Support\Str;
  11. class Embeds extends Field implements FieldsCollection
  12. {
  13. use ResolveField;
  14. /**
  15. * @var \Closure
  16. */
  17. protected $builder = null;
  18. /**
  19. * Create a new HasMany field instance.
  20. *
  21. * @param string $column
  22. * @param array $arguments
  23. */
  24. public function __construct($column, $arguments = [])
  25. {
  26. $this->column = $column;
  27. if (count($arguments) == 1) {
  28. $this->label = $this->formatLabel();
  29. $this->builder = $arguments[0];
  30. }
  31. if (count($arguments) == 2) {
  32. [$this->label, $this->builder] = $arguments;
  33. }
  34. }
  35. /**
  36. * Prepare input data for insert or update.
  37. *
  38. * @param array $input
  39. * @return array
  40. */
  41. protected function prepareInputValue($input)
  42. {
  43. $form = $this->buildEmbeddedForm();
  44. return $form->setOriginal($this->original)->prepare($input);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getValidator(array $input)
  50. {
  51. if (! Arr::has($input, $this->column)) {
  52. return false;
  53. }
  54. //$input = Arr::only($input, $this->column);
  55. $rules = $attributes = $messages = [];
  56. /** @var Field $field */
  57. foreach ($this->buildEmbeddedForm()->fields() as $field) {
  58. if (! $fieldRules = $field->getRules()) {
  59. continue;
  60. }
  61. File::deleteRules($field, $fieldRules);
  62. $column = $field->column();
  63. /*
  64. *
  65. * For single column field format rules to:
  66. * [
  67. * 'extra.name' => 'required'
  68. * 'extra.email' => 'required'
  69. * ]
  70. *
  71. * For multiple column field with rules like 'required':
  72. * 'extra' => [
  73. * 'start' => 'start_at'
  74. * 'end' => 'end_at',
  75. * ]
  76. *
  77. * format rules to:
  78. * [
  79. * 'extra.start_atstart' => 'required'
  80. * 'extra.end_atend' => 'required'
  81. * ]
  82. */
  83. if (is_array($column)) {
  84. foreach ($column as $key => $name) {
  85. $rules["{$this->column}.$name$key"] = $fieldRules;
  86. }
  87. $this->resetInputKey($input, $column);
  88. } else {
  89. $rules["{$this->column}.$column"] = $fieldRules;
  90. }
  91. /**
  92. * For single column field format attributes to:
  93. * [
  94. * 'extra.name' => $label
  95. * 'extra.email' => $label
  96. * ].
  97. *
  98. * For multiple column field with rules like 'required':
  99. * 'extra' => [
  100. * 'start' => 'start_at'
  101. * 'end' => 'end_at',
  102. * ]
  103. *
  104. * format rules to:
  105. * [
  106. * 'extra.start_atstart' => "$label[start_at]"
  107. * 'extra.end_atend' => "$label[end_at]"
  108. * ]
  109. */
  110. $attributes = array_merge(
  111. $attributes,
  112. $this->formatValidationAttribute($input, $field->label(), $column)
  113. );
  114. $messages = array_merge(
  115. $messages,
  116. $this->formatValidationMessages($input, $field->getValidationMessages())
  117. );
  118. }
  119. if (empty($rules)) {
  120. return false;
  121. }
  122. return Validator::make($input, $rules, array_merge($this->getValidationMessages(), $messages), $attributes);
  123. }
  124. /**
  125. * Format validation messages.
  126. *
  127. * @param array $input
  128. * @param array $messages
  129. * @return array
  130. */
  131. protected function formatValidationMessages(array $input, array $messages)
  132. {
  133. $result = [];
  134. foreach ($messages as $k => $message) {
  135. $result[$this->column.'.'.$k] = $message;
  136. }
  137. return $result;
  138. }
  139. /**
  140. * Format validation attributes.
  141. *
  142. * @param array $input
  143. * @param string $label
  144. * @param string $column
  145. * @return array
  146. */
  147. protected function formatValidationAttribute($input, $label, $column)
  148. {
  149. $new = $attributes = [];
  150. if (is_array($column)) {
  151. foreach ($column as $index => $col) {
  152. $new[$col.$index] = $col;
  153. }
  154. }
  155. foreach (array_keys(Arr::dot($input)) as $key) {
  156. if (is_string($column)) {
  157. if (Str::endsWith($key, ".$column")) {
  158. $attributes[$key] = $label;
  159. }
  160. } else {
  161. foreach ($new as $k => $val) {
  162. if (Str::endsWith($key, ".$k")) {
  163. $attributes[$key] = $label."[$val]";
  164. }
  165. }
  166. }
  167. }
  168. return $attributes;
  169. }
  170. /**
  171. * Reset input key for validation.
  172. *
  173. * @param array $input
  174. * @param array $column $column is the column name array set
  175. * @return void.
  176. */
  177. public function resetInputKey(array &$input, array $column)
  178. {
  179. $column = array_flip($column);
  180. foreach (Arr::get($input, $this->column) as $key => $value) {
  181. if (! array_key_exists($key, $column)) {
  182. continue;
  183. }
  184. $newKey = $key.$column[$key];
  185. /*
  186. * set new key
  187. */
  188. Arr::set($input, "{$this->column}.$newKey", $value);
  189. /*
  190. * forget the old key and value
  191. */
  192. Arr::forget($input, "{$this->column}.$key");
  193. }
  194. }
  195. /**
  196. * Get data for Embedded form.
  197. *
  198. * Normally, data is obtained from the database.
  199. *
  200. * When the data validation errors, data is obtained from session flash.
  201. *
  202. * @return array
  203. */
  204. protected function getEmbeddedData()
  205. {
  206. return Helper::array($this->value);
  207. }
  208. /**
  209. * Build a Embedded Form and fill data.
  210. *
  211. * @return EmbeddedForm
  212. */
  213. protected function buildEmbeddedForm()
  214. {
  215. $form = new EmbeddedForm($this->column);
  216. $form->setParent($this->form);
  217. $form->setResolvingFieldCallbacks($this->resolvingFieldCallbacks);
  218. call_user_func($this->builder, $form);
  219. $form->fill($this->getEmbeddedData());
  220. return $form;
  221. }
  222. /**
  223. * Render the form.
  224. *
  225. * @return \Illuminate\View\View
  226. */
  227. public function render()
  228. {
  229. $this->addVariables(['form' => $this->buildEmbeddedForm()]);
  230. return parent::render();
  231. }
  232. /**
  233. * 根据字段名称查找字段.
  234. *
  235. * @param string $column
  236. * @return Field|null
  237. */
  238. public function field($name)
  239. {
  240. return $this->buildEmbeddedForm()->fields()->first(function (Field $field) use ($name) {
  241. return $field->column() == $name;
  242. });
  243. }
  244. /**
  245. * 获取所有字段
  246. *
  247. * @return void
  248. */
  249. public function fields()
  250. {
  251. return $this->buildEmbeddedForm()->fields();
  252. }
  253. }