繁体   English   中英

Symfony2中的复杂表单收集类型和事件预设数据

[英]complex form collection type and events pre set data in Symfony2

在控制器中

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

在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
            ));
        }
    }

    // ...
}

在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
                    ));
                }
            });
    }

    // ...
}

收集表单中的数据确认过程不起作用。
此后,ArticleType必须从另一个收集。
现在,我想清除此阶段的错误。 (在PHP 5.3中)

您必须将ExampleType.php中的buildform更改为:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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