2

I want to add many hidden files to form builder with one name like an array. For example:

<input type="hidden" name="test[]">
<input type="hidden" name="test[]">
<input type="hidden" name="test[]">
<input type="hidden" name="test[]">

How I can do this? Thanks.

$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
                $form = $event->getForm();

                $photos = $event->getData()->getPhotos();

                if ($photos) {
                    foreach ($photos as $photo) {
                        $form->add('uploadedPhoto', CollectionType::class, array(
                            'entry_type' => HiddenType::class,
    //                        'data' => $photo->getId(),
                            'mapped' => false,
                        ));
                    }
                }
            });

1 Answer 1

2

You need to use collection type:

use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;

$builder->add('foo', CollectionType::class , array('entry_type' => HiddenType::class));

I assume that You want to list as hidden all elements under photos property from data source.

$builder->add('photos', CollectionType::class , array(
    'entry_type' => HiddenType::class, 
    'mapped' => false
));

Read more about this field type: http://symfony.com/doc/current/reference/forms/types/collection.html#entry-type

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. But I use that variant and I have not got result. I add code to question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.