使用Forms collection symfony 2上传后显示图像

时间:2018-10-12 08:44:32

标签: forms symfony

我想在我的视图中显示表单集合的图像,但这会使我的脚本崩溃。

我认为我有三个字段(2个文本和1个文件),这要归功于表单的原型。

当我告知3个字段时,将充分考虑2个文本和文件。该文件位于服务器中,然后插入数据库中。

当我想在视图中显示字段时,脚本由于图像而崩溃。当我只填写两个文本并想在表单中显示字段时,一切都将正确显示。

所以关注点来自图像。

您能帮我找到解决方法吗?

代码如下:

表格

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 ) {


}

查看

            <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>

感谢您的帮助

0 个答案:

没有答案