hous hous - 5 months ago 14
PHP Question

Parameter from controller to form has alwyas null value

I'd like to pass a parameter from the controller to the form but the parameter has alwyas null value.

This is the Controller

$form = $this->createForm(BaseType::class, $entity, array('nom' => 'Informatique'));

and this is the FormType

->add('departement', EntityType::class, array(
'class'=> 'AppBundle\Entity\Departement',
'choice_label' => 'nom',
'multiple' => false,
'expanded' => false,
'query_builder' => function(DepartementRepository $er ) use($options) {
return $er->createQueryBuilder('d')
->where('d.nom like :nom')
->setParameter('nom', '%' .$options['nom']. '%')

public function configureOptions(OptionsResolver $resolver)
'data_class' => 'AppBundle\Entity\Formation',
'nom' => null,

output of var_dump($options['nom']); is null


The third parameter of createForm is the array $options that can contain.. anything you want.

So in your controller:

public function yourAction()
    $form = $this->createForm(
        ['yourCustomOption' => 'value']

And you can access or set a default value in your FormType:

public function buildForm(FormBuilderInterface $builder, array $options)
   $yourCustomOption = $options['yourCustomOption']; //will be 'value'

public function configureOptions(OptionsResolver $resolver)
        'yourCustomOption' => 'Default value',