【问题标题】:Access variable from FormType in types twig template从类型树枝模板中的 FormType 访问变量
【发布时间】:2014-07-13 00:04:48
【问题描述】:

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

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';                                       
    }                                                                     

}    

在另一种形式 (GameType) 中,我这样使用它:

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

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

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;                                                                       
}   

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

value="{{ asset('data/swf/backendtool.swf') }}?game={{ game.id }}

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

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

【问题讨论】:

  • 你试过{{ form.game.id }}吗?

标签: symfony twig symfony-forms symfony-2.4


【解决方案1】:

您可以通过包装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']
        ));
    }

    /* ... */                                                                   
}

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

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

        <input value="{{ form.vars.game.id }}" />
    {% endspaceless %}
{% endblock %}

【讨论】:

  • 如果不需要合并数组,只想加一个key,也可以$view-&gt;vars['some_key'] = 'Some value';
猜你喜欢
  • 2013-05-11
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
  • 1970-01-01
  • 2016-02-14
相关资源
最近更新 更多