Access variable from FormType in types twig template
Asked Answered
C

1

19

I created a custom form type like this:

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

}    

And in another form (GameType) type I use it like this:

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

And inside the controller I want to create the whole form:

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

So basically, all I want to do is to pass-through the Game instance to the PositioningFlashType and inside it's template I want to access this game instance like this:

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

But symfony throws an error, saying that the variable game is not defined.

What is the correct way to pass-through a variable from the controller to a nested FormType?

Cutcheon answered 23/5, 2014 at 14:57 Comment(1)
Have you tried using {{ form.game.id }} ?Electrophorus
T
43

You can add custom view variables by wrapping 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']
        ));
    }

    /* ... */                                                                   
}

You'll now have game under form.vars. Simply override your custom form widget to do whatever you need to do with it.

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

        <input value="{{ form.vars.game.id }}" />
    {% endspaceless %}
{% endblock %}
Telpher answered 24/5, 2014 at 11:18 Comment(1)
If you don't need to merge arrays and just want to add a key, you can also do $view->vars['some_key'] = 'Some value';Bournemouth

© 2022 - 2024 — McMap. All rights reserved.