I created a custom form field extending de entity field for display a select with hierarchy of destinations. Table destination is a nested tree based on gedmo Nested Tree behavior. It's works fine but i need to show the hierarchy in select options based on tree level. For example:
Destination1
--Subdestination
--Subdestination
Destination2
--Subdestination
--Subdestination
----Subdestination
I build the tree correctly in custom form field class but i dont know how render properly the options for this select type. I was thinking via form theming override the block {% block choice_widget_options %} but this affect all selects.
How can i do that or there are a best way to do it?
you could try to implement __toString() function on your entity so ti displays proper number of "---" before label
check vendor\symfony\symfony\src\Symfony\Bridge\Twig\Resources\views\Form\form_div_layout.html.twig to see how it workds and override the {% block choice_widget_expanded %} block or whichever type of list you use, you need to add {% form_theme form _self %} in the .twig file where your form is so the twig will search for overriden blocks in the same file
Related
I think the title is not very clear, so I will explain better :
I have edited the CustomerRegister form the add a few fields, and I would like to add DefaultAdress fields too (defaultAdress is a linked object to Customer - I have a getDefaultAdresse method in CustomerObject). I would like to add all the fields that are in the DefaultAddress object (street, country, etc.)
I don't know how to do that...
Do I need to modify the CustomerRegistrationTypeExtension to add fields for the address ?
How can I call the fields in my twig file ? Like that : {{ form_row(form.defaultAddress.street) }} ?
I didn't find doc to explain that case.
Thanks for your help !
You should create CustomerRegistrationTypeExtension, just like you described. Inside of this extension, simply do:
use Sylius\Bundle\AddressingBundle\Form\Type\AddressType;
$builder->add('defaultAddress', AddressType::class)
Then override the template and you should be able to render the fields or entire form with:
{{ form_widget(form.defaultAddress) }}
I'd recommend using our standard address form template, by simply including it:
{% include '#SyliusShop/Common/Form/_address.html.twig' with {'form': form.defaultAddress} %}
I have little more complex problem, i have never done more than 1 nested form in an a form, which was fine to just include a custom collection widget template. (Reason: I need to re-code website based on laravel and Spaghetti code, with Spaghetti structure with tons of bugs and buggy patches, what i am fixing for a year now, and i have decided to recreate this app on my loved Symfony).
Let me write the structure:
Questionnaire (short name QN for later use)
- QN -
|_ QN Sections -
|_ QN Parts -
|_ QN Groups -
|_ QN Questions -
|_ some predefined answers
As you can see each QN can have Sections, these sections can have parts, these parts can have groups, and these groups can have questions, these questions have some conditional fields like if question type is "A,B,C" it has another nested collection form for these predefined answers,...
Problem is that for each of these collection form, i need to specify somehow a custom collection wiget template, because each of these collection form has different layout of fields, how they are displayed and handled.
Currently i have created a collection widget template that i include before QN Sections to style QN Sections, but this template is used also for his child collection form, what i need to avoid.
In a perfect world it should work like "prototype_template" => "some path", and then create javascript for each tempalte to add, remove, etc.
Here is an screenshot with some information how it looks now:
In final stage using these separate templates for each collection form, the "form builder" should look like actual web app.
If you need more informations please let me know, i will provide as much informations as i can.
To recap: I need somehow to specify for each nested collection form type to use different collection form widget template.
Thank you
Finnaly I got it.
The main collection template i have used was collection_widget block.
That was bad because it was too general. I hav renamed it to _questionnaire_questionnaireSections_widget, so the 1. level collection were themed. But after day of headake, 1000 combinations of form names i have got how to name the 2. level collection, its block name is "_questionnaire_questionnaireSections_entry_questionnaireParts_widget" what simply stand for "_mainFormTypeName_fieldNameOfFirstCLevelCollection_entry_FieldNameOfSecondLevelCollection_widget"
Than i just needed to place each template file to right place at the right moment.
edit.html.twig
{{ form_start(form) }}
{% form_theme form "::templates/collection/questionnaire/questionnaireSection.html.twig" %}
.........
{{ form_widget(form.questionnaireSections) }}
questionnaireSection.html.twig
{% block _questionnaire_questionnaireSections_widget %}
.............
{% for rows in form %}
{% form_theme form "::templates/collection/questionnaire/questionnairePart.html.twig" %}
questionnairePart.html.twig
{% block _questionnaire_questionnaireSections_entry_questionnaireParts_widget %}
So problem is solved, i have successfully themed nested collections. Thanks to myself and to symfony docs that has 0 words documentation to nested form theming, but they has some information about the "entry " part.
I need to validate field in a Symfony2 form. It's OK, but the error is always displayed next to the related fields.
In case of any error, I also would like to display a message at the form of the form which would say 'Carreful, one or more fields are not valid, please look below'.
Is it possible ? Should I use a custom constraint to add a violation ?
Supposing your form name is form, you can do the following in your view (twig)
{% if not form.vars.valid %}
Carreful, one or more fields are not valid, please look below
{% endif %}
I need display particular fields in form in Symfony 2.6. I use a class Form. I have the folowing fields: name, email, message, send, recet. I need display all of them except recet .I try like this:
{{form_start(form)}}
{{ form_errors(form) }}
{{form_row(form.name)}}
{{form_row(form.email)}}
{{form_row(form.message)}}
{{form_end(form)}}
But, it's displaying all fields in form, it is not what I want. Even if i leave only {{form_start(form)}} and {{form_end(form)}} - its display all fields. Can someone help me wit this problem?
remove form_end, just close form with HTML
</form>
but then you must handle CSRF token generation by adding:
{{ form_widget(form._token) }}
{{ form_widget(form._token) }}
or try setting field you do not want to show using:
{% do form.recet.setRendered %}
but probably best way is not to add this field in the first place, rather than hiding it, by form options or event listeners depending on some criterias
A better solution is to remove the field from the form type. If you only remove it from the view, it may be interpreted as a blank submission for that field and delete existing data.
If you only use the form in one place, then just remove the field from the type. If you use it in multiple places, then you can selectively remove the field in a FormEvents::POST_SET_DATA event listener.
I have actions "New" and "Edit". I want to reuse the same template in both actions. The problem is, when I'm creating a new entity, I want to show "New entity" in the page title. When I'm editing an entity I want to "Editing entity title".
I could pass a variable in each action indicating the action but I don't feel right doing it ... Are there any way to detect if it's a creation or edition in twig?
How would you solve this common issue?
you can pass the entity to the view and create a variable in twig
{% set isNew = not entity.id > 0 %}
easy?
if you want to pass only the form to the view you can get the entity directly from the form
{% set entity = form.get('value') %}