ListField.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /**
  9. * Max list size.
  10. *
  11. * @var int
  12. */
  13. protected $max;
  14. /**
  15. * Minimum list size.
  16. *
  17. * @var int
  18. */
  19. protected $min = 0;
  20. /**
  21. * @var array
  22. */
  23. protected $value = [''];
  24. /**
  25. * Set Max list size.
  26. *
  27. * @param int $size
  28. *
  29. * @return $this
  30. */
  31. public function max(int $size)
  32. {
  33. $this->max = $size;
  34. return $this;
  35. }
  36. /**
  37. * Set Minimum list size.
  38. *
  39. * @param int $size
  40. *
  41. * @return $this
  42. */
  43. public function min(int $size)
  44. {
  45. $this->min = $size;
  46. return $this;
  47. }
  48. /**
  49. * Fill data to the field.
  50. *
  51. * @param array $data
  52. *
  53. * @return void
  54. */
  55. public function formatFieldData($data)
  56. {
  57. $this->data = $data;
  58. return Arr::get($data, $this->column, $this->value);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getValidator(array $input)
  64. {
  65. if ($this->validator) {
  66. return $this->validator->call($this, $input);
  67. }
  68. if (!is_string($this->column)) {
  69. return false;
  70. }
  71. $rules = $attributes = [];
  72. if (!$fieldRules = $this->getRules()) {
  73. return false;
  74. }
  75. if (!Arr::has($input, $this->column)) {
  76. return false;
  77. }
  78. $rules["{$this->column}.values.*"] = $fieldRules;
  79. $attributes["{$this->column}.values.*"] = __('Value');
  80. $rules["{$this->column}.values"][] = 'array';
  81. if (!is_null($this->max)) {
  82. $rules["{$this->column}.values"][] = "max:$this->max";
  83. }
  84. if (!is_null($this->min)) {
  85. $rules["{$this->column}.values"][] = "min:$this->min";
  86. }
  87. $attributes["{$this->column}.values"] = $this->label;
  88. return validator($input, $rules, $this->getValidationMessages(), $attributes);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. protected function setupScript()
  94. {
  95. $this->script = <<<JS
  96. $('.{$this->column}-add').on('click', function () {
  97. var tpl = $('template.{$this->column}-tpl').html();
  98. $('tbody.list-{$this->column}-table').append(tpl);
  99. });
  100. $('tbody').on('click', '.{$this->column}-remove', function () {
  101. $(this).closest('tr').remove();
  102. });
  103. JS;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function prepare($value)
  109. {
  110. return array_values($value['values']);
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function render()
  116. {
  117. $this->setupScript();
  118. Admin::style('td .form-group {margin-bottom: 0 !important;}');
  119. return parent::render();
  120. }
  121. }