ListField.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. return Helper::array($this->getValueFromData($data, null, $this->value));
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getValidator(array $input)
  58. {
  59. if ($this->validator) {
  60. return $this->validator->call($this, $input);
  61. }
  62. if (! is_string($this->column)) {
  63. return false;
  64. }
  65. $rules = $attributes = [];
  66. if (
  67. (! $fieldRules = $this->getRules())
  68. && ! $this->max
  69. && ! $this->min
  70. ) {
  71. return false;
  72. }
  73. if (! Arr::has($input, $this->column)) {
  74. return false;
  75. }
  76. if ($fieldRules) {
  77. $rules["{$this->column}.values.*"] = $fieldRules;
  78. }
  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. $input = $this->prepareValidatorInput($input);
  89. return validator($input, $rules, $this->getValidationMessages(), $attributes);
  90. }
  91. public function formatValidatorMessages($messageBag)
  92. {
  93. $messages = new MessageBag();
  94. foreach ($messageBag->toArray() as $column => $message) {
  95. $messages->add($this->column, $message);
  96. }
  97. return $messages;
  98. }
  99. protected function prepareValidatorInput(array $input)
  100. {
  101. Arr::forget($input, "{$this->column}.values.".static::DEFAULT_FLAG_NAME);
  102. return $input;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. protected function prepareInputValue($value)
  108. {
  109. unset($value['values'][static::DEFAULT_FLAG_NAME]);
  110. if (empty($value['values'])) {
  111. return [];
  112. }
  113. return array_values($value['values']);
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function render()
  119. {
  120. $value = $this->value();
  121. $this->addVariables(['count' => $value ? count($value) : 0]);
  122. return parent::render();
  123. }
  124. }