I
I
Ivan Antonov2015-10-07 15:35:43
symfony
Ivan Antonov, 2015-10-07 15:35:43

Symfony form builder with multiple photo upload?

d91499acc39c4f509cc221e794575ca8.PNG
1. Built a form for adding an ad, the Offer entity.
2. Added photo upload, File entity (AJAX upload returns photo ID).
3. How to create OfferImage entities after validating and adding the Offer entity?
Added the Offer entity to the form:

$builder->add('images', 'collection', array(
    'type' => new OfferImage(),
));

in the template like so:
{% for image in form.images %}
    {{ form_widget(image) }}
{% endfor %}
<input type="file" name="pics" multiple>

He explained the situation as best he could. Strongly do not beat if it is not clear.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2015-10-07
@antonowano

If you upload OfferImage separately, then you need to bind them to Offer.
At this point, you are creating an Offer creation form that creates or deletes an OfferImage. (parameters allow_add and allow_delete) On the contrary,
you need to bind existing OfferImages.

// Тут хоть аяксом, хоть вручную выбираете загруженные  OfferImage,
// Потому что они уже есть.
$builder->add('images');

If there is no OfferImage, but there is a File, then
$builder->add('images', 'collection', array(
    'type' => new OfferImageType()
));
// Где в OfferImageType  $builder->add('file');  // И тут выбираем файл из списка

To create an Offer and its accompanying OfferImage in one form
$builder->add('images', 'collection', array(
    'type' => new OfferImageType(),
    'allow_add' => true,
));

Where OfferImageType
$builder->add('file', 'file');
// или $builder->add('file', new FileType()); раз он привязан так.
// и в FileType уже $builder->add('file', 'file');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question