Skip to content

Commit 4dc7374

Browse files
authored
Added ability to pass attributes to individual options in select/choices (#594)
* Added option_attributes to select element * Added test for option attributes
1 parent d086de5 commit 4dc7374

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

src/Kris/LaravelFormBuilder/Fields/ChoiceType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected function buildCheckableChildren($fieldType)
107107
$options = $this->formHelper->mergeOptions(
108108
$this->getOption('choice_options'),
109109
[
110-
'attr' => ['id' => $id],
110+
'attr' => array_merge(['id' => $id], $this->options['option_attributes'][$key] ?? []),
111111
'label_attr' => ['for' => $id],
112112
'label' => $choice,
113113
'checked' => in_array($key, (array)$this->options[$this->valueProperty]),

src/Kris/LaravelFormBuilder/Fields/SelectType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function getDefaults()
2727
{
2828
return [
2929
'choices' => [],
30+
'option_attributes' => [],
3031
'empty_value' => null,
3132
'selected' => null
3233
];

src/views/select.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<?php if ($showField): ?>
1212
<?php $emptyVal = $options['empty_value'] ? ['' => $options['empty_value']] : null; ?>
13-
<?= Form::select($name, (array)$emptyVal + $options['choices'], $options['selected'], $options['attr']) ?>
13+
<?= Form::select($name, (array)$emptyVal + $options['choices'], $options['selected'], $options['attr'], $options['option_attributes']) ?>
1414
<?php include helpBlockPath(); ?>
1515
<?php endif; ?>
1616

tests/Fields/SelectTypeTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ public function it_creates_select_field(): void
3939
$expectedOptions['choices'] = $choices;
4040
$expectedOptions['empty_value'] = 'Select your sex';
4141
$expectedOptions['selected'] = '1';
42+
$expectedOptions['option_attributes'] = [];
4243

4344
$select = new SelectType('sex', 'select', $this->plainForm, $options);
4445

4546
$select->render(['selected' => '1']);
4647

4748
$this->assertEquals($expectedOptions, $select->getOptions());
4849
}
49-
}
50+
}

tests/FormTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,4 +1178,55 @@ public function it_filter_and_mutate_fields_request_values()
11781178
$this->assertEquals('TEST', $this->request['test_field']);
11791179
}
11801180

1181+
/** @test */
1182+
public function it_add_option_attributes_properly()
1183+
{
1184+
$config = $this->config;
1185+
1186+
$formHelper = new FormHelper($this->view, $this->translator, $config);
1187+
$formBuilder = new FormBuilder($this->app, $formHelper, $this->eventDispatcher);
1188+
1189+
$choices = ['en' => 'English', 'fr' => 'French', 'zh' => 'Chinese'];
1190+
$optionAttributes = ['zh' => ['disabled' => 'disabled']];
1191+
1192+
$form = $formBuilder->plain()
1193+
->add('languages_select', 'select', [
1194+
'choices' => $choices,
1195+
'option_attributes' => $optionAttributes
1196+
])
1197+
->add('languages_choice_select', 'choice', [
1198+
'choices' => $choices,
1199+
'option_attributes' => $optionAttributes,
1200+
'expanded' => false,
1201+
'multiple' => false
1202+
])
1203+
->add('languages_choice_select_multiple', 'choice', [
1204+
'choices' => $choices,
1205+
'option_attributes' => $optionAttributes,
1206+
'expanded' => false,
1207+
'multiple' => true
1208+
])
1209+
->add('languages_choice_checkbox', 'choice', [
1210+
'choices' => $choices,
1211+
'option_attributes' => $optionAttributes,
1212+
'expanded' => true,
1213+
'multiple' => true
1214+
])
1215+
->add('languages_choice_radio', 'choice', [
1216+
'choices' => $choices,
1217+
'option_attributes' => $optionAttributes,
1218+
'expanded' => true,
1219+
'multiple' => false
1220+
]);
1221+
1222+
$formView = $form->renderForm();
1223+
1224+
$this->assertStringContainsString('<option value="zh" disabled="disabled">', $formView);
1225+
$this->assertStringNotContainsString('<option value="en" disabled="disabled">', $formView);
1226+
$this->assertStringNotContainsString('<option value="fr" disabled="disabled">', $formView);
1227+
$this->assertStringContainsString('<input id="languages_choice_checkbox_zh" disabled="disabled" name="languages_choice_checkbox[]" type="checkbox" value="zh">', $formView);
1228+
$this->assertStringNotContainsString('<input id="languages_choice_checkbox_en" disabled="disabled" name="languages_choice_checkbox[]" type="checkbox" value="en">', $formView);
1229+
$this->assertStringContainsString('<input id="languages_choice_radio_zh" disabled="disabled" name="languages_choice_radio" type="radio" value="zh">', $formView);
1230+
$this->assertStringNotContainsString('<input id="languages_choice_radio_en" disabled="disabled" name="languages_choice_radio" type="radio" value="en">', $formView);
1231+
}
11811232
}

0 commit comments

Comments
 (0)