如何在表单构建器中使用参数化构造函数

如何在表单构建器中使用参数化构造函数

本文介绍了Symfony2 Forms - 如何在表单构建器中使用参数化构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用 Symfony2,在我阅读的文档中,所有与 Symfony 表单一起使用的实体都有空的构造函数,或者根本没有.(例子)

I am learning to use Symfony2 and in the documentation I have read, all entities being used with Symfony forms have empty constructors, or none at all. (examples)

http://symfony.com/doc/current/book/index.html 第 12 章
http://symfony.com/doc/current/cookbook/doctrine/registration_form.html

http://symfony.com/doc/current/book/index.html Chapter 12
http://symfony.com/doc/current/cookbook/doctrine/registration_form.html

我对构造函数进行了参数化,以便在创建时要求某些信息.Symfony 的方法似乎是将强制执行留给验证过程,本质上依靠元数据断言和数据库约束来确保对象被正确初始化,放弃构造函数约束来确保状态.

I have parametrized constructors in order to require certain information at time of creation. It seems that Symfony's approach is to leave that enforcement to the validation process, essentially relying on metadata assertions and database constraints to ensure that the object is properly initialized, forgoing constructor constraints to ensure state.

考虑:

Class Employee {
    private $id;
    private $first;
    private $last;

    public function __construct($first, $last)
    {  ....   }
}

...
class DefaultController extends Controller
{
    public function newAction(Request $request)
    {
        $employee = new Employee();  // Obviously not going to work, KABOOM!

        $form = $this->createFormBuilder($employee)
            ->add('last', 'text')
            ->add('first', 'text')
            ->add('save', 'submit')
            ->getForm();

        return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}

我不应该使用构造函数参数来做到这一点吗?

Should I not be using constructor arguments to do this?

谢谢

在下面回答

推荐答案

找到解决方案:

查看控制器createForm()"方法的 API,我发现了一些从示例中并不明显的东西.似乎第二个参数不一定是对象:

Looking into the API for the Controllers "createForm()" method I found something that is not obvious from the examples. It seems that the second argument is not necessarily an object:

**Parameters**
    string|FormTypeInterface     $type  The built type of the form
    mixed                        $data  The initial data for the form
    array                        $options   Options for the form

因此,您可以简单地传入具有适当字段值的数组,而不是传入实体的实例:

So rather than pass in an instance of the Entity, you can simply pass in an Array with the appropriate field values:

$data = array(
    'first' => 'John',
    'last' => 'Doe',
);
$form = $this->createFormBuilder($data)
    ->add('first','text')
    ->add('last', 'text')
    ->getForm();

另一种选择(可能更好),是创建一个 空数据集 作为表单类中的默认选项.解释这里这里

Another option (which may be better), is to create an empty data set as a default option in your Form Class.Explanations here and here

class EmployeeType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('first');
        $builder->add('last');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'empty_data' => new Employee('John', 'Doe'),
        ));
    }
    //......
}

class EmployeeFormController extends Controller
{
    public function newAction(Request $request)
    {
        $form = $this->createForm(new EmployeeType());
    }
    //.........
}

希望这能让其他人免于头疼.

Hope this saves others the head scratching.

这篇关于Symfony2 Forms - 如何在表单构建器中使用参数化构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:34