Symfony3表单覆盖实体

时间:2016-03-27 15:45:55

标签: forms entity symfony

我刚刚注意到我的代码不再有效了,我不确定我是否做错了或Symfony3中有错误。

所以我得到了一个实体User,其构造函数中设置的属性isActiveisActive = 1

当我使用UserType连接实体时,突然我的isActive属性的默认值不是1而是NULL

所以这就是它的样子:

    $user = new User();
    var_dump($user->getIsActive()); // gives 1
    $form = $this->createForm(UserType::class, $user);
    var_dump($user->getIsActive()); // gives 1
    $form->handleRequest($request);
    var_dump($user->getIsActive()); // gives NULL

以下是UserType的样子:

public function buildForm(FormBuilderInterface $builder, array $option)
{
    $this->name($builder);
    $this->isActive($builder);
}

isActive特质

trait IsActiveField
{
    public function isActive(FormBuilderInterface $builder, array $options = [])
    {
        $defaultOptions = [
           'label' => 'word.is_active',
           'choices' => ['word.no' => 0, 'word.yes' => 1],
        ];

        $options = array_merge($defaultOptions, $options);
        return $builder->add('isActive', ChoiceType::class, $options);
    }
}

1 个答案:

答案 0 :(得分:0)

您的用户变量名为$user似乎很奇怪,但createForm方法的第二个参数是$program变量:

$user = new User();
$form = $this->createForm(UserType::class, $program);