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 请问不小心下载到了钓鱼软件怎么办?
  • ¥15 求国博抢票 求国博抢票 有的私
  • ¥50 swiftui @query 报错
  • ¥50 怎么解决刷卡或扫码后,点击软件输入框,win10屏幕键盘不会自动弹出的问题
  • ¥15 如何使用arcgispro的训练深度模型,发现water和nowater精度为0?(相关搜索:深度学习)
  • ¥20 matlab作业不太懂呀有问题能给个代码吗
  • ¥15 自制电路图为何无法驱动ESP01S?
  • ¥15 前端加access数据库
  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]