简体   繁体   中英

Symfony 3 form collection of entities with FOSRestBundle

We are using the FOSRestBundle . Assume I would like to create a Ticket object via API with the following JSON body:

{
    "title": "I need help",
    "symptoms": [1, 4, 6]
    "author": 31
}

The author and array of symptoms reference primary keys in the database.

My form building looks like:

$builder->add('title', TextType::class);
$builder->add('author', EntityType::class, [
    'class' => User::class
]);
$builder->add('symptoms', CollectionType::class, [
    'entry_type' => EntityType::class,
    'entry_options' => [
        'class' => Symptom::class,
    ],
]);

The problem is that title and author are assigned correctly to the ticket object but the array of symptoms is not transformed into an collection of entities. Additionally there is an error that This form should not contain extra fields .

How should the form builder look like if I would like to assign a collection of entities to an object?

This form should not contain extra fields

This error means that the form structure has changed between its creation and the submission. Are you using an AJAX request to modify the DOM dynamically? I think you should take a look here

For the empty array of tags you should set the allow_add option to true in the definition of symptoms widget Take a look here

for more details ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM