Symfony2 Forms动态添加删除元素

时间:2015-10-06 10:11:58

标签: forms symfony events

您好我无法了解如何根据表单中的选择值添加/删除表单字段。

我有一个名为ContestType的formType:

我想要做的是显示/隐藏其他表单输入,具体取决于contestStyle的值。

ContestStyle是一个有两个值的实体:

  • 0 - >头对头
  • 1 - >联赛

如果选择了(0)Head to Head,我希望有一个隐藏的表单字段,playerCount,其值设置为2

如果选择Leauge(1),我想删除隐藏字段并显示带选项的选择。

它实际上比这更复杂,但是我的头脑应该让我走了。

class ContestType extends AbstractType
{
    protected $em;

   public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            array( 'data_class' => 'GameBundle\Entity\Contest')
        );
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('scheduleGroup', 'entity', array(
                'class' => 'GameBundle:ScheduleGroup',
                'property' => 'name',
                'label' => 'Date',
                'placeholder' => 'Select Date',
            ))

            ->add('contestStyle', 'entity', array(
                'class' => 'GameBundle:ContestStyle',
                'property' => 'name',
                'label' => 'Contest Style',
                'placeholder' => 'Select Contest Style',
            ))
            ->add('playerCount', 'hidden', array(
                'data' => '2',
            ))
            ->getForm();

        $builder->addEventListener(
            FormEvents::POST_SET_DATA,
            function (FormEvent $event) {
                $form = $event->getForm();

                $data = $event->getData();
                $contestStyle = $data->getContestStyle();
                $playerCountOptions = array(
                    3       => '3 Players',
                    4       => '4 Players',
                    5       => '5 Players',
                    6       => '6 Players',
                    7       => '7 Players',
                    8       => '8 Players',
                    9       => '9 Players',
                    10      => '10 Players',
                    11      => '11 Players',
                    12      => '12 Players',
                    13      => '13 Players',
                    14      => '14 Players',
                    15      => '15 Players',
                    16      => '16 Players',
                    17      => '17 Players',
                    18      => '18 Players',
                    19      => '19 Players',
                    20      => '20  Players',
                );
                $list = null === $contestStyle ? array() : $playerCountOptions;
                //var_dump($data);die();
                if($contestStyle >1){
                    $form->remove('playerCount');
                    $form->add('playerCount', 'choice', array(
                        'choices' => $list
                    ));
                }
            }
        );
    }

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

我收到错误提示:类Doctrine \ Common \ Collections \ ArrayCollection的对象无法转换为int

我如何获得contestStyle字段的值?

以下是相关实体:

class Contest
{
    public function __construct()
    {
        $this->scheduleGroup = new ArrayCollection();
        $this->contestStyle = new ArrayCollection();
        $this->entryFee = new ArrayCollection();
        $this->payoutStructure = new ArrayCollection();
    }

    /**
     * @var integer
     *
     * @ORM\Column(name="contestID", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $contestID;

    /**
     * @ORM\OneToOne(targetEntity="FantasyPro\GameBundle\Entity\ScheduleGroup")
     * @Assert\Valid()
     */
    private $scheduleGroup;

     /**
      *
      * @ORM\OneToOne(targetEntity="FantasyPro\GameBundle\Entity\ContestStyle")
      * @Assert\Valid()
      */
    private $contestStyle;

    /**
     * @var integer
     *
     * @ORM\Column(name="playerCount", type="integer", nullable=false)
     * @Assert\NotBlank()
     */
    private $playerCount;

    /**
     * @ORM\OneToOne(targetEntity="FantasyPro\GameBundle\Entity\EntryFee")
     * @Assert\Valid()
     */
    private $entryFee;

    /**
     * @var string
     *
     * @ORM\OneToOne(targetEntity="FantasyPro\GameBundle\Entity\PayoutStructure")
     */
    private $payoutStructure;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="opponent", type="string", length=255, nullable=false)
     *
     */
    private $playerScope;

    /**
     * Get contestID
     *
     * @return integer
     */
    public function getContestID()
    {
        return $this->contestID;
    }

    public function getScheduleGroup()
    {
        return $this->scheduleGroup;
    }

    /**
     * @param ScheduleGroup $scheduleGroup
     */
    public function setScheduleGroup(ScheduleGroup $scheduleGroup = null){
        $this->scheduleGroup = $scheduleGroup;
    }

    public function getContestStyle()
    {
        return $this->contestStyle;
    }

    /**
     * @param ContestStyle $contestStyle
     */
    public function setContestStyle(ContestStyle $contestStyle = null )
    {
        $this->contestStyle = $contestStyle;
    }

    /**
     * @return int
     */
    public function getPlayerCount()
    {
        return $this->playerCount;
    }

    /**
     * @param int $playerCount
     */
    public function setPlayerCount( $playerCount )
    {
        $this->playerCount = $playerCount;
    }

    public function getEntryFee()
    {
        return $this->entryFee;
    }

    public function setEntryFee(EntryFee $entryFee = null )
    {
        $this->entryFee = $entryFee;
    }

    /**
     * @return string
     */
    public function getPayoutStructure()
    {
        return $this->payoutStructure;
    }

    /**
     * @param string $payoutStructure
     */
    public function setPayoutStructure( $payoutStructure )
    {
        $this->payoutStructure = $payoutStructure;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName( $name )
    {
        $this->name = $name;
    }

    /**
     * @return string
     */
    public function getPlayerScope()
    {
        return $this->playerScope;
    }

    /**
     * @param string $playerScope
     */
    public function setPlayerScope( $playerScope )
    {
        $this->playerScope = $playerScope;
    }
}

ContestStyle:

class ContestStyle
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=100)
     */
    private $name;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return ContestStyle
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
}

1 个答案:

答案 0 :(得分:1)

处理程序错误,您需要使用SUBMIT作为事件处理程序:

class ContestType extends AbstractType
{
    protected $em;

   public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            array( 'data_class' => 'GameBundle\Entity\Contest')
        );
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('scheduleGroup', 'entity', array(
                'class' => 'GameBundle:ScheduleGroup',
                'property' => 'name',
                'label' => 'Date',
                'placeholder' => 'Select Date',
            ))

            ->add('contestStyle', 'entity', array(
                'class' => 'GameBundle:ContestStyle',
                'property' => 'name',
                'label' => 'Contest Style',
                'placeholder' => 'Select Contest Style',
            ))
            ->add('playerCount', 'hidden', array(
                'data' => '2',
            ))
            ->getForm();

        $builder->addEventListener(
            FormEvents::SUBMIT,
            function (FormEvent $event) {
                $form = $event->getForm();

                $data = $event->getData();
                $contestStyle = $data->getContestStyle()->getID();;
                $playerCountOptions = array(
                    3       => '3 Players',
                    4       => '4 Players',
                    5       => '5 Players',
                    6       => '6 Players',
                    7       => '7 Players',
                    8       => '8 Players',
                    9       => '9 Players',
                    10      => '10 Players',
                    11      => '11 Players',
                    12      => '12 Players',
                    13      => '13 Players',
                    14      => '14 Players',
                    15      => '15 Players',
                    16      => '16 Players',
                    17      => '17 Players',
                    18      => '18 Players',
                    19      => '19 Players',
                    20      => '20 Players',
                );
                $list = null === $contestStyle ? array() : $playerCountOptions;

                if($contestStyle >1){
                    $form->remove('playerCount');
                    $form->add('playerCount', 'choice', array(
                        'choices' => $list
                    ));
                }
            }
        );
    }

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