Skip to content

Commit f714e49

Browse files
authored
ParentField disable/enable fixed (#705)
- Parent Field when disabled or enabled did only populate information to its children and forgot about it itself. - fixed some typing error - extended tests for disable/enable functionality - added `attr` population for some ParentType derivatives - added variant specific config (wrapper_class, label_class, field_class) for choice type
1 parent b9ee07c commit f714e49

File tree

7 files changed

+329
-9
lines changed

7 files changed

+329
-9
lines changed

src/Kris/LaravelFormBuilder/Fields/ChoiceType.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Kris\LaravelFormBuilder\Fields;
44

5+
use Illuminate\Support\Arr;
6+
57
class ChoiceType extends ParentType
68
{
79
/**
@@ -32,7 +34,7 @@ protected function determineChoiceField()
3234
$expanded = $this->options['expanded'];
3335
$multiple = $this->options['multiple'];
3436

35-
if ($multiple) {
37+
if (!$expanded && $multiple) {
3638
$this->options['attr']['multiple'] = true;
3739
}
3840

@@ -102,12 +104,14 @@ protected function buildCheckableChildren($fieldType)
102104
{
103105
$multiple = $this->getOption('multiple') ? '[]' : '';
104106

107+
$attr = $this->options['attr']?? [];
108+
$attr = Arr::except($attr, ['class', 'multiple', 'id', 'name']);
105109
foreach ((array)$this->options['choices'] as $key => $choice) {
106110
$id = str_replace('.', '_', $this->getNameKey()) . '_' . $key;
107111
$options = $this->formHelper->mergeOptions(
108112
$this->getOption('choice_options'),
109113
[
110-
'attr' => array_merge(['id' => $id], $this->options['option_attributes'][$key] ?? []),
114+
'attr' => array_merge(['id' => $id], $this->options['option_attributes'][$key] ?? $attr),
111115
'label_attr' => ['for' => $id],
112116
'label' => $choice,
113117
'checked' => in_array($key, (array)$this->options[$this->valueProperty]),
@@ -148,15 +152,25 @@ protected function setDefaultClasses(array $options = [])
148152
{
149153
$defaults = parent::setDefaultClasses($options);
150154
$choice_type = $this->determineChoiceField();
155+
Arr::forget($defaults, 'attr.class');
151156

152157
$wrapper_class = $this->formHelper->getConfig('defaults.' . $this->type . '.' . $choice_type . '_wrapper_class', '');
153158
if ($wrapper_class) {
154159
$defaults['wrapper']['class'] = (isset($defaults['wrapper']['class']) ? $defaults['wrapper']['class'] . ' ' : '') . $wrapper_class;
155160
}
156161

157-
$choice_wrapper_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.wrapper_class', '');
158-
$choice_label_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.label_class', '');
159-
$choice_field_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.field_class', '');
162+
$choice_wrapper_class = $this->formHelper->getConfig(
163+
'defaults.' . $this->type . '.choice_options.wrapper_class',
164+
$this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.' . $choice_type . '.wrapper_class', '')
165+
);
166+
$choice_label_class = $this->formHelper->getConfig(
167+
'defaults.' . $this->type . '.choice_options.label_class',
168+
$this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.' . $choice_type . '.label_class', '')
169+
);
170+
$choice_field_class = $this->formHelper->getConfig(
171+
'defaults.' . $this->type . '.choice_options.field_class',
172+
$this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.' . $choice_type . '.field_class', '')
173+
);
160174

161175
if ($choice_wrapper_class) {
162176
$defaults['choice_options']['wrapper']['class'] = $choice_wrapper_class;

src/Kris/LaravelFormBuilder/Fields/CollectionType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ protected function setupChild(FormField $field, $name, $value = null)
234234

235235
$firstFieldOptions = $this->formHelper->mergeOptions(
236236
$this->getOption('options'),
237-
['attr' => ['id' => $newFieldName]]
237+
['attr' => array_merge(['id' => $newFieldName], $this->getOption('attr'))]
238238
);
239239

240240
$field->setName($newFieldName);

src/Kris/LaravelFormBuilder/Fields/ParentType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,23 @@ public function __clone()
175175
*/
176176
public function disable()
177177
{
178+
parent::disable();
178179
foreach ($this->children as $field) {
179180
$field->disable();
180181
}
182+
return $this;
181183
}
182184

183185
/**
184186
* @inheritdoc
185187
*/
186188
public function enable()
187189
{
190+
parent::enable();
188191
foreach ($this->children as $field) {
189192
$field->enable();
190193
}
194+
return $this;
191195
}
192196

193197
/**

src/config/config.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,23 @@
2424
// 'wrapper' => ['class' => 'form-radio'],
2525
// 'label' => ['class' => 'form-radio-label'],
2626
// 'field' => ['class' => 'form-radio-field'],
27+
// ]
2728
//],
29+
//
30+
// 'choice' => [
31+
// 'choice_options' => [
32+
// 'wrapper_class' => 'choice-wrapper-class',
33+
// 'label_class' => 'choice-label-class',
34+
// 'field_class' => 'choice-field-class'
35+
//
36+
// # For choice type you may overwrite default choice options for each variant (checkbox, radio or select)
37+
// 'checkbox' => [
38+
// 'wrapper_class' => 'choice-checkbox-wrapper-class',
39+
// 'label_class' => 'choice-checkbox-label-class',
40+
// 'field_class' => 'choice-checkbox-field-class',
41+
// ]
42+
// ]
43+
//]
2844
],
2945
// Templates
3046
'form' => 'laravel-form-builder::form',

tests/Fields/ChoiceTypeTest.php

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,185 @@ public function it_can_override_choices()
106106

107107
$this->assertEquals('test', $choice->getOption('selected'));
108108
}
109+
110+
/** @test */
111+
public function it_disables_select()
112+
{
113+
$options = [
114+
'attr' => ['class' => 'choice-class'],
115+
'choices' => ['yes' => 'Yes', 'no' => 'No'],
116+
'selected' => 'yes'
117+
];
118+
$field = new ChoiceType('some_choice', 'choice', $this->plainForm, $options);
119+
$children = $field->getChildren();
120+
121+
// there shall be no 'disabled' attribute set beforehand
122+
$this->assertArrayNotHasKey('disabled', $field->getOption('attr'));
123+
foreach ($children as $child) {
124+
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
125+
}
126+
127+
$field->disable();
128+
129+
// there shall be 'disabled' attribute set after
130+
$this->assertArrayHasKey('disabled', $field->getOption('attr'));
131+
$this->assertEquals('disabled', $field->getOption('attr')['disabled']);
132+
foreach ($children as $child) {
133+
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
134+
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
135+
}
136+
}
137+
138+
/** @test */
139+
public function it_disables_checkbox_list()
140+
{
141+
$options = [
142+
'attr' => ['class' => 'choice-class-something'],
143+
'choices' => [1 => 'monday', 2 => 'tuesday'],
144+
'selected' => 'tuesday',
145+
'multiple' => true,
146+
'expanded' => true
147+
];
148+
149+
$field = new ChoiceType('some_choice', 'choice', $this->plainForm, $options);
150+
$children = $field->getChildren();
151+
152+
// there shall be no 'disabled' attribute set beforehand
153+
$this->assertArrayNotHasKey('disabled', $field->getOption('attr'));
154+
foreach ($children as $child) {
155+
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
156+
}
157+
158+
$field->disable();
159+
160+
// there shall be 'disabled' attribute set after
161+
$this->assertArrayHasKey('disabled', $field->getOption('attr'));
162+
$this->assertEquals('disabled', $field->getOption('attr')['disabled']);
163+
foreach ($children as $child) {
164+
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
165+
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
166+
}
167+
}
168+
169+
/** @test */
170+
public function it_disables_radios_list()
171+
{
172+
$options = [
173+
'attr' => ['class' => 'choice-class-something'],
174+
'choices' => [1 => 'yes', 2 => 'no'],
175+
'selected' => 'no',
176+
'expanded' => true
177+
];
178+
179+
$field = new ChoiceType('some_choice', 'choice', $this->plainForm, $options);
180+
$children = $field->getChildren();
181+
182+
// there shall be no 'disabled' attribute set beforehand
183+
$this->assertArrayNotHasKey('disabled', $field->getOption('attr'));
184+
foreach ($children as $child) {
185+
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
186+
}
187+
188+
$field->disable();
189+
190+
// there shall be 'disabled' attribute set after
191+
$this->assertArrayHasKey('disabled', $field->getOption('attr'));
192+
$this->assertEquals('disabled', $field->getOption('attr')['disabled']);
193+
foreach ($children as $child) {
194+
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
195+
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
196+
}
197+
}
198+
199+
/** @test */
200+
public function it_enables_select()
201+
{
202+
$options = [
203+
'attr' => ['class' => 'choice-class', 'disabled' => 'disabled'],
204+
'choices' => ['yes' => 'Yes', 'no' => 'No'],
205+
'selected' => 'yes'
206+
];
207+
$field = new ChoiceType('some_choice', 'choice', $this->plainForm, $options);
208+
$children = $field->getChildren();
209+
210+
// there shall be 'disabled' attribute set beforehand
211+
$this->assertArrayHasKey('disabled', $field->getOption('attr'));
212+
$this->assertEquals('disabled', $field->getOption('attr')['disabled']);
213+
foreach ($children as $child) {
214+
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
215+
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
216+
}
217+
218+
219+
$field->enable();
220+
221+
// there shall be no 'disabled' attribute set after
222+
$this->assertArrayNotHasKey('disabled', $field->getOption('attr'));
223+
foreach ($children as $child) {
224+
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
225+
}
226+
}
227+
228+
/** @test */
229+
public function it_enables_checkbox_list()
230+
{
231+
$options = [
232+
'attr' => ['class' => 'choice-class-something', 'disabled' => 'disabled'],
233+
'choices' => [1 => 'monday', 2 => 'tuesday'],
234+
'selected' => 'tuesday',
235+
'multiple' => true,
236+
'expanded' => true
237+
];
238+
239+
$field = new ChoiceType('some_choice', 'choice', $this->plainForm, $options);
240+
$children = $field->getChildren();
241+
242+
// there shall be 'disabled' attribute set beforehand
243+
$this->assertArrayHasKey('disabled', $field->getOption('attr'));
244+
$this->assertEquals('disabled', $field->getOption('attr')['disabled']);
245+
foreach ($children as $child) {
246+
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
247+
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
248+
}
249+
250+
251+
$field->enable();
252+
253+
// there shall be no 'disabled' attribute set after
254+
$this->assertArrayNotHasKey('disabled', $field->getOption('attr'));
255+
foreach ($children as $child) {
256+
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
257+
}
258+
}
259+
260+
/** @test */
261+
public function it_enables_radios_list()
262+
{
263+
$options = [
264+
'attr' => ['class' => 'choice-class-something', 'disabled' => 'disabled'],
265+
'choices' => [1 => 'yes', 2 => 'no'],
266+
'selected' => 'no',
267+
'expanded' => true
268+
];
269+
270+
$field = new ChoiceType('some_choice', 'choice', $this->plainForm, $options);
271+
$children = $field->getChildren();
272+
273+
// there shall be 'disabled' attribute set beforehand
274+
$this->assertArrayHasKey('disabled', $field->getOption('attr'));
275+
$this->assertEquals('disabled', $field->getOption('attr')['disabled']);
276+
foreach ($children as $child) {
277+
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
278+
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
279+
}
280+
281+
282+
$field->enable();
283+
284+
// there shall be no 'disabled' attribute set after
285+
$this->assertArrayNotHasKey('disabled', $field->getOption('attr'));
286+
foreach ($children as $child) {
287+
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
288+
}
289+
}
109290
}

tests/Fields/CollectionTypeTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,55 @@ public function it_uses_empty_model_for_new_collection_children_after_validation
297297
}
298298
}
299299

