从类型twig模板中的FormType访问变量

Joh*_*auß 12 symfony-forms symfony twig symfony-2.4

我创建了一个这样的自定义表单类型:

class PositioningFlashType extends AbstractType                           
{                                                                         
    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    {                                                                     
        $resolver->setDefaults(array(                                     
            'game' => new Game()                                          
        ));                                                               
    }                                                                     

    public function getParent()                                           
    {                                                                     
        return 'form';                                                    
    }                                                                     

    /**                                                                   
     * Returns the name of this type.                                     
     *                                                                    
     * @return string The name of this type                               
     */                                                                   
    public function getName()                                             
    {                                                                     
        return 'positioning_flash';                                       
    }                                                                     

}    
Run Code Online (Sandbox Code Playgroud)

在另一种form(GameType)类型中,我使用它:

$builder                                                 
    ->add('flash', new PositioningFlashType(), array(    
        'mapped' => false,                               
        'game' => $options['game']                       
    ))   
Run Code Online (Sandbox Code Playgroud)

在控制器内部我想创建整个表单:

private function createEditForm(Game $entity)                                           
{                                                                                       
    $form = $this->createForm(new GameType(), $entity, array(                           
        'action' => $this->generateUrl('game_update', array('id' => $entity->getId())), 
        'method' => 'PUT',                                                              
        'edit' => true,                                                                 
        'game' => $entity                                                               
    ));                                                                                 

    $form->add('submit', 'submit', array('label' => 'Speichern'));                      

    return $form;                                                                       
}   
Run Code Online (Sandbox Code Playgroud)

所以基本上,我想做的就是将Game实例传递给PositioningFlashType它的模板,我想要访问这个game实例,如下所示:

value="{{ asset('data/swf/backendtool.swf') }}?game={{ game.id }}
Run Code Online (Sandbox Code Playgroud)

但symfony抛出一个错误,说game没有定义变量.

将变量从控制器传递给嵌套FormType的正确方法是什么?

gil*_*den 28

您可以通过换行添加自定义视图变量buildView().

/* ... */

use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

/* ... */

class PositioningFlashType extends AbstractType                           
{                                                                         
    /* ... */

    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        parent::buildView($view, $form, $options);

        $view->vars = array_merge($view->vars, array(
            'game' => $options['game']
        ));
    }

    /* ... */                                                                   
}
Run Code Online (Sandbox Code Playgroud)

您现在有gameform.vars.只需覆盖您的自定义表单小部件即可执行您需要执行的任何操作.

{% block positioning_flash_widget %}
    {% spaceless %}
        {# ... #}

        <input value="{{ form.vars.game.id }}" />
    {% endspaceless %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)