Sergio González
Sergio González

Reputation: 142

Symofony building subform

Im trying to create a special form, first of all I have a form about a professional, and it has experience as a property. I add those values to my form:

$builder->add('job_company');
$builder->add('job_position');

The problem is that I want to be able to generate multiple experiences, with a button. I should use the CollectionType of symphony, but I don't know if I need to create another special form for the experience, so when I click on the button I create those inputs with a different name (this name must be different if I want to validate them).

I've tried to use:

->add('experiences', CollectionType::class, array(
        'entry_type'   => TextType::class,
        'allow_add'  => true,
        'allow_delete' => true,
    ))

But when I try to render it ({{ form_widget(form.experiences)}}), I get these error:

AppBundle\Entity\ProfessionalExperience could not be converted to string

I also tried ({{ form_widget(form.experiences.company)}}) but I can't get this variable.

How can I do it properly?

Thank you for helping.

Upvotes: 0

Views: 35

Answers (1)

Jakub Matczak
Jakub Matczak

Reputation: 15656

You have declared that you experiences subform is TextType, therefore

{{ form_widget(form.experiences)}}

is trying to create a simple <input/> field and set its value. The value of input is a simple string while you're providing AppBundle\Entity\ProfessionalExperience instance. That's why you're getting this error.

You don't want it to be TextType. It should be some kind of custom form class like ExperienceType.

->add('experiences', CollectionType::class, array(
        'entry_type'   => ExperienceType::class,
        'allow_add'  => true,
        'allow_delete' => true,
    ))

And you should of course create such form class with fields suitable for your AppBundle\Entity\ProfessionalExperience entity

Upvotes: 1

Related Questions