Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection Form Label #11819

Closed
Vinorcola opened this issue Sep 1, 2014 · 6 comments
Closed

Collection Form Label #11819

Vinorcola opened this issue Sep 1, 2014 · 6 comments
Labels

Comments

@Vinorcola
Copy link

Hi all,

After looking few different solutions, I didn't find what I wanted. It seams there is no simple way to customize the label of a form rendering into a collection (see the image attached). It should be a great idea to add such a functionnality to the form.
sf2_form_collection_problem

As you can see, new inner form label are set right through javascript, but there is no way of configuring the label of pre-set data when editing the collection.

@stof
Copy link
Member

stof commented Sep 1, 2014

This is possible through the form theming, using field-specific overrides for the label of the collection entry:

{% form_theme form 'quiz_form.html.twig' %}

{{ form_widget(form) }}
{# quiz_form.html.twig #}
{% block _myquiz_answers_entry_label %}
    {{ form_label(form, 'Answer ' ~ (form.name + 1)) }}
{% endblock %}

@Vinorcola
Copy link
Author

Ok I see.

But I don't see what "myquizz_answer_entry" represent here. When I put "_question_form_availableAnswers_label", it changes the label of the collection, not the label of each form in the collection (i.e. the "Answers available" label in the picture)

EDIT: Ah ok, you mean the "entry" part ;) It worked now. Thanks.
EDIT2: Except for "form.name". I have got an exception saying that name property doesn't exists.
EDIT3: Ok, it is form.vars.name instead

@Vinorcola
Copy link
Author

The thing now is that this also replace the "name__label" string in the prototype by "Answer 1", so the new answer added by javascript always have the "Answer 1" label ^^

@stof
Copy link
Member

stof commented Sep 1, 2014

@Vinorcola in my exampke, myquiz_answers is the block name for the collection field itself, assuming the root form is named myquiz and it has a answers field for the collection. I could not use the real names as they were not in your comment.

for the prototype, it would indeed not work ('__name__' + 1 will return 1 as my code was assuming integer names. Here is a fixed version:

{# quiz_form.html.twig #}
{% block _myquiz_answers_entry_label %}
    {%- if label != '__name__label' -%}
        {%- set label = 'Answer ' ~ (name + 1) -%}
    {%- endif -%}
    {{- form_label(form, label) -}}
{% endblock %}

@Vinorcola
Copy link
Author

Thanks stof.

@fabpot fabpot added the Form label Sep 5, 2014
@webmozart
Copy link
Contributor

This seems to be solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants