1

I understand how to customize the twig template for forms per the documenation.

But I do not fully understand which variables are available in each block. I've studied the original twig form template file, but the scope of variables available in each block is not obvious to me yet.

For example, I followed the tutorial to add an asterisk to the form_label block, if it's required. But I do not want the asterisk if it's a hidden field (obviously... but the tutorials leave that part out). I am told hidden fields cannot ever BE required, but my hidden field required value is evaluating true...

This is how my hidden field is defined:

->add('timePublish', 'hidden', array(
    'required' => false
))

This still gets rendered in the HTML:

<span class="required" title="This field is Required">*</span>
<div id="view_version">
    <input type="hidden" id="view_version_timePublish" name="view[version][timePublish]" class="form-control">
</div>

So I don't know why that is happening in the first place. But given that it is, I would like to be explicit and skip the asterisk if it's type = hidden.

The required attribute is available in this block, but not the type of input:

{% block form_label %}
{{ parent() }}
{% if required is not empty and type != "hidden" %}
    <span class="required" title="This field is Required">*</span>
{% endif %}
{{ block('form_help') }}
{% endblock %}

Unfortunately, type is not available, and throws an error:

Variable "type" does not exist in GutensiteCmsBundle:Form:fields.html.twig

I tried other variations like form.type, form.vars.type, form.attr.type, form.vars.attr.type, and attr.type, but none of these work either...

In addition to helping me understand this one variable name, can someone please reference comprehensive documentation for these variables (or give a summary of how to understand it myself), and help me understand how to dump this data for my own debugging in the future?

UPDATE

The hidden input field was not causing the required asterisk to show up. Instead it was the included sub form. I have a primary form type Form/Type/ViewType.php which defines the main form, and includes several sub forms, e.g.

->add('version', new ViewVersionType(), array(
    'label' => false,
    'required' => false
))

Even though I had 'label'=>false, the form_label was still rendering the asterisk. I had to also specify 'required'=>false as well and that got rid of the asterisk. That seems really odd to me, that the included form IS allowed to have a required attribute (or at least that it defaults to true).

Finding the Type

For Symfony 2.4 you can access type like this: {{ form.vars.block_prefixes.1 }}. This will show 'text', 'choice', etc. (reference) It's not the HTML input 'type', but rather the symfony form type, so if you define a custom type (like a nested form) it will show the name of that, e.g. 'viewVersion'

Community
  • 1
  • 1
Chadwick Meyer
  • 7,041
  • 7
  • 44
  • 65

1 Answers1

1

In your use case, I would not worry to much, since the hidden field type is not allowed to have required set to true (see hidden Field Type).

Regarding your question about the variables (from Twig Template Form Function and Variable Reference:

... These variables are made available by the form rendering system. But more importantly, these are the variables that you can override when calling form_label (since in this example, you're rendering the label).

The exact variables available to override depends on which part of the form you're rendering (e.g. label versus widget) and which field you're rendering (e.g. a choice widget has an extra expanded option). ...

On the same site, there is a list of variables, common to all form types (eg. id, name, error, ...).

But to correctly determine all available variables in the current twig context, you can use the answer, posted in this question. Basically you can loop through all available variables with

{% for key, value in _context %}
    {{ key }}
{% endfor %}
Community
  • 1
  • 1
Syjin
  • 2,740
  • 1
  • 27
  • 31
  • Yet, oddly, it is setting a * in a span before the hidden field, which would suggest that `required is not empty`. `
    *
    `
    – Chadwick Meyer May 23 '14 at 15:32
  • @ChadwickMeyer How do you render the form field in your twig? – Syjin May 26 '14 at 14:38
  • Sorry for the slow reply! I got terribly sidetracked from this project by billable work. So I looped through `_context`, BTW, where do you know that `_context` is an available variable? (so much to learn and no clear way to learn it...) Anyway I see all the variables there and `required` is available. But when I do `{% if required is not empty %}` the asterisk appears still on the hidden field (see update to question). – Chadwick Meyer Jun 12 '14 at 21:42
  • Back to the original question: how do I check the `type` of input it is, e.g. 'text', 'hidden', 'select', etc. – Chadwick Meyer Jun 12 '14 at 21:48
  • For the record, the required asterisk was NOT coming from the hidden input, but from the included sub form... which evidently renders a label, even though label=>false. I had to specify required=>false as well e.g. `->add('version', new ViewVersionType(), array('label' => false, 'required' => false))` – Chadwick Meyer Jun 12 '14 at 22:42
  • And for the record, for Symfony 2.4 you can access type like this: {{ form.vars.block_prefixes.1 }}. This will show 'text', 'choice', etc. It's not the HTML 'type', but rather the form type, so if you define a custom type (like a nested form) it will show the name of that, e.g. 'viewVersion'. – Chadwick Meyer Jun 12 '14 at 22:49