dsfsw1233 2018-09-12 09:47
浏览 44
已采纳

在symfony中添加带有表单事件的字段

I try add fields with form events in symfony but fields is add automaticly when refresh page ... scenario is when I fill input email fields body is display ...

code PostType:

<?php

    namespace AppBundle\Form;

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolver;
    use Symfony\Component\Form\Extension\Core\Type\SubmitType;
    use Symfony\Component\Form\FormEvent;
    use Symfony\Component\Form\FormEvents;

    class PostType extends AbstractType
    {
        /**
         * {@inheritdoc}
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('titl')->add('email')->add('phone');
            $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event){
                $post = $event->getData();
                $form = $event->getForm();
                   if(!empty($post->getEmail))
                   {
                     $form->add('body');
                   }

                });
            $builder->add('save', SubmitType::class);
        }/**
         * {@inheritdoc}
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'AppBundle\Entity\Post'
            ));
        }

        /**
         * {@inheritdoc}
         */
        public function getBlockPrefix()
        {
            return 'appbundle_post';
        }


    }

help me please and thanks for advanced

  • 写回答

1条回答 默认 最新

  • doq70020 2018-09-13 08:22
    关注

    as it says in comment you have to use Javascript to do this, for example make an ajax request to submit the form and to apply, always in javascript, the HTML response.

    There is a great video tutorial for this (in french) https://www.youtube.com/watch?v=F0Z-D3MSjA0&t=1504s

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?