I would like to display the images from a collection of the form in my view but that makes crash my script.
In my view I have three fields (2 texts and 1 file) that I can duplicate as much as I want thanks to the prototype of the form.
When I inform the 3 fields, the 2 texts and the file are well taken into account. The file is in the server and insert into database.
When I want to display the fields in my view, the script crashes because of the image. When I only fill in the two texts and want to display my fields in my form, everything is displayed correctly.
So the concern comes from the image.
Can you please help me find the solution?
Here is the code:
Form
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('pictoDesign', 'choice', array(
'choices' => array(
'empty' => 'Rien',
'round' => 'round',
'bubble' => 'bulle',
),
'attr' => array(
'class' => 'form-control'
),
'label' => 'Forme du fond du picto',
'mapped' => false
))
->add('pictoSize', 'text', array(
'attr' => array(
'class' => 'form-control'
),
'label' => 'Taille du picto',
'mapped' => false,
'required' => false
))
->add('pictoColor', 'text', array(
'attr' => array(
'class' => 'colorpicker'
),
'label' => 'Couleur du picto',
'mapped' => false,
'required' => false
))
->add('textSize', 'text', array(
'attr' => array(
'class' => 'form-control'
),
'label' => 'Taille du texte',
'mapped' => false,
'required' => false
))
->add('textColor', 'text', array(
'attr' => array(
'class' => 'colorpicker'
),
'label' => 'Couleur du texte',
'mapped' => false,
'required' => false
))
->add('backgroundColor', 'text', array(
'attr' => array(
'class' => 'colorpicker'
),
'label' => 'Couleur de fond',
'mapped' => false,
'required' => false
))
->add('sources', 'collection', array(
'type' => new SourcesCollectionType(),
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'prototype' => true,
'required' => false,
'mapped' => false
))
;
$builder->addEventListener(FormEvents::POST_SET_DATA, array($this, 'onPostSetData'));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'OuibeatAppBundle:Storyboard',
));
}
public function onPostSetData(FormEvent $event) {
$form = $event->getForm();
$settings = $event->getData();
if($settings) {
foreach ((array)$settings as $name => $value) {
if($name != "backgroundImagePath") {
$form->get($name)->setData($value);
}
}
}
}
public function getName()
{
return 'counterSW';
}
SourcesCollectionType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('socialMedia', 'choice', array(
'choices' => array(
'facebook' => 'facebook',
'twitter' => 'twitter',
'instagram' => 'instagram'
),
'label' => "Sélectionner une source",
'multiple' => false,
'expanded' => true
))
->add('idSource', 'text', array(
'attr' => array(
'class' => 'form-control'
),
'label' => 'ID des sources',
'required' => true
))
->add('backgroundColorSW', 'text', array(
'attr' => array(
'class' => 'colopicker'
),
'label' => 'Couleur de fond de la source',
'required' => false
))
->add('backgroundImageSW', 'file', array(
'label' => 'Image de fond de la source',
'required' => false
))
;
$builder->addEventListener(FormEvents::POST_SET_DATA, array($this, 'onPostSetData'));
}
public function getName()
{
return 'source_collection_type';
}
public function onPostSetData( FormEvent $event ) {
}
View
<div class="col-md-12">
<div id="sourcesSW" class="collection-block form-group" data-prototype="{{ form_widget(formSettings.sources.vars.prototype)|e }}">
{% for content in formSettings.sources %}
{{ form_widget(content) }}
{% endfor %}
</div>
<button type="button" id="add-another-background-color" class="btn btn-default add-another-item">{% trans %}Ajouter une nouvelle source{% endtrans %}</button>
Thank for your help