php – 如何使用symfony2中的FileType输入处理编辑表单
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 如何使用symfony2中的FileType输入处理编辑表单,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2720字,纯文字阅读大概需要4分钟。
内容图文
![php – 如何使用symfony2中的FileType输入处理编辑表单](/upload/InfoBanner/zyjiaocheng/752/ea86bd6e9172494a883f902ff36aac9b.jpg)
在symfony2应用程序中,实体Message与文档具有一对多的关系.文档表示用户上传.我创建了一个表单.我意识到两种形式:MessageForm和DocumentForm. DocumentForm位于MessageForm中的集合FormField中.上传和处理文件确实有效.
但是,如果我想编辑实体消息,则表单包含与存在的文档一样多的空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);
}
}
}
自定义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 }
在formtype中
$builder->add('file','file', array(
"label" => "Datei",
"required" => true,
"attr" => array(),
"file_path" => "webPath",
"file_name" => "name"
));
而已
内容总结
以上是互联网集市为您收集整理的php – 如何使用symfony2中的FileType输入处理编辑表单全部内容,希望文章能够帮你解决php – 如何使用symfony2中的FileType输入处理编辑表单所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。