Reputation: 9552
I have a Symfony form that includes two TextType
fields. If a certain check evaluates to false
, I don't want to display the input
fields but output the static content of the field and include the form fields as hidden
fields instead. How can I do that?
Upvotes: 9
Views: 44297
Reputation: 1
If you use Symfony 4.4, you can set the `div style="display: none;" style:
{{ form_start(form) }}
<div class="row">
{% if app.user %}
<div class="col-lg-12">
<fieldset>
Login by: {{ app.user.username }}
</fieldset>
</div>
<div class="col-lg-12">
<fieldset>
Mail : {{ app.user.email }}
</fieldset>
</div>
<div class="col-md-12 col-sm-12" style="display: none;">
<fieldset>
{{ form_widget(form.owner, { 'attr': {'class': '', value: 'null'}}) }}
</fieldset>
</div>
{% else %}
<div class="col-md-6 col-sm-12">
<fieldset>
{{ form_widget(form.owner, {'attr': {'class': ''}}) }}
</fieldset>
</div>
{% endif %}
<div class="col-lg-12">
<fieldset>
{{ form_widget(form.content, {'attr': {'class': ''}}) }}
</fieldset>
</div>
<div class="col-lg-12">
<fieldset>
<button type="submit" class="main-button">New</button>
</fieldset>
</div>
</div>
{{ form_end(form) }}
Upvotes: -1
Reputation: 983
You can prevent any output for the form field by pretending, that it was already rendered:
{{ form_start(form) }}
{% if someValue == true %}
{% do form.fieldName.setRendered() %}
{% endif %}
{{ form_end(form) }}
Upvotes: 20
Reputation: 583
you can use HiddenType
,
or hide field in template:
{{ form_start(form) }}
{% if someValue == true %}
{{ form_widget(form.fieldName) }}
{% else %}
{{ form_widget(form.fieldName, { 'attr': {'class': 'hidden-row'} }) }}
{% endif %}
{# other fields... #}
{{ form_end(form) }}
or you can use FormEvents like FormEvents::PRE_SET_DATA
in FormType.
(doc)
Upvotes: 16