Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access variable from FormType in types twig template

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?

like image 793
Johannes Klauß Avatar asked May 23 '14 14:05

Johannes Klauß


1 Answers

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 %}
like image 76
kgilden Avatar answered Nov 19 '22 01:11

kgilden