【问题标题】:Symfony2 Forms dynamically adding removing elementsSymfony2 Forms 动态添加删除元素
【发布时间】:2015-10-06 10:11:58
【问题描述】:

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

我有一个名为 ContestType 的 formType:

我想要做的是根据竞赛样式的值显示/隐藏其他表单输入。

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

如何获得比赛样式字段的值?

这里是相关的实体:

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;
    }
}

比赛风格:

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;
    }
}

【问题讨论】:

    标签: forms symfony events


    【解决方案1】:

    Handler错误,需要使用SUBMIT作为eventhandler:

    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';
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-12-29
      • 2016-01-28
      • 2013-08-22
      • 2013-11-23
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多