douhui2307 2014-03-07 04:09
浏览 31
已采纳

复杂的表单集合类型和事件在Symfony2中预先设置数据

in Controller

$form = $this->createForm(new ArticleType($this->getUser()), $article);

in ArticleType

class ArticleType extends AbstractType
{
    private $appUser;

    public function __construct($appUser)
    {
        $this->appUser = $appUser;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $data = $builder->getData();
        $builder->add('name', 'text');
        $builder->add('examples', 'collection', array(
            'type'    => new ExampleType($this->appUser),
            'options' => array(
                'required' => true,
            ),
            'allow_add'    => true,
            'by_reference' => false,
            'allow_delete' => true,
            'prototype'    => true
        ));

        if (NULL == $data->getOwner() || $data->getOwner() == $this->appUser) {
            $builder->add('status', 'choice', array(
                'choices' => array(
                    'A' => 'A',
                    'B' => 'B',
                    'C' => 'C',
                ),
                'required' => true
            ));
        }
    }

    // ...
}

in ExampleType

// ...
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class ExampleType extends AbstractType
{
    private $appUser;

    public function __construct($appUser)
    {
        $this->appUser = $appUser;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text');

        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function(FormEvent $event) {
                $example = $event->getData();
                $form    = $event->getForm();

                // Problem 1 : This $example is null if data already registered example.
                // problem 2 : Using $this when not in object context
                if (!$example || (null == $example->getOwner() || $example->getOwner() == $this->appUser)) {
                    $form->add('status', 'choice', array(
                        'choices' => array(
                            'A' => 'A',
                            'B' => 'B',
                            'C' => 'C',
                        ),
                        'required' => true
                    ));
                }
            });
    }

    // ...
}

Data confirmation process in the collection form does not work.
After this, ArticleType has to be collection from another.
For now, I want to clear the error of this stage. (in PHP 5.3)

  • 写回答

1条回答 默认 最新

  • duanqin4238 2014-03-07 05:32
    关注

    You have to change buildform in ExampleType.php to:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text');
        $appUser = $this->appUser;
        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function(FormEvent $event) use ($appUser) {
                $example = $event->getData();
                $form    = $event->getForm();
    
                // This $example is null if data already registered example.
                // Error: Using $this when not in object context
                if (!$example || (null == $example->getOwner() || $example->getOwner() == $appUser)) {
                    $form->add('status', 'choice', array(
                        'choices' => array(
                            'A' => 'A',
                            'B' => 'B',
                            'C' => 'C',
                        ),
                        'required' => true
                    ));
                }
            });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建