diff --git a/src/Kris/LaravelFormBuilder/Fields/FormField.php b/src/Kris/LaravelFormBuilder/Fields/FormField.php index b5032140..056416bf 100644 --- a/src/Kris/LaravelFormBuilder/Fields/FormField.php +++ b/src/Kris/LaravelFormBuilder/Fields/FormField.php @@ -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) ); } diff --git a/src/Kris/LaravelFormBuilder/FormHelper.php b/src/Kris/LaravelFormBuilder/FormHelper.php index c3107dd6..4b65af10 100644 --- a/src/Kris/LaravelFormBuilder/FormHelper.php +++ b/src/Kris/LaravelFormBuilder/FormHelper.php @@ -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; @@ -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) . '" '; } } diff --git a/tests/Fields/CollectionTypeTest.php b/tests/Fields/CollectionTypeTest.php index 15c59983..2e2a6760 100644 --- a/tests/Fields/CollectionTypeTest.php +++ b/tests/Fields/CollectionTypeTest.php @@ -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() { @@ -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"; + }, + ], + ], + ]); + } + } }