我刚刚注意到我的代码不再有效了,我不确定我是否做错了或Symfony3中有错误。
所以我得到了一个实体User
,其构造函数中设置的属性isActive
为isActive = 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);
}
}
答案 0 :(得分:0)
您的用户变量名为$user
似乎很奇怪,但createForm
方法的第二个参数是$program
变量:
$user = new User();
$form = $this->createForm(UserType::class, $program);