Passer un paramètre au construct de mon Form Class
Hello
Avant sous sf2 je faisais ca :
Code:
1 2 3 4 5
|
$search_form = $this->createForm(new SearchType($em), $data, array(
'action' => $this->generateUrl('document'),
'method' => 'GET',
)); |
Maintenant on doit faire ça
Code:
1 2 3 4 5
|
$search_form = $this->createForm(SearchType::class, $data, array(
'action' => $this->generateUrl('document'),
'method' => 'GET',
)); |
Dans la doc il disent de passer par un service (pas cool :-( )
Donc j'ai fait ca
Code:
1 2 3 4 5 6 7
|
services:
app.form.type.search:
class: GenealogieBundle\Form\SearchType
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: form.type } |
Et puis dans mon controller ??
Code:
1 2
|
$search_form = $this->get('app.form.type.search'); |
Mais ça plante :-(
Citation:
Attempted to call an undefined method named "handleRequest" of class "GenealogieBundle\Form\SearchType".
Mon formulaire
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
<?php
class SearchType extends AbstractType
{
protected $em;
function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('commune')->add('submit', 'submit', array(
'label' => 'Rechercher',
))
->add('raz', 'submit', array(
'label' => 'raz.button',
'attr' => array('class' => 'btn-sm btn-success',
'title' => 'Search raz')));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'GenealogieBundle\Entity\Acte'
));
}
} |
Merci