Skip to content

accept closure attributes (eg wrapper.class) #698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Kris/LaravelFormBuilder/Fields/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,13 @@ protected function prepareOptions(array $options = [])
}
}

$this->setOption('wrapperAttrs', $helper->prepareAttributes($this->getOption('wrapper')));
$this->setOption('errorAttrs', $helper->prepareAttributes($this->getOption('errors')));
$this->setOption('wrapperAttrs', $helper->prepareAttributes($this->getOption('wrapper'), $this));
$this->setOption('errorAttrs', $helper->prepareAttributes($this->getOption('errors'), $this));

if ($this->getOption('help_block.text')) {
$this->setOption(
'help_block.helpBlockAttrs',
$helper->prepareAttributes($this->getOption('help_block.attr'))
$helper->prepareAttributes($this->getOption('help_block.attr'), $this)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Kris/LaravelFormBuilder/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function getFieldType($type)
* @param $options
* @return string
*/
public function prepareAttributes($options)
public function prepareAttributes($options, ...$optionalArguments)
{
if (!$options) {
return null;
Expand All @@ -199,7 +199,7 @@ public function prepareAttributes($options)
foreach ($options as $name => $option) {
if ($option !== null) {
$name = is_numeric($name) ? $option : $name;
$attributes[] = $name . '="' . $option . '" ';
$attributes[] = $name . '="' . value($option, ...$optionalArguments) . '" ';
}
}

Expand Down
48 changes: 48 additions & 0 deletions tests/Fields/CollectionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,30 @@ public function it_creates_collection_with_child_form()
$this->assertEquals(2, count($childFormCollection->getChildren()));
}

/** @test */
public function it_can_assign_dynamic_classes_to_child_forms()
{
$items = new \Illuminate\Support\Collection([
(new DummyEloquentModel2())->forceFill(['id' => 1, 'foo' => 'a']),
(new DummyEloquentModel2())->forceFill(['id' => 2, 'foo' => 'b']),
(new DummyEloquentModel2())->forceFill(['id' => 3, 'foo' => 'c']),
]);

$model = (new DummyEloquentModel())->forceFill(['id' => 11]);
$model->setRelation('items', $items);

$form = $this->formBuilder->create('LaravelFormBuilderCollectionTypeTest\Forms\WithDynamicAttributesFormCollectionForm', [
'model' => $model,
]);

$html = $form->renderForm();

$timesAlwaysClass = count(explode('always-class', $html)) - 1;
$timesOnceClass = count(explode('once-class', $html)) - 1;
$this->assertEquals(3, $timesAlwaysClass);
$this->assertEquals(1, $timesOnceClass);
}

/** @test */
public function it_creates_collection_with_child_form_with_correct_model()
{
Expand Down Expand Up @@ -391,4 +415,28 @@ function buildForm()
]);
}
}

class WithDynamicAttributesFormCollectionForm extends Form
{
function buildForm()
{
$this->add('items', 'collection', [
'type' => 'form',
'prototype' => false,
'empty_row' => false,
'options' => [
'class' => NamespacedDummyFormCollectionChildForm::class,
'wrapper' => [
'class' => function(\Kris\LaravelFormBuilder\Fields\FormField $field) {
$form = $field->getForm();
$model = $form ? $form->getModel() : null;
$id = $model ? $model->id : 0;
$onceClass = $id == 2 ? 'once-class' : '';
return "always-class $onceClass";
},
],
],
]);
}
}
}