gpt4 book ai didi

php - 如何在 symfony2 中使用 FileType 输入处理编辑表单

转载 作者:可可西里 更新时间:2023-11-01 00:09:43 25 4
gpt4 key购买 nike

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

但是如果我想编辑实体 Message 表单包含与现有文档一样多的空 FileInput。期望的行为是:

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

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

如何实现?

最佳答案

解决方案是编写自定义表单类型扩展。如 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);
}
}
}

自定义文件小部件

    {% 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 %}

服务.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"
));

就是这样;)

关于php - 如何在 symfony2 中使用 FileType 输入处理编辑表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15553060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com