9

I want to get all the variables available in the Symfony form theme file form_div_layout.html.twig, I read the Symfony official documention and searched on the web, but couldn't find any useful information on this, can someone help me?

imikay
  • 742
  • 1
  • 8
  • 17

4 Answers4

16

Well, you can get all the available variables in each block by iterating the context:

{% block form_widget_simple %}
<ol>
    {% for key, value in _context %}
    <li>{{ key }}</li>
    {% endfor %}
</ol>
{% spaceless %}
    {% set type = type|default('text') %}
    <input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
{% endspaceless %}
{% endblock form_widget_simple %}

And if you want to use yours, then you'll have to overwrite the classes that actually are rendering those widgets, just take a look at AbtractType::buildView...

As @Gregoire suggested, you can use {{ dump(_context) }} from version 1.5 (http://twig.sensiolabs.org/doc/functions/dump.html), but be aware that it will print a big amount of info.

coma
  • 16,429
  • 4
  • 51
  • 76
  • 1
    You can also add a `{{ dump(value) }}` in the loop, to display the content of the variables. – Gregoire Sep 04 '12 at 09:26
  • @Gregoire In Symfony 2.5 `{{ dump(value) }}` makes the entire page go white (known memory leak evidently) in cases where the value is an object (which renders the dump useless since that is when I usually need the dump). – Chadwick Meyer Oct 03 '14 at 20:16
7

I hit the same problem recently, being the lack of documentation on the available variables (attributes) when working in themes. In the end I found my solution by searching through the vendor folder (took a while) for the variables I did know, to see what else is available.

The best place for me was to look in here: Symfony\Component\Form\Extension\Core\Type

The base type, being FieldType provides these variables via buildView

    $view
        ->set('form', $view)
        ->set('id', $id)
        ->set('name', $name)
        ->set('full_name', $fullName)
        ->set('errors', $form->getErrors())
        ->set('value', $form->getClientData())
        ->set('read_only', $form->isReadOnly())
        ->set('required', $form->isRequired())
        ->set('max_length', $form->getAttribute('max_length'))
        ->set('pattern', $form->getAttribute('pattern'))
        ->set('size', null)
        ->set('label', $form->getAttribute('label'))
        ->set('multipart', false)
        ->set('attr', $form->getAttribute('attr'))
        ->set('types', $types)
    ;

prototype is an attribute that only exists in the collection type, as is allow_add and allow_delete, see CollectionType in the same folder.

After the base FieldType, this appears to be the complete list.

CheckboxType.php:        ->setAttribute('value', $options['value'])
ChoiceType.php:          ->setAttribute('choice_list', $options['choice_list'])
ChoiceType.php:          ->setAttribute('preferred_choices', $options['preferred_choices'])
ChoiceType.php:          ->setAttribute('multiple', $options['multiple'])
ChoiceType.php:          ->setAttribute('expanded', $options['expanded'])
ChoiceType.php:          ->setAttribute('required', $options['required'])
ChoiceType.php:          ->setAttribute('empty_value', $emptyValue)
CollectionType.php:      ->setAttribute('prototype', $prototype->getForm());
CollectionType.php:      ->setAttribute('allow_add', $options['allow_add'])
CollectionType.php:      ->setAttribute('allow_delete', $options['allow_delete'])
DateTimeType.php:        ->setAttribute('widget', $options['widget']);
DateType.php:            ->setAttribute('formatter', $formatter)
DateType.php:            ->setAttribute('widget', $options['widget']);
FormType.php:            ->setAttribute('virtual', $options['virtual'])
MoneyType.php:           ->setAttribute('currency', $options['currency'])
PasswordType.php:        ->setAttribute('always_empty', $options['always_empty']);
RadioType.php:           ->setAttribute('value', $options['value'])
TimeType.php:            ->setAttribute('widget', $options['widget'])
TimeType.php:            ->setAttribute('with_seconds', $options['with_seconds'])
MadManMonty
  • 816
  • 1
  • 7
  • 25
  • Thanks, I will have a try and see how I can use them. – imikay Mar 01 '12 at 17:42
  • In `collectionType`, how can we iterate each input field included in the collection? And do you know how to access parent elements or parts of the entire form? [See this question](http://stackoverflow.com/questions/26184990/set-the-input-labels-for-each-field-as-headers-in-symfony-collection-widget) – Chadwick Meyer Oct 03 '14 at 20:21
2

See my answer here: https://stackoverflow.com/a/41020474/5758328

You simply need to use

{% dump %}

and all variables available in the template will be dumped to the profiler

Community
  • 1
  • 1
Mawcel
  • 1,967
  • 15
  • 22
1

You can pull all of the ones out of the original file, and only overload the ones that you need:

vendor/symfony/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
chasen
  • 456
  • 5
  • 16
  • Actually, the problem is that I have a form with a collection, I want the widgets in the collection to use another theme that different with the parent form, I lost my mind on how to archive this. – imikay Feb 28 '12 at 17:13
  • I tried to create a custom theme for the custom field, but the widgets reference with each other, I tried to follow the chain to override each one but I am lost. – imikay Feb 28 '12 at 17:26
  • can you paste what you have so far, and ill see if I can help you with it – chasen Feb 28 '12 at 23:14
  • This is what I mean, https://github.com/symfony/symfony-docs/issues/696, and someone created a theme for the embedded form, http://pastie.org/2522598, this is exactly what I'm looking for. – imikay Feb 29 '12 at 01:26
  • I see he used the variables 'prototype', 'attr', is there any others I can use? What should I know if I want to create a theme like this? And the 'form' is traversable, what's the content of it? – imikay Feb 29 '12 at 01:39