0

I have find more answer about this, but return error "The option "prova" does not exist "

I make this in defaultcontroller.php

$form = $this->createForm(new ProvinciaType(), $provincia, array('prova' => 'ciao'));

in ProvinciaType.php

public function buildForm(FormBuilder $builder, array $options)
    {
       $builder->add('name', 'entity', array(
    'class' => 'AcmeIndexBundle:Provincia',
    'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('u')
            ->orderBy('u.name', 'ASC');
    },'empty_value' => $options['prova'] 
));

    }

but not work why?

hakre
  • 193,403
  • 52
  • 435
  • 836
Popolitus
  • 452
  • 1
  • 6
  • 21
  • Because `$options` inside the _lambda_ `function(){}` (also known as _closure_) do not know about it. You can use... `use` with function. though. See usage [on this page, it's the best found](http://stackoverflow.com/questions/6716776/symfony-2-how-to-pass-data-to-formbuilder/7688619#answer-6870684) – renoirb May 23 '12 at 04:47

1 Answers1

5

Simply pass it to the constructor :

$this->createForm(new ProvinciaType($options), $provincia)

And use it in the form :

public function __construct($options) {
    $this->options = $options;
}

Then use this in buildForm :

$options = $this->options;
....
function(EntityRepository $er) use ($options)
    ...
},'empty_value' => $options['prova'] 
....
i.am.michiel
  • 10,281
  • 7
  • 50
  • 86
  • the construct i have put in ProvinciaType.php? – Popolitus Jan 30 '12 at 14:46
  • i need to pass at builderform – Popolitus Jan 30 '12 at 14:48
  • $builder->add('name', 'entity', array( 'class' => 'AcmeIndexBundle:Provincia', 'query_builder' => function(EntityRepository $er) { return $er->createQueryBuilder('u') ->orderBy('u.name', 'ASC'); },'empty_value' => $options['prova'] <------------ – Popolitus Jan 30 '12 at 14:48
  • this say me syntaxs error return $er->createQueryBuilder('u') use ($options) – Popolitus Jan 30 '12 at 15:42
  • I answered this question here http://stackoverflow.com/questions/7643391/symfony2-access-the-container-in-the-repository/7715886#7715886 Maybe it can help you – Reuven Jan 30 '12 at 16:31