Skip to content

Initial Values Normalizer produces StdClass instead of Array for collections #29

@jakub-zawislak

Description

@jakub-zawislak

Symfony 2.8

I have a form:

$builder->add('name', TextType::class);
$builder->add('contacts', CollectionType::class, [...]);

The normalizer produces this output:

stdClass Object
(
    [name] => Test
    [contacts] => stdClass Object
        (
            [0] => stdClass Object
                (
                    [value] => TestValue
                )
        )
)

instead of:

stdClass Object
(
    [name] => Test
    [contacts] => Array <--------- array
        (
            [0] => stdClass Object
                (
                    [value] => TestValue
                )
        )
)

I'm passing this data to a JS in this way inside <script> in .twig:

window.form.initialValues = {{ initialValues|json_encode|raw }}

stdClass and Array are different in JSON format. stdClass gives {0: item} instead of [item].

As a result I have error while using liform-react in ConnectedFieldArray.js:83

nextValue.every is not a function

When I hardcoded StdClass -> Array conversion, all works

$initialValues->contacts = json_decode(json_encode($initialValues->contacts), true);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions