首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有symfony的动态形式

带有symfony的动态形式
EN

Stack Overflow用户
提问于 2018-03-30 15:25:08
回答 1查看 66关注 0票数 1

你好,我想显示动物种族,这取决于动物的类型,问题是,$event->getForm()->get('Type_Animal')->getData()总是给我空。这是我的代码,谢谢你的回复

代码语言:javascript
复制
class AjoutPost extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $race=array("Lionhead"=>"Lionhead", "Mini Rex"=>"Mini Rex", "Holland Lop"=>"Holland Lop", "Dutch"=>"Dutch", "Dwarf Hotot"=>"Dwarf Hotot", "Mini Lop"=>"Mini Lop", "Mini Satin"=>"Mini Satin", "Netherland Dwarf"=>"Netherland Dwarf", "Polish"=>"Polish", "Other"=>"Other");

        $builder
            ->add('nom',null,array('required'=>true))
            ->add('contenu',TextareaType::class, array(
                'attr' => array('cols' => '100', 'rows' => '5','placeholder'=>'Your Post Here','required'=>true)))
            ->add('type',ChoiceType::class,array(
                'choices'=>array('Lost Animal'=>'Lost Animal',"Found Animal"=>"Found Animal","Coupling"=>"Coupling","Adoption"=>"Adoption"),
                'multiple'=>false,
                'label'=>'Type'
            ))
            ->add('Type_Animal',ChoiceType::class,array(
                'choices'=>array('Dog'=>'Dog',"Cat"=>"Cat","Rabbit"=>"Rabbit","Other"=>"Other"),
                'multiple'=>false,
                'label'=>'Type_Animal',
            ))
            ->add('url_image', FileType::class, array('label' => 'Image'))
            ->add('age',IntegerType::class)
            ->add('sexe',ChoiceType::class,array(
                'choices'=>array('Male'=>'m',"Female"=>"f"),
                'multiple'=>false,
                'label'=>'Sex'
            ))
            ->add('couleur',ChoiceType::class,array(
                'choices'=>array("Brown"=>"Brown", "Black"=>"Black", "Yellow"=>"Yellow", "Green"=>"Green", "Grey"=>"Grey", "Blue"=>"Blue"),
                'multiple'=>false,
                'label'=>'Color'
            ))
            ->add('nom_animal')
            ->add('couleur_yeux',ChoiceType::class,array(
                'choices'=>array("Brown"=>"Brown", "Black"=>"Black", "Yellow"=>"Yellow", "Green"=>"Green", "Grey"=>"Grey", "Blue"=>"Blue"),
                'multiple'=>false,
                'label'=>'Eye Color'
            ))
            ->add('Race',ChoiceType::class,array(
                'choices'=>$race,
                'multiple'=>false,
                'label'=>'Race'
            ))
            ->add('Post', SubmitType::class);
        $builder->addEventListener(FormEvents::PRE_SET_DATA
            , function (FormEvent $event) {
            $form=$event->getForm();
            dump($form->get('Type_Animal')->getData());
            $type=$form->get('Type_Animal')->getData();
            $race = array();
            if($type=='Dog'){
                $form->remove('Race');
                $race=array("Labrador retriever"=>"Labrador retriever", "German shepherd"=>"German shepherd", "Golden retriever"=>"Golden retriever", "Bulldog"=>"Bulldog", "Beagle"=>"Beagle", "French bulldog"=>"French bulldog", "Poodle"=>"Poodle", "Rottweilers"=>"Rottweilers", "Yorkshire terrier"=>"Yorkshire terrier", "Boxers"=>"Boxers","Other"=>"Boxers");
                $form->add('Race',ChoiceType::class,array(
                    'choices'=>$race,
                    'multiple'=>false,
                    'label'=>'Race'
                ));
            }
            else if($type=="Cat"){
                $form->remove('Race');
                $race=array("American Shorthair"=>"American Shorthair", "Persian"=>"Persian", "Maine Coon"=>"Maine Coon", "Siamese"=>"Siamese", "Abyssinian"=>"Abyssinian", "Other"=>"Other");
                $form->add('Race',ChoiceType::class,array(
                    'choices'=>$race,
                    'multiple'=>false,
                    'label'=>'Race'
                ));
            }
            else if($type=="Rabbit"){
                $form->remove('Race');
                $race=array("Lionhead"=>"Lionhead", "Mini Rex"=>"Mini Rex", "Holland Lop"=>"Holland Lop", "Dutch"=>"Dutch", "Dwarf Hotot"=>"Dwarf Hotot", "Mini Lop"=>"Mini Lop", "Mini Satin"=>"Mini Satin", "Netherland Dwarf"=>"Netherland Dwarf", "Polish"=>"Polish", "Other"=>"Other");
                $form->add('Race',ChoiceType::class,array(
                    'choices'=>$race,
                    'multiple'=>false,
                    'label'=>'Race'
                ));
            }
            else{
                $form->remove('Race');
                $form->add('Race');
            }
        });
    }

    public function configureOptions(OptionsResolver $resolver)
    {

    }
    public function getBlockPrefix()
    {
        return "AjoutPOst";
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-31 15:23:07

实际上,您正在尝试在FormEvent::PRE_SET_DATA事件中获取FormEvent::PRE_SET_DATA,实际上,在设置相关文献中所述的数据之前,会触发该事件。

因此,您可以从FormEvents::POST_SET_DATA事件开始获取所需的内容。

代码语言:javascript
复制
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
    $form = $event->getForm();
    dump($form->get('Type_Animal')->getData());
});

关于表单事件的更多信息在正式文件中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49577042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档