Skip to content

Commit

Permalink
field (css class) defaults+ (#731)
Browse files Browse the repository at this point in the history
  • Loading branch information
rudiedirkx authored Dec 1, 2024
1 parent 0dbb803 commit b01ef09
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/Kris/LaravelFormBuilder/Fields/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Fields/CheckableTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
Expand Down
29 changes: 25 additions & 4 deletions tests/Fields/FormFieldTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

use Illuminate\Http\Request;
use Kris\LaravelFormBuilder\FormHelper;
use Kris\LaravelFormBuilder\Fields\CheckableType;
use Kris\LaravelFormBuilder\Fields\InputType;
use Kris\LaravelFormBuilder\FormHelper;

class FormFieldTest extends FormBuilderTestCase
{
Expand Down Expand Up @@ -117,7 +118,7 @@ public function it_appends_to_the_class_attribute_of_the_field()
$text = new InputType('field_name', 'text', $this->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'));
Expand All @@ -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()
{
Expand All @@ -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'));
Expand All @@ -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'));
Expand Down
5 changes: 4 additions & 1 deletion tests/FormBuilderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1230,8 +1230,8 @@ public function it_add_option_attributes_properly()
$this->assertStringContainsString('<option value="zh" disabled="disabled">', $formView);
$this->assertStringNotContainsString('<option value="en" disabled="disabled">', $formView);
$this->assertStringNotContainsString('<option value="fr" disabled="disabled">', $formView);
$this->assertStringContainsString('<input id="languages_choice_checkbox_zh" disabled="disabled" name="languages_choice_checkbox[]" type="checkbox" value="zh">', $formView);
$this->assertStringNotContainsString('<input id="languages_choice_checkbox_en" disabled="disabled" name="languages_choice_checkbox[]" type="checkbox" value="en">', $formView);
$this->assertStringContainsString('<input class="custom-checkbox-field-class" id="languages_choice_checkbox_zh" disabled="disabled" name="languages_choice_checkbox[]" type="checkbox" value="zh">', $formView);
$this->assertStringNotContainsString('<input class="custom-checkbox-field-class" id="languages_choice_checkbox_en" disabled="disabled" name="languages_choice_checkbox[]" type="checkbox" value="en">', $formView);
$this->assertStringContainsString('<input id="languages_choice_radio_zh" disabled="disabled" name="languages_choice_radio" type="radio" value="zh">', $formView);
$this->assertStringNotContainsString('<input id="languages_choice_radio_en" disabled="disabled" name="languages_choice_radio" type="radio" value="en">', $formView);
}
Expand Down

0 comments on commit b01ef09

Please sign in to comment.