ListField.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Form\Field;
  4. use Dcat\Admin\Support\Helper;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\MessageBag;
  7. class ListField extends Field
  8. {
  9. const DEFAULT_FLAG_NAME = '_def_';
  10. /**
  11. * Max list size.
  12. *
  13. * @var int
  14. */
  15. protected $max;
  16. /**
  17. * Minimum list size.
  18. *
  19. * @var int
  20. */
  21. protected $min = 0;
  22. /**
  23. * Set Max list size.
  24. *
  25. * @param int $size
  26. *
  27. * @return $this
  28. */
  29. public function max(int $size)
  30. {
  31. $this->max = $size;
  32. return $this;
  33. }
  34. /**
  35. * Set Minimum list size.
  36. *
  37. * @param int $size
  38. *
  39. * @return $this
  40. */
  41. public function min(int $size)
  42. {
  43. $this->min = $size;
  44. return $this;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function formatFieldData($data)
  50. {
  51. $this->data = $data;
  52. $value = Helper::array($this->getValueFromData($data, null, $this->value));
  53. unset($value['values'][static::DEFAULT_FLAG_NAME]);
  54. return $value;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getValidator(array $input)
  60. {
  61. if ($this->validator) {
  62. return $this->validator->call($this, $input);
  63. }
  64. if (! is_string($this->column)) {
  65. return false;
  66. }
  67. $rules = $attributes = [];
  68. if (
  69. (! $fieldRules = $this->getRules())
  70. && ! $this->max
  71. && ! $this->min
  72. ) {
  73. return false;
  74. }
  75. if (! Arr::has($input, $this->column)) {
  76. return false;
  77. }
  78. if ($fieldRules) {
  79. $rules["{$this->column}.values.*"] = $fieldRules;
  80. }
  81. $attributes["{$this->column}.values.*"] = __('Value');
  82. $rules["{$this->column}.values"][] = 'array';
  83. if (! is_null($this->max)) {
  84. $rules["{$this->column}.values"][] = "max:$this->max";
  85. }
  86. if (! is_null($this->min)) {
  87. $rules["{$this->column}.values"][] = "min:$this->min";
  88. }
  89. $attributes["{$this->column}.values"] = $this->label;
  90. $input = $this->prepareValidatorInput($input);
  91. return validator($input, $rules, $this->getValidationMessages(), $attributes);
  92. }
  93. public function formatValidatorMessages($messageBag)
  94. {
  95. $messages = new MessageBag();
  96. foreach ($messageBag->toArray() as $column => $message) {
  97. $messages->add($this->column, $message);
  98. }
  99. return $messages;
  100. }
  101. protected function prepareValidatorInput(array $input)
  102. {
  103. Arr::forget($input, "{$this->column}.values.".static::DEFAULT_FLAG_NAME);
  104. return $input;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. protected function prepareInputValue($value)
  110. {
  111. unset($value['values'][static::DEFAULT_FLAG_NAME]);
  112. if (empty($value['values'])) {
  113. return [];
  114. }
  115. return array_values($value['values']);
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function render()
  121. {
  122. $value = $this->value();
  123. $this->addVariables(['count' => $value ? count($value) : 0]);
  124. return parent::render();
  125. }
  126. }