ListField.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form\Field;
  5. use Illuminate\Support\Arr;
  6. class ListField extends Field
  7. {
  8. const DEFAULT_FLAG_NAME = '_def_';
  9. /**
  10. * Max list size.
  11. *
  12. * @var int
  13. */
  14. protected $max;
  15. /**
  16. * Minimum list size.
  17. *
  18. * @var int
  19. */
  20. protected $min = 0;
  21. /**
  22. * @var array
  23. */
  24. protected $value = [''];
  25. /**
  26. * Set Max list size.
  27. *
  28. * @param int $size
  29. *
  30. * @return $this
  31. */
  32. public function max(int $size)
  33. {
  34. $this->max = $size;
  35. return $this;
  36. }
  37. /**
  38. * Set Minimum list size.
  39. *
  40. * @param int $size
  41. *
  42. * @return $this
  43. */
  44. public function min(int $size)
  45. {
  46. $this->min = $size;
  47. return $this;
  48. }
  49. /**
  50. * Fill data to the field.
  51. *
  52. * @param array $data
  53. *
  54. * @return void
  55. */
  56. public function formatFieldData($data)
  57. {
  58. $this->data = $data;
  59. return Arr::get($data, $this->column, $this->value);
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getValidator(array $input)
  65. {
  66. if ($this->validator) {
  67. return $this->validator->call($this, $input);
  68. }
  69. if (!is_string($this->column)) {
  70. return false;
  71. }
  72. $rules = $attributes = [];
  73. if (!$fieldRules = $this->getRules()) {
  74. return false;
  75. }
  76. if (!Arr::has($input, $this->column)) {
  77. return false;
  78. }
  79. $rules["{$this->column}.values.*"] = $fieldRules;
  80. $attributes["{$this->column}.values.*"] = __('Value');
  81. $rules["{$this->column}.values"][] = 'array';
  82. if (!is_null($this->max)) {
  83. $rules["{$this->column}.values"][] = "max:$this->max";
  84. }
  85. if (!is_null($this->min)) {
  86. $rules["{$this->column}.values"][] = "min:$this->min";
  87. }
  88. $attributes["{$this->column}.values"] = $this->label;
  89. $input = $this->prepareValidatorInput($input);
  90. return validator($input, $rules, $this->getValidationMessages(), $attributes);
  91. }
  92. protected function prepareValidatorInput(array $input)
  93. {
  94. Arr::forget($input, "{$this->column}.values.".static::DEFAULT_FLAG_NAME);
  95. return $input;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. protected function setupScript()
  101. {
  102. $value = old($this->column, $this->value());
  103. $number = $value ? count($value) : 0;
  104. $this->script = <<<JS
  105. (function () {
  106. var index = {$number};
  107. $('.{$this->column}-add').on('click', function () {
  108. var tpl = $('template.{$this->column}-tpl').html().replace('{key}', index);
  109. $('tbody.list-{$this->column}-table').append(tpl);
  110. index++;
  111. });
  112. $('tbody').on('click', '.{$this->column}-remove', function () {
  113. $(this).closest('tr').remove();
  114. });
  115. })();
  116. JS;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. protected function prepareToSave($value)
  122. {
  123. unset($value['values'][static::DEFAULT_FLAG_NAME]);
  124. if (empty($value['values'])) {
  125. return [];
  126. }
  127. return array_values($value['values']);
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function render()
  133. {
  134. $this->setupScript();
  135. Admin::style('td .form-group {margin-bottom: 0 !important;}');
  136. return parent::render();
  137. }
  138. }