简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM