form.post_bind被解雇两次

时间:2015-03-17 02:39:22

标签: php symfony

我在我的Symfony表单中添加了一个表单事件。问题是这个事件似乎被解雇了两次。当我调试时,它进入方法两次,我不知道为什么。

我有一个使用其他表单类型的表单类型。我有一个VideoFileType表单,它添加了一个ImageType字段。提交表单后,这两种表单都需要完成一些过程。

将ImageType

<?php
# src/Acme/PhotoBundle/Form/Type/ThumbnailType.php

namespace OSC\MediaBundle\Form\Type;

use OSC\MediaBundle\Manager\ImageManager;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ImageType extends AbstractType
{
    private $imageManager;

    public function __construct(ImageManager $imageManager) {
        $this->imageManager = $imageManager;
    }


    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('file', 'file');

        $builder->addEventListener(
            FormEvents::POST_SUBMIT,
            array($this, 'onPostSetData')
        );
    }

    public function getDefaultOptions(array $options) {
        return array('data_class' => 'OSC\MediaBundle\Entity\Image');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'OSC\MediaBundle\Entity\Image'
        ));
    }

    public function onPostSetData(FormEvent $event) {
        $image = $event->getData();
        $form = $event->getForm();

        //We need here to update the image file with the new content
        $image = $this->imageManager->uploadImage($image);
        $event->setData($image);
    }

    public function getName()
    {
        return 'image';
    }
}

VideoFileType

<?php
# src/Acme/PhotoBundle/Form/Type/ThumbnailType.php

namespace OSC\MediaBundle\Form\Type;

use OSC\MediaBundle\Manager\VideoFileManager;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class VideoFileType extends AbstractType
{

    public $container;

    public $videoPresets = [];

    public $videoFileManager;

    public function __construct(VideoFileManager $videoFileManager, Container $container) {
        $this->container = $container;
        $videoPresets = $container->getParameter('osc_media.video.presets');
        foreach ($videoPresets as $key => $videoPreset) {
            array_push($this->videoPresets, $key);
        }

        $this->videoFileManager = $videoFileManager;

    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('thumbnail', 'image');
        $builder->add('file', 'file');
        $builder->add('videoPreset', 'choice', array(
            'choices' => $this->videoPresets,
            'multiple' => false,
            'required' => true
        ));
        $builder->addEventListener(
            FormEvents::POST_SUBMIT,
            array($this, 'onPostSetData')
        );
        $builder->add('save', 'submit');
    }

    public function onPostSetData(FormEvent $event) {
        $videoFile = $event->getData();
        $form = $event->getForm();

        //We upload the video file
        $videoFile = $this->videoFileManager->uploadVideoFile($videoFile);

        $event->setData($videoFile);
    }


    public function getDefaultOptions(array $options) {
        return array('data_class' => 'OSC\MediaBundle\Entity\VideoFile');
    }

    public function getName()
    {
        return 'video_file';
    }
}

我不知道为什么onPostSetData方法被调用两次......

0 个答案:

没有答案