简体   繁体   中英

dynamic forms with symfony

Hello i want to display races of animals which depend on the type of the animal , the problem is that $event->getForm()->get('Type_Animal')->getData() always gives me null . This is my code , thanks for the reply =))

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

Actually you're trying to get Type_Animal in FormEvent::PRE_SET_DATA event which is fired, in fact, before setting the data as stated in the relative documentation .

Therefore you can get what you need starting from FormEvents::POST_SET_DATA event.

$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
    $form = $event->getForm();
    dump($form->get('Type_Animal')->getData());
});

Further info about form events are in the official documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM