Symfony2表单无法检测是否已提交实体或上载文件

时间:2015-02-19 18:35:45

标签: php forms symfony

我有一个表单PostType,用于创建名为Post的实体,此表单包含实体PhotoType的嵌入子表单Photo

在编辑Post时,我将PhotoType表单换成ImagePreviewType表单,以显示Photo的小预览,并禁止对Photo进行任何更改}。

这样可行,但问题是,当我预填充PostType表单以创建类似于旧Post的{​​{1}}时。表单呈现正常,我看到Post中的Photo但是在提交时ImagePreviewType数据为空,我无法再次选择$builder->getData(),因此从回退到ImagePreviewType并期望上传的文件但接收实体并引发错误。

我希望你能理解我的问题,这是我的评论来源,它失败了:

PhotoType

PostType

<?php namespace Socialised\FbPostingBundle\Form; use Socialised\FbPostingBundle\Entity\Post; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\Validator\Constraints\Url; use Doctrine\ORM\EntityRepository; use Socialised\FbPostingBundle\Form\Type\ImagePreviewType; use Socialised\FbPostingBundle\Form\Type\VideoPreviewType; class PostType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('message', 'textarea', array( 'required' => false, 'label' => 'form.label.message', 'attr' => array('class' => 'variable photo text link video'), )) ->add('link', 'url', array( 'required' => false, 'label' => 'form.helptext.link.url', 'constraints' => new Url(), 'attr' => array('class' => 'variable link'), ) ) if ($builder->getData()->getPhoto() != null){ // <- This is where it fails /* On submit of a new Entity $builder->getData() is empty, even thought data valid is submit. I'm unable to detect if an image was uploaded or the reference to an `Photo` was send. So the wrong Form is selected. */ $builder->add('photo', new ImagePreviewType(), array( 'label' => 'form.label.photo', 'horizontal_input_wrapper_class' => 'variable photo col-lg-8', 'translation_domain' => 'SocialisedFbPostingBundle', )); }else{ $builder->add('photo', new PhotoType(), array( 'required' => false, 'label' => 'form.label.photo', 'horizontal_input_wrapper_class' => 'variable photo col-lg-8', 'translation_domain' => 'SocialisedFbPostingBundle', )) } } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Socialised\FbPostingBundle\Entity\Post', 'translation_domain' => 'SocialisedFbPostingBundle', 'show_legend' => false, 'attr' => array( 'class' => 'form-horizontal' ), 'securityContext' => false )); } /** * @return string */ public function getName() { return 'socialised_fbpostingbundle_post'; } }

PhotoType

<?php namespace Socialised\FbPostingBundle\Form; use Socialised\FbPostingBundle\Entity\Post; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\Validator\Constraints\Image; use Symfony\Component\Validator\Constraints\File; class PhotoType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('file', 'file', array( 'required' => true, 'label' => false, 'constraints' => array(new Image(array( 'minWidth' => 200, 'minHeight' => 200, )), new File(array( 'maxSize' => '4096k', )) ), ) ) ; } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Socialised\FbPostingBundle\Entity\Photo', 'translation_domain' => 'SocialisedFbPostingBundle', 'show_legend' => false, )); } /** * @return string */ public function getName() { return 'socialised_fbpostingbundle_photo'; } }

ImagePreviewType

解决方案:

将此代码添加到<?php namespace Socialised\FbPostingBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Socialised\FbPostingBundle\Form\PhotoType; class ImagePreviewType extends AbstractType { public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( //'class' => 'SocialisedFbPostingBundle:Photo', 'data_class' => 'Socialised\FbPostingBundle\Entity\Photo', 'property' => 'id', 'by_reference' => false, )); } public function getParent() { return 'text'; } public function getName() { return 'imagepreview'; } } 表单似乎可以解决问题。

PostType

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用POST_SET_DATA表单事件?

http://symfony.com/doc/current/components/form/form_events.html