Mirage
Mirage

Reputation: 31548

How can i pass extra variable in symfony form builForm Function

THis my code

public function buildForm(FormBuilder $builder, array $options , $task )
    {
        $builder
            ->add('genTasks','text',array('label'=>$task->getName()))

        ;
    }

Is there any way i can access the $task variable inside buildForm

Upvotes: 1

Views: 2091

Answers (1)

gremo
gremo

Reputation: 48899

One solution:

public function buildForm(FormBuilder $builder, array $options)
{
    $task = $options['task'];

    // If you want...
    if(is_null($task)) throw new \LogicException('Task option is required.');

    $builder
        ->add('genTasks', 'text', array('label' => $task->getName()))
    ;
}

public function getDefaultOptions(array $options)
{
    return $options + array('task' => null);
}

And pass your task object as option when you create your form.

Upvotes: 3

Related Questions