laravel-form-builder icon indicating copy to clipboard operation
laravel-form-builder copied to clipboard

Collection of child forms of child form binds the parent model

Open koichirose opened this issue 7 years ago • 0 comments

I have a SupplierForm and a SupplierTranslationForm. SupplierForm adds a collection of SupplierTranslationForm and if I dump $child values like this within my custom collection.php:

foreach ((array)$options['children'] as $k => $child) {
    $translation_model = $child->getValue();

I get a ChildFormType:

ChildFormType {#3190 ▼
  #form: SupplierTranslationForm {#3186 ▼
    #fields: array:5 []
    #model: SupplierTranslation {#611 }

This is correct.

I also have an AnswerForm with a collection of TextAnswerForm and TextAnswerForm adds a collection of TextAnswerTranslationForm (so it's parent->child->child instead of parent->child above)

This is the ChildFormType I get:

ChildFormType {#741 ▼
  #form: TextAnswerTranslationForm {#817 ▼
    #fields: array:3 []
    #model: TextAnswer {#796 }

As you can see, the model is TextAnswer (which is the parent) instead of TextAnswerTranslation.

Here's how I'm instantiating them:

//in AnswerForm:
            $this->add('text_answer', 'form', [
                'type' => 'form',
                'label' => false,
                'class' => $this->formBuilder->create(TextAnswerForm::class, ['model' => $question->supplierAnswers($supplier)]),
                'options' => [
                    'label' => false,
                    'wrapper' => false,
                ],
            ]);

//in TextAnswerForm:
        $this->add('translations', 'translation', [ //translation.php is a slightly modified collection.php
            'type' => 'form',
            'wrapper' => [
                'class' => 'translations'
            ],
            'label' => false,
            'options' => [
                'class' => $this->formBuilder->create(TextAnswerTranslationForm::class),
                'label' => false,
                'wrapper' => false,
            ],
        ]);

Is there any way to fix this? I'm pretty sure we talked about this in the past and it's been fixed for the single-child case.

Thanks!

EDIT: forgot to say that since the model is not bound correctly, then of course all my fields within TextAnswerTranslationForm have no value. I looked at previous issues but there seems to be no way of explicitely binding a model for a collection (since it's a collection, of course)

koichirose avatar Sep 05 '17 15:09 koichirose