Skip to content

Commit b01ef09

Browse files
authored
field (css class) defaults+ (#731)
1 parent 0dbb803 commit b01ef09

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

src/Kris/LaravelFormBuilder/Fields/FormField.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ protected function normalizeRules($rules)
406406
*
407407
* @param array $first first set of rules
408408
* @param array $second second set of rules
409-
* @return array merged set of rules without duplicates
409+
* @return array merged set of rules without duplicates
410410
*/
411411
protected function mergeRules($first, $second)
412412
{
@@ -672,12 +672,16 @@ protected function addErrorClass()
672672
*/
673673
protected function setDefaultOptions(array $options = [])
674674
{
675+
// Get default defaults from config (eg. defaults.field_class)
675676
$this->options = $this->formHelper->mergeOptions($this->allDefaults(), $this->getDefaults());
676-
$this->options = $this->prepareOptions($options);
677677

678+
// Maybe overwrite with field type defaults from config (eg. defaults.checkbox.field_class)
678679
$defaults = $this->setDefaultClasses($options);
679680
$this->options = $this->formHelper->mergeOptions($this->options, $defaults);
680681

682+
// Add specific field classes (eg. attr.class_append)
683+
$this->options = $this->prepareOptions($options);
684+
681685
$this->setupLabel();
682686
}
683687

tests/Fields/CheckableTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function it_creates_checkbox_field(): void
2121

2222
$expectedOptions = $this->getDefaults(
2323
[
24-
'class' => null,
24+
'class' => 'custom-checkbox-field-class',
2525
'required' => 'required',
2626
'id' => 'test',
2727
],

tests/Fields/FormFieldTest.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

33
use Illuminate\Http\Request;
4-
use Kris\LaravelFormBuilder\FormHelper;
4+
use Kris\LaravelFormBuilder\Fields\CheckableType;
55
use Kris\LaravelFormBuilder\Fields\InputType;
6+
use Kris\LaravelFormBuilder\FormHelper;
67

78
class FormFieldTest extends FormBuilderTestCase
89
{
@@ -117,7 +118,7 @@ public function it_appends_to_the_class_attribute_of_the_field()
117118
$text = new InputType('field_name', 'text', $this->plainForm, $options);
118119
$renderResult = $text->render();
119120

120-
$this->assertMatchesRegularExpression('/appended/', $text->getOption('attr.class'));
121+
$this->assertMatchesRegularExpression('/\bappended\b/', $text->getOption('attr.class'));
121122

122123
$defaultClasses = $this->config['defaults']['field_class'];
123124
$this->assertEquals('form-control appended', $text->getOption('attr.class'));
@@ -126,6 +127,26 @@ public function it_appends_to_the_class_attribute_of_the_field()
126127
$this->assertStringNotContainsString('class_append', $renderResult);
127128
}
128129

130+
/** @test */
131+
public function it_appends_to_the_class_attribute_of_a_custom_classes_checkbox_field()
132+
{
133+
$options = [
134+
'attr' => [
135+
'class_append' => 'appended',
136+
],
137+
];
138+
139+
$text = new CheckableType('field_name', 'checkbox', $this->plainForm, $options);
140+
$renderResult = $text->render();
141+
142+
$this->assertMatchesRegularExpression('/\bappended\b/', $text->getOption('attr.class'));
143+
144+
$this->assertEquals('custom-checkbox-field-class appended', $text->getOption('attr.class'));
145+
146+
$defaultClasses = $this->config['defaults']['field_class'];
147+
$this->assertStringNotContainsString($defaultClasses, $text->getOption('attr.class'));
148+
}
149+
129150
/** @test */
130151
public function it_appends_to_the_class_attribute_of_the_label()
131152
{
@@ -138,7 +159,7 @@ public function it_appends_to_the_class_attribute_of_the_label()
138159
$text = new InputType('field_name', 'text', $this->plainForm, $options);
139160
$renderResult = $text->render();
140161

141-
$this->assertMatchesRegularExpression('/appended/', $text->getOption('label_attr.class'));
162+
$this->assertMatchesRegularExpression('/\bappended\b/', $text->getOption('label_attr.class'));
142163

143164
$defaultClasses = $this->config['defaults']['label_class'];
144165
$this->assertEquals('control-label appended', $text->getOption('label_attr.class'));
@@ -159,7 +180,7 @@ public function it_appends_to_the_class_attribute_of_the_wrapper()
159180
$text = new InputType('field_name', 'text', $this->plainForm, $options);
160181
$renderResult = $text->render();
161182

162-
$this->assertMatchesRegularExpression('/appended/', $text->getOption('wrapper.class'));
183+
$this->assertMatchesRegularExpression('/\bappended\b/', $text->getOption('wrapper.class'));
163184

164185
$defaultClasses = $this->config['defaults']['wrapper_class'];
165186
$this->assertEquals('form-group appended', $text->getOption('wrapper.class'));

tests/FormBuilderTestCase.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ public function setUp(): void
199199
$this->validatorFactory = $this->app['validator'];
200200
$this->eventDispatcher = $this->app['events'];
201201
$this->model = new TestModel();
202-
$this->config = include __DIR__.'/../src/config/config.php';
202+
203+
$config = include __DIR__.'/../src/config/config.php';
204+
$config['defaults']['checkbox']['field_class'] = 'custom-checkbox-field-class';
205+
$this->config = $config;
203206

204207
$this->formHelper = new FormHelper($this->view, $this->translator, $this->config);
205208
$this->formBuilder = new FormBuilder($this->app, $this->formHelper, $this->eventDispatcher);

tests/FormTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,8 +1230,8 @@ public function it_add_option_attributes_properly()
12301230
$this->assertStringContainsString('<option value="zh" disabled="disabled">', $formView);
12311231
$this->assertStringNotContainsString('<option value="en" disabled="disabled">', $formView);
12321232
$this->assertStringNotContainsString('<option value="fr" disabled="disabled">', $formView);
1233-
$this->assertStringContainsString('<input id="languages_choice_checkbox_zh" disabled="disabled" name="languages_choice_checkbox[]" type="checkbox" value="zh">', $formView);
1234-
$this->assertStringNotContainsString('<input id="languages_choice_checkbox_en" disabled="disabled" name="languages_choice_checkbox[]" type="checkbox" value="en">', $formView);
1233+
$this->assertStringContainsString('<input class="custom-checkbox-field-class" id="languages_choice_checkbox_zh" disabled="disabled" name="languages_choice_checkbox[]" type="checkbox" value="zh">', $formView);
1234+
$this->assertStringNotContainsString('<input class="custom-checkbox-field-class" id="languages_choice_checkbox_en" disabled="disabled" name="languages_choice_checkbox[]" type="checkbox" value="en">', $formView);
12351235
$this->assertStringContainsString('<input id="languages_choice_radio_zh" disabled="disabled" name="languages_choice_radio" type="radio" value="zh">', $formView);
12361236
$this->assertStringNotContainsString('<input id="languages_choice_radio_en" disabled="disabled" name="languages_choice_radio" type="radio" value="en">', $formView);
12371237
}

0 commit comments

Comments
 (0)