1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kris\LaravelFormBuilder\Fields; |
4
|
|
|
|
5
|
|
|
use Kris\LaravelFormBuilder\Form; |
6
|
|
|
|
7
|
|
|
class ChildFormType extends ParentType |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var Form |
12
|
|
|
*/ |
13
|
|
|
protected $form; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @inheritdoc |
17
|
|
|
*/ |
18
|
21 |
|
protected function getTemplate() |
19
|
|
|
{ |
20
|
21 |
|
return 'child_form'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return Form |
25
|
|
|
*/ |
26
|
2 |
|
public function getForm() |
27
|
|
|
{ |
28
|
2 |
|
return $this->form; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
21 |
|
protected function getDefaults() |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
21 |
|
'class' => null, |
38
|
21 |
|
'value' => null, |
39
|
21 |
|
'formOptions' => [], |
40
|
21 |
|
'data' => [], |
41
|
21 |
|
'exclude' => [] |
42
|
21 |
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
2 |
|
public function getAllAttributes() |
49
|
|
|
{ |
50
|
|
|
// Collect all children's attributes. |
51
|
2 |
|
return $this->parent->getFormHelper()->mergeAttributes($this->children); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Allow form-specific value alters. |
56
|
|
|
* |
57
|
|
|
* @param array $values |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
2 |
|
public function alterFieldValues(array &$values) |
61
|
|
|
{ |
62
|
2 |
|
$this->parent->getFormHelper()->alterFieldValues($this->form, $values); |
63
|
2 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Allow form-specific valid alters. |
67
|
|
|
* |
68
|
|
|
* @param Form $mainForm |
69
|
|
|
* @param bool $isValid |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
2 |
|
public function alterValid(Form $mainForm, &$isValid) |
73
|
|
|
{ |
74
|
2 |
|
$this->parent->getFormHelper()->alterValid($this->form, $mainForm, $isValid); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return mixed|void |
79
|
|
|
*/ |
80
|
21 |
|
protected function createChildren() |
81
|
|
|
{ |
82
|
21 |
|
$this->form = $this->getClassFromOptions(); |
83
|
|
|
|
84
|
18 |
|
if ($this->form->getFormOption('files')) { |
85
|
1 |
|
$this->parent->setFormOption('files', true); |
86
|
1 |
|
} |
87
|
18 |
|
$model = $this->getOption($this->valueProperty); |
88
|
18 |
|
if ($this->isValidValue($model)) { |
89
|
5 |
|
foreach ($this->form->getFields() as $name => $field) { |
90
|
4 |
|
$field->setValue($this->getModelValueAttribute($model, $name)); |
91
|
5 |
|
} |
92
|
5 |
|
} |
93
|
|
|
|
94
|
18 |
|
$this->children = $this->form->getFields(); |
95
|
18 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return Form |
99
|
|
|
* @throws \Exception |
100
|
|
|
*/ |
101
|
21 |
|
protected function getClassFromOptions() |
102
|
|
|
{ |
103
|
21 |
|
if ($this->form instanceof Form) { |
104
|
|
|
return $this->form->setName($this->name); |
105
|
|
|
} |
106
|
|
|
|
107
|
21 |
|
$class = $this->getOption('class'); |
108
|
|
|
|
109
|
21 |
|
if (!$class) { |
110
|
1 |
|
throw new \InvalidArgumentException( |
111
|
|
|
'Please provide full name or instance of Form class.' |
112
|
1 |
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
20 |
|
if (is_string($class)) { |
116
|
|
|
$options = [ |
117
|
9 |
|
'model' => $this->getOption($this->valueProperty) ?: $this->parent->getModel(), |
118
|
9 |
|
'name' => $this->name, |
119
|
9 |
|
'language_name' => $this->parent->getLanguageName(), |
120
|
9 |
|
'translation_template' => $this->parent->getTranslationTemplate(), |
121
|
9 |
|
]; |
122
|
|
|
|
123
|
9 |
|
if (!$this->parent->clientValidationEnabled()) { |
124
|
1 |
|
$options['client_validation'] = false; |
125
|
1 |
|
} |
126
|
|
|
|
127
|
9 |
|
if (!$this->parent->haveErrorsEnabled()) { |
128
|
1 |
|
$options['errors_enabled'] = false; |
129
|
1 |
|
} |
130
|
|
|
|
131
|
9 |
|
$formOptions = array_merge($options, $this->getOption('formOptions')); |
132
|
|
|
|
133
|
9 |
|
$data = array_merge($this->parent->getData(), $this->getOption('data')); |
134
|
|
|
|
135
|
9 |
|
return $this->parent->getFormBuilder()->create($class, $formOptions, $data); |
136
|
|
|
} |
137
|
|
|
|
138
|
11 |
|
if ($class instanceof Form) { |
139
|
10 |
|
$class->setName($this->name, false); |
140
|
10 |
|
$class->setModel($class->getModel() ?: $this->parent->getModel()); |
|
|
|
|
141
|
|
|
|
142
|
10 |
|
if (!$class->getData()) { |
143
|
10 |
|
$class->addData($this->parent->getData()); |
|
|
|
|
144
|
10 |
|
} |
145
|
|
|
|
146
|
10 |
|
if (!$class->getLanguageName()) { |
147
|
9 |
|
$class->setLanguageName($this->parent->getLanguageName()); |
148
|
9 |
|
} |
149
|
|
|
|
150
|
10 |
|
if (!$class->getTranslationTemplate()) { |
151
|
9 |
|
$class->setTranslationTemplate($this->parent->getTranslationTemplate()); |
152
|
9 |
|
} |
153
|
|
|
|
154
|
10 |
|
if (!$this->parent->clientValidationEnabled()) { |
155
|
|
|
$class->setClientValidationEnabled(false); |
156
|
|
|
} |
157
|
|
|
|
158
|
10 |
|
if (!$this->parent->haveErrorsEnabled()) { |
159
|
|
|
$class->setErrorsEnabled(false); |
160
|
|
|
} |
161
|
|
|
|
162
|
10 |
|
return $class->setName($this->name); |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
throw new \InvalidArgumentException( |
166
|
|
|
'Class provided does not exist or it passed in wrong format.' |
167
|
1 |
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritdoc |
172
|
|
|
*/ |
173
|
1 |
|
public function removeChild($key) |
174
|
|
|
{ |
175
|
1 |
|
if ($this->getChild($key)) { |
176
|
1 |
|
$this->form->remove($key); |
177
|
1 |
|
return parent::removeChild($key); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @inheritdoc |
185
|
|
|
*/ |
186
|
3 |
|
protected function getRenderData() { |
187
|
3 |
|
$data = parent::getRenderData(); |
188
|
3 |
|
$data['child_form'] = $this->form; |
189
|
3 |
|
return $data; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param $method |
194
|
|
|
* @param $arguments |
195
|
|
|
* |
196
|
|
|
* @return Form|null |
197
|
|
|
*/ |
198
|
5 |
|
public function __call($method, $arguments) |
199
|
|
|
{ |
200
|
5 |
|
if (method_exists($this->form, $method)) { |
201
|
5 |
|
return call_user_func_array([$this->form, $method], $arguments); |
202
|
|
|
} |
203
|
|
|
|
204
|
1 |
|
throw new \BadMethodCallException( |
205
|
1 |
|
'Method ['.$method.'] does not exist on form ['.get_class($this->form).']' |
206
|
1 |
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Check if provided value is valid for this type. |
211
|
|
|
* |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
21 |
|
protected function isValidValue($value) |
215
|
|
|
{ |
216
|
21 |
|
return $value !== null; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.