0

当我为任意实体定义表单类型时,比如说User,我可以添加一个 UserFormType 类,如下所示,

class UserFormType 
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // ...
        $builder->add('email', 'text');
    }
}

如果我想为用户定义另一种表单类型怎么办:

  • 在这种情况下,什么是好的命名约定?
  • 把逻辑放在哪里?放入一个单独的 FormType 类或作为另一种方法放入现有的类中?
4

1 回答 1

2

You can define as many Types as you need for each entity.

If I need more than one Type, I usually have the "standard" entitynameType and for the other ones I attach some keyword related to the place I am using it. For example if one form is only for update the status UserStatusType.

You have to use another class because you are extending AbstractType and the method that build the form must be buildForm.

If you need to adapt your form depending the kind of user you have you can use an event listener http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

于 2013-09-06T14:54:27.587 回答