7
votes

I need your advice with CollectionType field.

I've made my collection type field using this: Symfony CollectionType Field
Everything works fine. Even dynamic adding field with JQuery works properly.

But there is an issue.

I can't find out how to add one field each time the collection field renders. Right now after my page loads, my collection field is empty. However, I want the first field from the collection after every page load.

How to achieve this? Should I use Javascript or kind of form event? Any tips for finding a right chapter in documentation or code snippets will be very welcome.

3
Can you elaborate a bit? Are you trying to load an existing entity? Or do you just need a blank field on first load? - OK sure
Ok. My CollectionType field option 'entry_type' => TextType::class. So after page loads i want to see one text field ready to fill for user. I know that im passing array of fields but unfortunately I don't know how to add deafult first element. - fabtosz
It's been a while since I've done this - does 'allow_add' => true, in the collection config not help? - OK sure
According to documentation: "If allow_add is set to true, then if any unrecognized items are submitted, they'll be added seamlessly to the array of items." so it gives option to add dynamically field after submitting. However I want the first item rendered before submit right after page load. - fabtosz

3 Answers

2
votes

This way worked for me.

// 3 default values for empty form
$configArr = [[], [], []];

if ($builder->getData()) {
    $configArr = $builder->getData()->getColumns();
}

$builder->add('columns', CollectionType::class, [
    'entry_type' => ColumnType::class,
    'prototype' => true,
    'allow_add' => true,
    'allow_delete' => true,
    'data' => $configArr,
    'mapped' => false,
]);
1
votes

I think I remember hitting this and I ended up creating data with an empty child on first load.

I'm unable to test this however I think the following should work depending on your form specifics.

public function someAction()
{
    $data = [];
    $data['collection_name'][] = ['text' => ''];

    $form = $this->createForm(SomeType::class, $data);

    return $this->templating->renderResponse('template.twig.html', [
        'form' => $form,
    ]);
}

Adding 'required' => false, to the collection's form config should prevent empty items being persisted.

You may also investigate the configureOptions method on the form Type class:

public function configureOptions(OptionsResolver $resolver)
{
    $data = [];
    $data['collection_name'][] = ['text' => ''];
    $resolver->setDefaults(array(
        'empty_data' => $data,
    ));
}
0
votes

This simply worked for me.

public function buildForm(FormBuilderInterface $builder, array $options) {
    $data = $builder->getData(); // data passed to the form
    $builder->add('textFields', CollectionType::class, [
        'entry_type' => TextType::class,
        'data' => $data ? $data->getTextFields() : ['']
    ]);
}