类型为file的Symfony 2 Form集合字段


Symfony 2 Form collection field with type file

我想通过POST请求上传多个文件(不带Ajax)。我可以将Symfony 2的表单集合字段与以下类型的文件一起使用吗:

实体代码:

public $pictures;
public function __construct()
{
    $this->pictures = new 'Doctrine'Common'Collections'ArrayCollection();
}

表单类代码:

$builder->add('pictures', 'collection', array(
        'type' => 'file',
        'required' => false,
        'attr' => array(
            'multiple' => 'multiple'
        )
));

Twig:中的代码

{% for picture in form.pictures %}
    <td>
        {{ form_widget(picture) }}
    </td>
{% endfor %}

我试过了,但似乎不起作用。它没有显示任何错误,但也没有显示输入文件。有什么想法吗?

要真正呈现输入类型,您需要将集合中的allow_add选项设置为true,并使用集合的表单原型、javascript和添加文件字段的按钮。

基于文档中的示例(集合-添加和删除)

形式:

<form action="..." method="POST" {{ form_enctype(form) }}>
{# ... #}
{# store the prototype on the data-prototype attribute #}
<ul id="image-container" class="collection-container" data-prototype="{{ form_widget(form.images.vars.prototype) | e }}">
{% for imageField in form.images%}
    <li>
        {{ form_widget(imageField) }}
    </li>
{% endfor %}
</ul>
<a href="#" class="collection-add" data-collection="image-container">Add image</a>
</form>

脚本:

<script type="text/javascript">
  var imageCount;
  jQuery(document).ready(function() {
    $(document).on('click', '.collection-add', function () {
        var $collectionContainer = $('#' + $(this).data('collection'));
        if(!imageCount){imageCount = $collectionContainer.children().length;}
        var prototype = $collectionContainer.attr('data-prototype');
        var item = prototype.replace(/__name__/g, imageCount);
        $collectionContainer.append(item);
        imageCount++;
    });
  })
</script>

这只是一个想法,根据你的需要还有很多事情要做。如果这不是你想要的,也许你可以调用添加按钮点击作为解决方法。

如果您想显示多个输入字段,不,它不起作用。集合类型要求在呈现字段之前提供一些数据。我已经尝试过了,并创建了一个单独的实体(例如File),并将关系添加到我的目标实体。

示例:

class File
{
    // properties
    public $file; // this holds an UploadedFile object
    // getters, setters
}

文件类型:

....
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('file', 'file', [
            'required' => false
        ])
    ;
}

产品:

class Product
{
    // properties
    private $images; // ManyToMany relationship
    // setters, getters
}

产品类型:

->add('images', 'collection', [
    'type' => 'YOUR_FILE_TYPE_NAME',
    'by_reference' => false,
    'required' => false
])

产品控制器:

public function someAction()
{
    ...
    $image = new File();
    $product->addImage($image);
}

我知道这个解决方案可能有些过头,会创建额外的表,但它确实有效。

您需要在集合中指定小部件的类型

看看:http://symfony.com/doc/2.0/reference/forms/types/collection.html

$builder->add('pictures', 'collection', array(
  // each item in the array will be an "email" field
  'type'   => 'file',
  // these options are passed to each "email" type
  'options'  => array(
    'required'  => false,
  ),
));

为了进一步阅读,我建议

http://symfony.com/doc/2.0/reference/forms/types/file.html

此外,你需要将某物添加到该集合中以显示,因为它在初始化时将为空,就像在实体的构造函数中一样。

表单类中的代码是正确的,但您也应该修改表单字段的名称。在您的情况下,full_name应该是form[pictures][]

实际上,在sf2.3中,修改表单字段名称似乎已经不可能了,但在sf2.4中,它将再次成为可能。

https://github.com/bamarni/symfony/commit/35639824e864ed8d4a4cc0d8360f2c73ae08b507#commitcomment-3627879