kristijanhusak / laravel-form-builder

Laravel Form builder for version 5+!
https://packagist.org/packages/kris/laravel-form-builder
MIT License
1.69k stars 295 forks source link

Collection of Plain Forms selects wrong items #278

Open rudiedirkx opened 8 years ago

rudiedirkx commented 8 years ago
  1. A collection of 3 models
  2. A form with a 'collection' type, where 'data' is the collection of models
  3. A plain form in that collection type, with 2 fields
  4. The 1st model will be used for the plain form all 3 times

Snippets:

The collection:

$collection = new ModelCollection([
    new Translation(['from' => 'Nederlands', 'to' => 'Dutch']),
    new Translation(['from' => 'Engels', 'to' => 'English']),
    new Translation(['from' => 'Ja', 'to' => 'Yes']),
]);

The form:

class TranslationsForm extends Form {
    public function buildForm() {
        $this->add('translations3', 'collection', [
            'type' => 'form',
            'data' => $this->data['collection'],
            'options' => [
                'class' => $this->formBuilder->plain()
                    ->add('from', 'text', ['label' => 'From'])
                    ->add('to', 'text', ['label' => 'To']),
                'label' => 'Translation',
            ],
        ]);
    }
}

Output:

Translation
From  [Nederlands]
To  [Dutch]

Translation
From  [Nederlands]
To  [Dutch]

Translation
From  [Nederlands]
To  [Dutch]

That's the 1st item being used 3 times. It has something to do with the plain form, because FQCN works:

$this->add('translations1', 'collection', [
    'type' => 'form',
    'data' => $this->data['collection'],
    'options' => [
        'class' => TranslationForm::class, // FQCN
        'label' => 'Translation',
    ],
]);

And so does a named form object:

$this->add('translations4', 'collection', [
    'type' => 'form',
    'data' => $this->data['collection'],
    'options' => [
        'class' => $this->formBuilder->create(TranslationForm::class), // 1 form object
        'label' => 'Translation',
    ],
]);
kristijanhusak commented 7 years ago

Could be issue with passing an instance as class instead of FQCN. Try initializing TranslationForm and than setting it as a class. It will probably do the same thing. I'll look into it, thanks.

rudiedirkx commented 7 years ago

That doesn't seem to be the problem. This works:

$formObject = $this->formBuilder->create(TranslationForm::class);
$this->add('translations4', 'collection', [
    'type' => 'form',
    'data' => $this->data['collection'],
    'options' => [
        'class' => $formObject,
        'label' => 'Translation',
    ],
]);

so it only fails for the plain form object. A named form works with both methods: FQCN and object.

The workaround is very easy luckily: always always use a dedicated class, even for tiny forms.

rudiedirkx commented 4 years ago

Still broken, but way to complex for my little brain. Work-around is super easy: don't use plain forms.