在 属性 路径中给出的 "string"、"null" 类型的预期参数

Expected argument of type "string", "null" given at property path

实际上,当我尝试通过发送空字段来编辑表单时,出现了上述错误,

我的用户类型 class 看起来像:

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstName', null, [
                'label' => 'Prénom'
            ])
            ->add('lastName', null, [
                'label' => 'Nom'
            ])
            ->add('email', EmailType::class, [
                'label' => 'Adresse e-mail'
            ])
            ->add('password', PasswordType::class, [
                'label' => 'Mot de passe'
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => User::class,
        ]);
    }
}

这个问题可以通过在 builder 添加函数中添加 'empty_data' 参数来解决:

因此新的 UserType 类变为:

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstName', null, [
                'label' => 'Prénom',
                **'empty_data' => ''**
            ])
            ->add('lastName', null, [
                'label' => 'Nom',
                **'empty_data' => ''**
            ])
            ->add('email', EmailType::class, [
                'label' => 'Adresse e-mail',
                **'empty_data' => ''**
            ])
            ->add('password', PasswordType::class, [
                'label' => 'Mot de passe',
                **'empty_data' => ''**
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => User::class,
        ]);
    }
}