Caryl Chessman
Caryl Chessman

Reputation: 3

pass variable in symfony form builForm Function

I have tried this code in my controller

$form = $this->createForm(new CentrexEdit2Type($ids), $centrex);

I want now to add the ids form to my Form Builder:

     public function buildForm(FormBuilderInterface $builder, array $options)
        {
          $ids = $options['ids']; 
          .......

I failed on it Any ideas please?

Here I am joining the code:

   if ($step==2){
        $form = $this->createForm(new CentrexEdit1Type(), $centrex);
        $ids=$form->get('bases')->getData();
        }

        foreach($ids as $id){echo($id->getId());}
        if ($step==3){
         $form = $this->createForm(new CentrexEdit2Type(array('ids'=>$ids)), $centrex);
        }

I need $ids when $step==3, this is the problem.

Upvotes: 0

Views: 1404

Answers (2)

Bernat
Bernat

Reputation: 1556

what I do is create the form this way:

$form = $this->createForm(new TheForm($anArray), $client);

where $anArray is an array.

Then, in the form I do something like:

public $anArray;

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

after this, inside the form class I access the array with $this->anArray

Upvotes: 1

DonCallisto
DonCallisto

Reputation: 29912

Try with

$form = $this->createForm(new CentrexEdit2Type(array('ids'=>$ids)), $centrex);

in your controller

and modify your form with

protected $ids;
public function __construct(array $parameters=Null)
{
  if($parameters)
  {
    if(array_key_exists('ids',$parameters)) $this->ids = $parameters['ids'];   
  }
}

last step

public function buildForm(FormBuilderInterface $builder, array $options)
{
  $ids = $this->ids; 
  [...] 
}

Upvotes: 0

Related Questions