Symfony2: form error displaying - forms

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 %}

Related

Symfony twig get data value from collection object

Hello,
when editing a form with obj-values from a collection in Symfony 4.2, i need each collection-objects ID for frontend stuff.
If I dump a collection object,
{{ dump(form.vars.data) }}
I can see something like this:
CollectionObj1 {#3341 ▼
-id: 21167
-value1: null
-value2: 74
If I now want to access to the id with
{{ dump(form.vars.data.id) }}
I get the following error:
Impossible to access an attribute ("id") on a null variable.
Can somebody tell me, how I can access to the id of the object in the collection?
Thanks very much in advance
Okay, two things I did not mention lead to the error:
My id is not called id. Due to an old database it is called someting like this: K_RESULTS_ID
I am using symfony's collection prototypes: https://symfony.com/doc/current/form/form_collections.html#allowing-new-tags-with-the-prototype and called the dump in the respective form-block
The first error was that you have to call such an id-name without underscores, it should be called like that:
{{ dump(form.vars.data.KRESULTSID) }}
The second one was, that I do have to check, if the object is not null, before fetching it. So I needed to add something like that:
{% if form.vars.data is not null %}
{% set resultId = form.vars.data.KRESULTSID%}
{% else %}
Now it works like expected.
Thanks anyway, #wp78de and sorry for not posting the full truth ;)

How to add DefaultAdress fields in Customer Sylius Form (symfony3)

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} %}

Simple forms post field specific field values

I am using simpleforms as a subscription form for subscribing to classes. The form uses a use_as and also sends the email to the sender as a confirmation.
I have edited the email twig template and added further informatie about what the subscriber should do next.
My question is if it is possible to set a specific field value in the instruction text. So far I have not yet been able to do this correctly. I would like to only show the field "name:" in this instruction text so that every sender that gets this email is greeted with his own name.
The for loop that is used in the email template is
{% for value in form %} {{ value }} {% endfor %}
And i'm looking for a specific field to be set instead of all the values of the form.
You can directly access the fields in your emailtemplate, you don't need to use a loop for it.
So if you have a field called 'name' in your config file you can use {{ form.name }} in the email template to display the value that the visitor has entered directly.

how to display particular fields in form Symfony 2.6

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.

Symfony2 Custom form field for hierarchy in select element

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