1
votes

My application show this error

Type error: Too few arguments to function AppBundle\Form\ActualiteType::__construct(), 0 passed in /Applications/MAMP/htdocs/SyndicNous/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php on line 90 and exactly 2 expected

My formType

class ActualiteType extends AbstractType { /** * @var bool $admin */ private $admin;

/**
 * @var User $user
 */
private $user;

/**
 * ActualiteType constructor.
 * @param bool|false $admin
 */
public function __construct($admin = false, $user)
{
    $this->admin = $admin;
    $this->user = $user;
}

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $categories = array(
        'Travaux' => 'Travaux',
        'Voisinage' => 'Voisinage',
    );

    $builder
        ->add('title')
        ->add('category')
        ->add('content')
        ->add('datePublish')
        ->add('category', ChoiceType::class, array(
                    'choices' => $categories
            )
        );
    if ($this->user->getResidence() != null) {
        $builder->add('residence', EntityType::class, array(
            'class' => 'AppBundle:Residence',
            'choices' => $this->user->getResidence(),
        ));
    } else {
        $builder->add('residence', 'entity', array(
            'class' => 'AppBundle:Residence',
            'choice_label' => 'name'
        ));
    };
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Actualite'
    ));
}

/**
 * {@inheritdoc}
 */
public function getBlockPrefix()
{
    return 'appbundle_actualite';
}

}

Do you have any idea where the problem would come from? Thank you

1
You're trying to instantiate the form without passing in any arguments, you need one for $user as it doesn't have a default value - JimL

1 Answers

3
votes

I do not understand what you're trying to do. You do not need to use the constructor to pass parameters to your formType. There is the second parameter of the buildForm method for this ($options).

In your controller, create your form like this :

$form = $this->createForm(ActualiteType::class, $actualite, [
        'admin' => $admin,
        'user'  => $user
    ]);

And modify your formType like that :

class ActualiteType extends AbstractType {

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $admin  = $options['admin']; // Not used ?
        $user   = $options['user'];

        $categories = array(
            'Travaux' => 'Travaux',
            'Voisinage' => 'Voisinage',
        );

        $builder->add('title')
                ->add('category')
                ->add('content')
                ->add('datePublish')
                ->add('category', ChoiceType::class, array(
                    'choices' => $categories
                )
        );

        if ($user->getResidence() != null) {
            $builder->add('residence', EntityType::class, array(
                'class' => 'AppBundle:Residence',
                'choices' => $user->getResidence(),
            ));
        } else {
            $builder->add('residence', 'entity', array(
                'class' => 'AppBundle:Residence',
                'choice_label' => 'name'
            ));
        };
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Actualite',
            'admin'      => null, // Default
            'user'       => null // Default
        ));
    }
}

Do not forget to put the default values of your options in configureOptions method.