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'