【问题标题】:howto handle edit forms with FileType inputs in symfony2如何在 symfony2 中处理带有 FileType 输入的编辑表单
【发布时间】:2013-03-21 16:31:41
【问题描述】:

在 symfony2 应用程序中,实体消息与文档具有一对多的关系。文档代表用户上传。我创建了一个表格。我实现了两种形式:MessageForm 和 DocumentForm。 DocumentForm 存在于 MessageForm 的集合 FormField 中。上传和处理文件确实有效。

但是,如果我想编辑实体消息,则表单包含与现有文档一样多的空文件输入。期望的行为是:

  • FileInputs 上传新文件
  • 现有文件的文件名(链接)
  • 可以删除现有文件

这应该在表单内部处理。提交表单时应进行更改。

如何实现?

【问题讨论】:

  • 如果你指出你尝试了什么,你可能会得到更好的回应。

标签: php forms symfony


【解决方案1】:

解决方案是编写自定义表单类型扩展。如http://symfony.com/doc/2.1/cookbook/form/create_form_type_extension.html 所述。

文件类型扩展

    <?php

    use Symfony\Component\Form\AbstractTypeExtension;
    use Symfony\Component\Form\FormView;
    use Symfony\Component\Form\FormInterface;
    use Symfony\Component\Form\Util\PropertyPath;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;

    /**
     * Class FileTypeExtension
     *
     * @see http://symfony.com/doc/2.1/cookbook/form/create_form_type_extension.html
     */
    class FileTypeExtension extends AbstractTypeExtension
    {
        /**
        * Returns the name of the type being extended.
        *
        * @return string The name of the type being extended
        */
        public function getExtendedType()
        {
            return 'file';
        }

        /**
         * Add the image_path option
         *
         * @param OptionsResolverInterface $resolver
         */
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setOptional(array('file_path', 'file_name'));
        }

        /**
         * Pass the image url to the view
         *
         * @param FormView $view
         * @param FormInterface $form
         * @param array $options
         */
        public function buildView(FormView $view, FormInterface $form, array $options)
        {
            if (array_key_exists('file_path', $options)) {
                $parentData = $form->getParent()->getData();

                if (null !== $parentData) {
                    $propertyPath = new PropertyPath($options['file_path']);
                    $fileUrl = $propertyPath->getValue($parentData);
                } else {
                    $fileUrl = null;
                }

                $view->set('file_url', $fileUrl);
            }

            if (array_key_exists('file_name', $options)) {
                $parentData = $form->getParent()->getData();

                if (null !== $parentData) {
                    $propertyPath = new PropertyPath($options['file_name']);
                    $fileName = $propertyPath->getValue($parentData);
                } else {
                    $fileName = null;
                }

                $view->set('file_name', $fileName);
            }
        }
    }

自定义的file_widget

    {% block file_widget %}
        {% spaceless %}

            {% if file_url is not null %}
                <div><a href="{{ file_url }}">{{ file_name }}</a></div>
                <div style="display:none">{{ block('form_widget') }}</div>
            {% else %}
                {{ block('form_widget') }}
            {% endif %}

        {% endspaceless %}
    {% endblock %}

services.yml

    parameters:
        foobar.file_type_extension.class: Foobar\Form\Extension\FileTypeExtension

    services:
        foobar.file_type_extension:
            class: %replacethis.file_type_extension.class%
            tags:
              - { name: form.type_extension, alias: file }

在表单类型中

    $builder->add('file','file', array(
                "label" => "Datei",
                "required" => true,
                "attr" => array(),
                "file_path" => "webPath",
                "file_name" => "name"
            ));

就是这样;)

【讨论】:

    【解决方案2】:

    除了上面的回答之外——它可以让你构建一个文件输入,它可以呈现为一个链接(如果它有一个 url)或一个字段(如果它没有)——你可以看看

    http://symfony.com/doc/2.0/cookbook/form/form_collections.html

    Wich 结合一些 jQuery 可以让你在 UI 中添加字段。

    【讨论】:

    • 不要认为问题在于能够添加多个文件输入,问题在于编辑它们。
    猜你喜欢
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 2021-10-23
    相关资源
    最近更新 更多