diff --git a/src/Kris/LaravelFormBuilder/Fields/FormField.php b/src/Kris/LaravelFormBuilder/Fields/FormField.php
index b5032140..2b32f79b 100644
--- a/src/Kris/LaravelFormBuilder/Fields/FormField.php
+++ b/src/Kris/LaravelFormBuilder/Fields/FormField.php
@@ -406,7 +406,7 @@ protected function normalizeRules($rules)
*
* @param array $first first set of rules
* @param array $second second set of rules
- * @return array merged set of rules without duplicates
+ * @return array merged set of rules without duplicates
*/
protected function mergeRules($first, $second)
{
@@ -672,12 +672,16 @@ protected function addErrorClass()
*/
protected function setDefaultOptions(array $options = [])
{
+ // Get default defaults from config (eg. defaults.field_class)
$this->options = $this->formHelper->mergeOptions($this->allDefaults(), $this->getDefaults());
- $this->options = $this->prepareOptions($options);
+ // Maybe overwrite with field type defaults from config (eg. defaults.checkbox.field_class)
$defaults = $this->setDefaultClasses($options);
$this->options = $this->formHelper->mergeOptions($this->options, $defaults);
+ // Add specific field classes (eg. attr.class_append)
+ $this->options = $this->prepareOptions($options);
+
$this->setupLabel();
}
diff --git a/tests/Fields/CheckableTypeTest.php b/tests/Fields/CheckableTypeTest.php
index 3a54156d..5d013062 100644
--- a/tests/Fields/CheckableTypeTest.php
+++ b/tests/Fields/CheckableTypeTest.php
@@ -21,7 +21,7 @@ public function it_creates_checkbox_field(): void
$expectedOptions = $this->getDefaults(
[
- 'class' => null,
+ 'class' => 'custom-checkbox-field-class',
'required' => 'required',
'id' => 'test',
],
diff --git a/tests/Fields/FormFieldTest.php b/tests/Fields/FormFieldTest.php
index 4a039cb0..a848a8eb 100644
--- a/tests/Fields/FormFieldTest.php
+++ b/tests/Fields/FormFieldTest.php
@@ -1,8 +1,9 @@
plainForm, $options);
$renderResult = $text->render();
- $this->assertMatchesRegularExpression('/appended/', $text->getOption('attr.class'));
+ $this->assertMatchesRegularExpression('/\bappended\b/', $text->getOption('attr.class'));
$defaultClasses = $this->config['defaults']['field_class'];
$this->assertEquals('form-control appended', $text->getOption('attr.class'));
@@ -126,6 +127,26 @@ public function it_appends_to_the_class_attribute_of_the_field()
$this->assertStringNotContainsString('class_append', $renderResult);
}
+ /** @test */
+ public function it_appends_to_the_class_attribute_of_a_custom_classes_checkbox_field()
+ {
+ $options = [
+ 'attr' => [
+ 'class_append' => 'appended',
+ ],
+ ];
+
+ $text = new CheckableType('field_name', 'checkbox', $this->plainForm, $options);
+ $renderResult = $text->render();
+
+ $this->assertMatchesRegularExpression('/\bappended\b/', $text->getOption('attr.class'));
+
+ $this->assertEquals('custom-checkbox-field-class appended', $text->getOption('attr.class'));
+
+ $defaultClasses = $this->config['defaults']['field_class'];
+ $this->assertStringNotContainsString($defaultClasses, $text->getOption('attr.class'));
+ }
+
/** @test */
public function it_appends_to_the_class_attribute_of_the_label()
{
@@ -138,7 +159,7 @@ public function it_appends_to_the_class_attribute_of_the_label()
$text = new InputType('field_name', 'text', $this->plainForm, $options);
$renderResult = $text->render();
- $this->assertMatchesRegularExpression('/appended/', $text->getOption('label_attr.class'));
+ $this->assertMatchesRegularExpression('/\bappended\b/', $text->getOption('label_attr.class'));
$defaultClasses = $this->config['defaults']['label_class'];
$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()
$text = new InputType('field_name', 'text', $this->plainForm, $options);
$renderResult = $text->render();
- $this->assertMatchesRegularExpression('/appended/', $text->getOption('wrapper.class'));
+ $this->assertMatchesRegularExpression('/\bappended\b/', $text->getOption('wrapper.class'));
$defaultClasses = $this->config['defaults']['wrapper_class'];
$this->assertEquals('form-group appended', $text->getOption('wrapper.class'));
diff --git a/tests/FormBuilderTestCase.php b/tests/FormBuilderTestCase.php
index 84f27d5c..48c4bc80 100644
--- a/tests/FormBuilderTestCase.php
+++ b/tests/FormBuilderTestCase.php
@@ -199,7 +199,10 @@ public function setUp(): void
$this->validatorFactory = $this->app['validator'];
$this->eventDispatcher = $this->app['events'];
$this->model = new TestModel();
- $this->config = include __DIR__.'/../src/config/config.php';
+
+ $config = include __DIR__.'/../src/config/config.php';
+ $config['defaults']['checkbox']['field_class'] = 'custom-checkbox-field-class';
+ $this->config = $config;
$this->formHelper = new FormHelper($this->view, $this->translator, $this->config);
$this->formBuilder = new FormBuilder($this->app, $this->formHelper, $this->eventDispatcher);
diff --git a/tests/FormTest.php b/tests/FormTest.php
index 7a488e08..67487717 100644
--- a/tests/FormTest.php
+++ b/tests/FormTest.php
@@ -1230,8 +1230,8 @@ public function it_add_option_attributes_properly()
$this->assertStringContainsString('