|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form\Field; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
|
6
|
|
|
use Encore\Admin\Form\Field; |
|
7
|
|
|
use Illuminate\Support\Arr; |
|
8
|
|
|
|
|
9
|
|
|
class ListField extends Field |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Max list size. |
|
13
|
|
|
* |
|
14
|
|
|
* @var int |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $max; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Minimum list size. |
|
20
|
|
|
* |
|
21
|
|
|
* @var int |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $min = 0; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $value = ['']; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Set Max list size. |
|
32
|
|
|
* |
|
33
|
|
|
* @param int $size |
|
34
|
|
|
* |
|
35
|
|
|
* @return $this |
|
36
|
|
|
*/ |
|
37
|
|
|
public function max(int $size) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->max = $size; |
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Set Minimum list size. |
|
46
|
|
|
* |
|
47
|
|
|
* @param int $size |
|
48
|
|
|
* |
|
49
|
|
|
* @return $this |
|
50
|
|
|
*/ |
|
51
|
|
|
public function min(int $size) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->min = $size; |
|
54
|
|
|
|
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Fill data to the field. |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $data |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
public function fill($data) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
$this->data = $data; |
|
68
|
|
|
|
|
69
|
|
|
$this->value = Arr::get($data, $this->column, $this->value); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
$this->formatValue(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getValidator(array $input) |
|
78
|
|
|
{ |
|
79
|
|
|
if ($this->validator) { |
|
80
|
|
|
return $this->validator->call($this, $input); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (!is_string($this->column)) { |
|
84
|
|
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$rules = $attributes = []; |
|
88
|
|
|
|
|
89
|
|
|
if (!$fieldRules = $this->getRules()) { |
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (!Arr::has($input, $this->column)) { |
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$rules["{$this->column}.values.*"] = $fieldRules; |
|
98
|
|
|
$attributes["{$this->column}.values.*"] = __('Value'); |
|
99
|
|
|
|
|
100
|
|
|
$rules["{$this->column}.values"][] = 'array'; |
|
101
|
|
|
|
|
102
|
|
View Code Duplication |
if (!is_null($this->max)) { |
|
|
|
|
|
|
103
|
|
|
$rules["{$this->column}.values"][] = "max:$this->max"; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
View Code Duplication |
if (!is_null($this->min)) { |
|
|
|
|
|
|
107
|
|
|
$rules["{$this->column}.values"][] = "min:$this->min"; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$attributes["{$this->column}.values"] = $this->label; |
|
111
|
|
|
|
|
112
|
|
|
return validator($input, $rules, $this->getValidationMessages(), $attributes); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function setupScript() |
|
119
|
|
|
{ |
|
120
|
|
|
$this->script = <<<SCRIPT |
|
121
|
|
|
|
|
122
|
|
|
$('.{$this->column}-add').on('click', function () { |
|
123
|
|
|
var tpl = $('template.{$this->column}-tpl').html(); |
|
124
|
|
|
$('tbody.list-{$this->column}-table').append(tpl); |
|
125
|
|
|
}); |
|
126
|
|
|
|
|
127
|
|
|
$('tbody').on('click', '.{$this->column}-remove', function () { |
|
128
|
|
|
$(this).closest('tr').remove(); |
|
129
|
|
|
}); |
|
130
|
|
|
|
|
131
|
|
|
SCRIPT; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* {@inheritdoc} |
|
136
|
|
|
*/ |
|
137
|
|
|
public function prepare($value) |
|
138
|
|
|
{ |
|
139
|
|
|
return array_values($value['values']); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* {@inheritdoc} |
|
144
|
|
|
*/ |
|
145
|
|
|
public function render() |
|
146
|
|
|
{ |
|
147
|
|
|
$this->setupScript(); |
|
148
|
|
|
|
|
149
|
|
|
Admin::style('td .form-group {margin-bottom: 0 !important;}'); |
|
150
|
|
|
|
|
151
|
|
|
return parent::render(); |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.