Skip to content

Commit

Permalink
ParentField disable/enable fixed (#705)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Eloar authored Apr 17, 2023
1 parent b9ee07c commit f714e49
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 9 deletions.
24 changes: 19 additions & 5 deletions src/Kris/LaravelFormBuilder/Fields/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Kris\LaravelFormBuilder\Fields;

use Illuminate\Support\Arr;

class ChoiceType extends ParentType
{
/**
Expand Down Expand Up @@ -32,7 +34,7 @@ protected function determineChoiceField()
$expanded = $this->options['expanded'];
$multiple = $this->options['multiple'];

if ($multiple) {
if (!$expanded && $multiple) {
$this->options['attr']['multiple'] = true;
}

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

$attr = $this->options['attr']?? [];
$attr = Arr::except($attr, ['class', 'multiple', 'id', 'name']);
foreach ((array)$this->options['choices'] as $key => $choice) {
$id = str_replace('.', '_', $this->getNameKey()) . '_' . $key;
$options = $this->formHelper->mergeOptions(
$this->getOption('choice_options'),
[
'attr' => array_merge(['id' => $id], $this->options['option_attributes'][$key] ?? []),
'attr' => array_merge(['id' => $id], $this->options['option_attributes'][$key] ?? $attr),
'label_attr' => ['for' => $id],
'label' => $choice,
'checked' => in_array($key, (array)$this->options[$this->valueProperty]),
Expand Down Expand Up @@ -148,15 +152,25 @@ protected function setDefaultClasses(array $options = [])
{
$defaults = parent::setDefaultClasses($options);
$choice_type = $this->determineChoiceField();
Arr::forget($defaults, 'attr.class');

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

$choice_wrapper_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.wrapper_class', '');
$choice_label_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.label_class', '');
$choice_field_class = $this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.field_class', '');
$choice_wrapper_class = $this->formHelper->getConfig(
'defaults.' . $this->type . '.choice_options.wrapper_class',
$this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.' . $choice_type . '.wrapper_class', '')
);
$choice_label_class = $this->formHelper->getConfig(
'defaults.' . $this->type . '.choice_options.label_class',
$this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.' . $choice_type . '.label_class', '')
);
$choice_field_class = $this->formHelper->getConfig(
'defaults.' . $this->type . '.choice_options.field_class',
$this->formHelper->getConfig('defaults.' . $this->type . '.choice_options.' . $choice_type . '.field_class', '')
);

if ($choice_wrapper_class) {
$defaults['choice_options']['wrapper']['class'] = $choice_wrapper_class;
Expand Down
2 changes: 1 addition & 1 deletion src/Kris/LaravelFormBuilder/Fields/CollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ protected function setupChild(FormField $field, $name, $value = null)

$firstFieldOptions = $this->formHelper->mergeOptions(
$this->getOption('options'),
['attr' => ['id' => $newFieldName]]
['attr' => array_merge(['id' => $newFieldName], $this->getOption('attr'))]
);

$field->setName($newFieldName);
Expand Down
4 changes: 4 additions & 0 deletions src/Kris/LaravelFormBuilder/Fields/ParentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,23 @@ public function __clone()
*/
public function disable()
{
parent::disable();
foreach ($this->children as $field) {
$field->disable();
}
return $this;
}

/**
* @inheritdoc
*/
public function enable()
{
parent::enable();
foreach ($this->children as $field) {
$field->enable();
}
return $this;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,23 @@
// 'wrapper' => ['class' => 'form-radio'],
// 'label' => ['class' => 'form-radio-label'],
// 'field' => ['class' => 'form-radio-field'],
// ]
//],
//
// 'choice' => [
// 'choice_options' => [
// 'wrapper_class' => 'choice-wrapper-class',
// 'label_class' => 'choice-label-class',
// 'field_class' => 'choice-field-class'
//
// # For choice type you may overwrite default choice options for each variant (checkbox, radio or select)
// 'checkbox' => [
// 'wrapper_class' => 'choice-checkbox-wrapper-class',
// 'label_class' => 'choice-checkbox-label-class',
// 'field_class' => 'choice-checkbox-field-class',
// ]
// ]
//]
],
// Templates
'form' => 'laravel-form-builder::form',
Expand Down
181 changes: 181 additions & 0 deletions tests/Fields/ChoiceTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,185 @@ public function it_can_override_choices()

$this->assertEquals('test', $choice->getOption('selected'));
}

/** @test */
public function it_disables_select()
{
$options = [
'attr' => ['class' => 'choice-class'],
'choices' => ['yes' => 'Yes', 'no' => 'No'],
'selected' => 'yes'
];
$field = new ChoiceType('some_choice', 'choice', $this->plainForm, $options);
$children = $field->getChildren();

// there shall be no 'disabled' attribute set beforehand
$this->assertArrayNotHasKey('disabled', $field->getOption('attr'));
foreach ($children as $child) {
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
}

$field->disable();

// there shall be 'disabled' attribute set after
$this->assertArrayHasKey('disabled', $field->getOption('attr'));
$this->assertEquals('disabled', $field->getOption('attr')['disabled']);
foreach ($children as $child) {
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
}
}

/** @test */
public function it_disables_checkbox_list()
{
$options = [
'attr' => ['class' => 'choice-class-something'],
'choices' => [1 => 'monday', 2 => 'tuesday'],
'selected' => 'tuesday',
'multiple' => true,
'expanded' => true
];

$field = new ChoiceType('some_choice', 'choice', $this->plainForm, $options);
$children = $field->getChildren();

// there shall be no 'disabled' attribute set beforehand
$this->assertArrayNotHasKey('disabled', $field->getOption('attr'));
foreach ($children as $child) {
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
}

$field->disable();

// there shall be 'disabled' attribute set after
$this->assertArrayHasKey('disabled', $field->getOption('attr'));
$this->assertEquals('disabled', $field->getOption('attr')['disabled']);
foreach ($children as $child) {
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
}
}

/** @test */
public function it_disables_radios_list()
{
$options = [
'attr' => ['class' => 'choice-class-something'],
'choices' => [1 => 'yes', 2 => 'no'],
'selected' => 'no',
'expanded' => true
];

$field = new ChoiceType('some_choice', 'choice', $this->plainForm, $options);
$children = $field->getChildren();

// there shall be no 'disabled' attribute set beforehand
$this->assertArrayNotHasKey('disabled', $field->getOption('attr'));
foreach ($children as $child) {
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
}

$field->disable();

// there shall be 'disabled' attribute set after
$this->assertArrayHasKey('disabled', $field->getOption('attr'));
$this->assertEquals('disabled', $field->getOption('attr')['disabled']);
foreach ($children as $child) {
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
}
}

/** @test */
public function it_enables_select()
{
$options = [
'attr' => ['class' => 'choice-class', 'disabled' => 'disabled'],
'choices' => ['yes' => 'Yes', 'no' => 'No'],
'selected' => 'yes'
];
$field = new ChoiceType('some_choice', 'choice', $this->plainForm, $options);
$children = $field->getChildren();

// there shall be 'disabled' attribute set beforehand
$this->assertArrayHasKey('disabled', $field->getOption('attr'));
$this->assertEquals('disabled', $field->getOption('attr')['disabled']);
foreach ($children as $child) {
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
}


$field->enable();

// there shall be no 'disabled' attribute set after
$this->assertArrayNotHasKey('disabled', $field->getOption('attr'));
foreach ($children as $child) {
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
}
}

/** @test */
public function it_enables_checkbox_list()
{
$options = [
'attr' => ['class' => 'choice-class-something', 'disabled' => 'disabled'],
'choices' => [1 => 'monday', 2 => 'tuesday'],
'selected' => 'tuesday',
'multiple' => true,
'expanded' => true
];

$field = new ChoiceType('some_choice', 'choice', $this->plainForm, $options);
$children = $field->getChildren();

// there shall be 'disabled' attribute set beforehand
$this->assertArrayHasKey('disabled', $field->getOption('attr'));
$this->assertEquals('disabled', $field->getOption('attr')['disabled']);
foreach ($children as $child) {
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
}


$field->enable();

// there shall be no 'disabled' attribute set after
$this->assertArrayNotHasKey('disabled', $field->getOption('attr'));
foreach ($children as $child) {
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
}
}

/** @test */
public function it_enables_radios_list()
{
$options = [
'attr' => ['class' => 'choice-class-something', 'disabled' => 'disabled'],
'choices' => [1 => 'yes', 2 => 'no'],
'selected' => 'no',
'expanded' => true
];

$field = new ChoiceType('some_choice', 'choice', $this->plainForm, $options);
$children = $field->getChildren();

// there shall be 'disabled' attribute set beforehand
$this->assertArrayHasKey('disabled', $field->getOption('attr'));
$this->assertEquals('disabled', $field->getOption('attr')['disabled']);
foreach ($children as $child) {
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
}


$field->enable();

// there shall be no 'disabled' attribute set after
$this->assertArrayNotHasKey('disabled', $field->getOption('attr'));
foreach ($children as $child) {
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
}
}
}
49 changes: 49 additions & 0 deletions tests/Fields/CollectionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,55 @@ public function it_uses_empty_model_for_new_collection_children_after_validation
}
}

