我使用 ->add('file','file') 添加与文章关联的图像。它工作得很好。
但是我应该怎么做才能从文章中删除图像。 带有文件的表单仅允许我添加或更改与文章关联的图像。没有选项可以简单地说:删除图像。
我应该怎么做?
- 文件中是否存在将此功能添加到表单中的选项?
- 我是否应该在 ArticleType 中添加一个新字段(映射到"false",因为既不是属性也不是实体)此字段允许用户选中"删除图像"复选框。当值设置为 true 时,我处理以删除控制器中的图像?
我更要求方法而不是代码问题,感谢您的任何帮助
代码
class ArticleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('image', new ImageType());
->add('removeimage', 'checkbox', array(
'label' => 'Remove the image?',
'mapped' => false,))
}
}
.
class ImageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', 'file');
}
}
如果您有时间设置文件上传,那么我建议您 用原则处理文件上传 .我有一个使用本文中描述的实践的工作项目,它运行得非常好。
如果要在表单中制作"删除"按钮,请按照以下简单步骤操作:
首先在表单构建器中添加以下行:
->add('save', 'submit', array('label' => 'Save')) // it's for saving article
->add('delete', 'submit', array('label' => 'Delete')) // it's for deleting file
然后在控制器中检查单击了哪个按钮:
if ($form->get('delete')->isClicked()) { // your code here }
而不是你的代码在这里写一些东西:
$article->getImage()->removeUpload();
$this->getDoctrine()->getManager()->flush();
就是这样!