0

I work with a symfony embedded form to manage my translated fields for a given entity. My principal entity has a boolean field and I have multiple text fields for each translation.

I do not use the translatable doctrine extension and I do not want to use it.

In my FormType, I use a CollectionType to embed the translated fields in my form, and in the template, I use the form theme to customize the HTML.

This is my problem : I would like to group fields in my form to optimize UX but once I call the form_widget on my field, I cannot use it a second time. I would like to show 2 fields for a given language and then another field for another language further in the form. How can I solve my problem ?

This is a twig example that illustrate my problem.

{% form_theme form _self %}

{% block _service_translations_entry_widget %}
<div>
    {% if name == 0 %}
    <div class="s12 m6 l6">
        {{ form_label(form.title) }}
        {{ form_errors(form.title) }}
        {{ form_widget(form.title) }}
    </div>

    <div class="s12 m6 l6">
        {{ form_label(form.subtitle) }}
        {{ form_errors(form.subtitle) }}
        {{ form_widget(form.subtitle) }}
    </div>
    {% endif %}

    {% if name == 1 %}
    <div class="s12 m12 l12">
        {{ form_label(form.desc) }}
        {{ form_errors(form.desc) }}
        {{ form_widget(form.desc) }}
    </div>
    {% endif %}
</div>
{% endblock %}

{% block body %}

{{ form_start() }}
{{ form_errors(form) }} 

<div>
    <div class="s12 m12 l12">
        {{ form_label(form.doubleBlock) }}
        {{ form_errors(form.doubleBlock) }}
        {{ form_widget(form.doubleBlock) }}
    </div>
</div>

<div id="block-1">
    {{ form_widget(form.translations) }}
</div>

<div>
    <div class="s12 m6 l6">
        {{ form_label(form.activedStyle) }}
        {{ form_errors(form.activedStyle) }}
        {{ form_widget(form.activedStyle) }}
    </div>

    <div class="s12 m6 l6">
        {{ form_label(form.checkoutOption) }}
        {{ form_errors(form.checkoutOption) }}
        {{ form_widget(form.checkoutOption) }}
    </div>
</div>

<div id="block-2">
    {{ form_widget(form.translations) }}
</div>

{{ form_widget(form.save) }}

{{ form_rest(form) }}
{{ form_end(form) }}

{% endblock body %}

1 Answer 1

0

As you say, you can’t reuse a field.

My suggested approach would be to add an additional field to the form, translations_extra, which either isn’t mapped to or persisted by the entity. Extract the data in the controller on form submission and then add it to the translations collection before persisting.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.