300+
/** @test */
301+
public function it_disables()
302+
{
303+
$options = [
304+
'type' => 'text'
305+
];
306+
$emailsCollection = new CollectionType('emails', 'collection', $this->plainForm, $options);
307+
$children = $emailsCollection->getChildren();
308+
309+
$this->assertArrayNotHasKey('disabled', $emailsCollection->getOption('attr'));
310+
foreach ($children as $child) {
311+
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
312+
}
313+
314+
$emailsCollection->disable();
315+
316+
$this->assertArrayHasKey('disabled', $emailsCollection->getOption('attr'));
317+
$this->assertEquals('disabled', $emailsCollection->getOption('attr')['disabled']);
318+
foreach ($children as $child) {
319+
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
320+
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
321+
}
322+
}
323+
324+
/** @test */
325+
public function it_enables()
326+
{
327+
$options = [
328+
'type' => 'text',
329+
'attr' => ['disabled' => 'disabled'],
330+
];
331+
$collection = new CollectionType('emails', 'collection', $this->plainForm, $options);
332+
$children = $collection->getChildren();
333+
334+
$this->assertArrayHasKey('disabled', $collection->getOption('attr'));
335+
$this->assertEquals('disabled', $collection->getOption('attr')['disabled']);
336+
foreach ($children as $child) {
337+
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
338+
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
339+
}
340+
341+
$collection->enable();
342+
343+
$this->assertArrayNotHasKey('disabled', $collection->getOption('attr'));
344+
foreach ($children as $child) {
345+
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
346+
}
347+
}
348+
300349
}
301350

302351
class DummyEloquentModel extends Model {

0 commit comments

Comments
 (0)