我想为上传的图片提取并保存EXIF数据。对于我使用的表格
<?php
namespace Timeline\DefaultBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Timeline\DefaultBundle\Form\EventListener\DocumentListener;
class DocumentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file')
->add('upload', 'submit')
->addEventSubscriber(new DocumentListener());
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Timeline\DefaultBundle\Entity\Document',
));
}
public function getName()
{
return 'document';
}
}
然后我有一个听众课程:
<?php
namespace Timeline\DefaultBundle\Form\EventListener;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DocumentListener implements EventSubscriberInterface{
public static function getSubscribedEvents()
{
return array(
FormEvents::POST_SUBMIT => 'onPostSubmit'
);
}
public function onPostSubmit(FormEvent $event)
{
$form = $event->getForm();
$form->add('exif','collection',array('data' => array("a" => 'some data',"b" => "some data","c" => "some data")));
}
我遇到的问题是,如果我使用POST_SUBMIT
,我会收到错误您无法将儿童添加到提交的表单中
如果我使用PRE_SUBMIT
,则会存储密钥,但不存储数据{"a":null,"b":null,"c":null}
如何在文件上传后添加EXIF数据?
答案 0 :(得分:0)
$event->getForm()->getParent()
您需要基本上将字段添加到表单的父级。所以以下内容应该有效:
public function onPostSubmit(FormEvent $event)
{
$form = $event->getForm()->getParent();
$form->add('exif','collection',array('data' => array("a" => 'some data',"b" => "some data","c" => "some data")));
}
答案 1 :(得分:0)
您需要在$formModifier
尝试使用buildForm
回调来包含此内容:
$formModifier = function (FormInterface $form) {
$form->add('exif','collection',array('data' => array("a" => 'some data',"b" => "some data","c" => "some data")));
};
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($formModifier) {
$formModifier($event->getForm());
});
FormModifier
在http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms
- 编辑
要使其工作,您需要以下使用语句
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
此外,如果你采用这种方法,你可以删除你的
->addEventSubscriber(new DocumentListener())
在您最初的$builder
电话