/** @test */
public function it_disables()
{
$options = [
'type' => 'text'
];
$emailsCollection = new CollectionType('emails', 'collection', $this->plainForm, $options);
$children = $emailsCollection->getChildren();

$this->assertArrayNotHasKey('disabled', $emailsCollection->getOption('attr'));
foreach ($children as $child) {
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
}

$emailsCollection->disable();

$this->assertArrayHasKey('disabled', $emailsCollection->getOption('attr'));
$this->assertEquals('disabled', $emailsCollection->getOption('attr')['disabled']);
foreach ($children as $child) {
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
}
}

/** @test */
public function it_enables()
{
$options = [
'type' => 'text',
'attr' => ['disabled' => 'disabled'],
];
$collection = new CollectionType('emails', 'collection', $this->plainForm, $options);
$children = $collection->getChildren();

$this->assertArrayHasKey('disabled', $collection->getOption('attr'));
$this->assertEquals('disabled', $collection->getOption('attr')['disabled']);
foreach ($children as $child) {
$this->assertArrayHasKey('disabled', $child->getOption('attr'));
$this->assertEquals('disabled', $child->getOption('attr')['disabled']);
}

$collection->enable();

$this->assertArrayNotHasKey('disabled', $collection->getOption('attr'));
foreach ($children as $child) {
$this->assertArrayNotHasKey('disabled', $child->getOption('attr'));
}
}

}

class DummyEloquentModel extends Model {
Expand Down
Loading

0 comments on commit f714e49

Please sign in to comment.