Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Actually when I try to edit the form by sending empty fields, the above error comes on ,

My UserType class looks like:

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,
        ]);
    }
}
like image 981
Mustapha GHLISSI Avatar asked Dec 18 '22 14:12

Mustapha GHLISSI


1 Answers

This problem can be resolved by adding 'empty_data' param in the builder add function:

So the new UserType classe becomes:

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,
        ]);
    }
}
like image 168
Mustapha GHLISSI Avatar answered Dec 27 '22 09:12

Mustapha GHLISSI