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

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

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上