Symfony twig get data value from collection object - forms

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 ;)

Related

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

Symfony2: form error displaying

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

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.

Symfony 2.6.3 {{ form_errors( form ) }} is blank when isValid() returns false

I'm using Symfony V2.6.3
I have a simple form with three fields based on a Type class.
The Entity class that the Type class specifies via setDefaultOptions() has the use Symfony\Component\Validator\Constraints as Assert; statement.
Each field has an #Assert\NotBlank() constraint in the Entity class.
CSRF is enabled.
HTML5 validation is disabled.
If I submit the form with all fields blank, the following happens:
In the controller:
isValid() returns false
getErrors( true ) returns an error for each field
In the template:
{{ form_errors( form ) }} generates no text for the form or any of the fields.
I created a custom form theme and modified {% block form_errors %} to
dump the errors variable. The dump shows the errors property is a FormErrorIterator object with two properties: form, which is, I believe the field definition, and errors which is an empty array.
Oddly enough, drilling down into the form property reveals an errors property that is an array with a single FormError object that contains the error message.
This is not my first time using forms. It has worked just fine for me in the past. Could this be a new bug in 2.6?
I have searched for this and all I found were situations where getErrors() was also returning nothing.
Thanks in advance,
Dave
Okay. I finally found the answer to this question.
Many thanks to thenetimp on the Symfony forum. He had the same problem and figured it out (http://forum.symfony-project.org/viewtopic.php?f=23&t=42841&p=135003&hilit=form_errors#p135003).
Turns out that you have to call createView() after calling isValid(). That actually makes sense when you think about it.
Form errors are bound to a FormType, not to the complete Form. The RootType will only contain the errors that bubbled up from sub types. The sub types will only contain the errors of their type and the ones bubbled up from sub sub types, etc.
Doing {{ form_errors(form) }} will only render the errors of the root form. Assume there was an error on a form.name field, you can get this error by doing: {{ form_errors(form.name) }}.

Symfony2 First form entity overriding second

I have a contact form on a page that has a name field, email field and textarea field. It is made in the most simple way...
$contactEntity = new ContactEntity();
$builder-> $this->container->get('form.factory')->createBuilder('form', $kontaktEntity, array();
$builder->add() ... fields added
Then, in the administrative area, I have a search form that searches for the reacivied messages with name and email fields. I create that form with a different entity with the fields name and email that are one of half a dozen other fields in ContactEntity.
The problem is that the search form is rendered in twig as if it was kontakt form.
To clarify, SearchEntity has fields name and email. ContactEntity also has the same fields but with addition of some other fields. When the search form is rendered in twig, it shows the name and email fields as if it were part of ContactEntity.
Also, twig customization doesn't work. I cannot change label text values, I cannot remove HTML5 validation, and can't do anything beacuse nothing works. I tried renaming the fields in the search entity but it doesn't work.
Twig rendering is made on diffrenet twig scripts and is normal and basic in both both forms. This is on a search form. Changing label taxt to Name doesn't work. The submit value, on the other hand, is correctly rendered.
{{ form_start(form, {attr: {'no-validate' : 'novalidate'}}) }}
<div class="search-rom">
{{ form_label(form.name, 'Name') }}
{{ form_errors(form.name) }}
{{ form_widget(form.name) }}
</div>
<div class="search-rom">
{{ form_label(form.email, 'Email') }}
{{ form_errors(form.email) }}
{{ form_widget(form.email) }}
</div>
{{ form_end(form) }}
To summarize, twig is rendering two of the fields of an entity that the built form has nothing to do with.
EDIT
I've just tested if SearchEntity is filled when form is submitted and it is, with the correct values. I really don't know what is going on here.
EDIT 2
This is a code fragment from the messageAction() method in the controller.
$searchEntity = new SearchEntity();
$builder = $this->container->get('form.factory')->createBuilder('form', $searchEntity, array());
$builder->add('name', 'text')
->add('email', 'text')
->getForm();
Then form is rendered in the response with $form->createView()
Maybe you don't need to pass the builder an entity. In other examples you can just create the entity after getting the data.
How to render a form without a class in other service?
http://symfony.com/doc/current/book/forms.html#using-a-form-without-a-class
I found what the problem is. It's translations.
I'm Croatian and site I'm building has two languages, english and croatian with english as the default locale. Translation is done in various ways, but the translation of forms and all that comes with forms is done via messages.en.php.
For translations, i choose array type. message.en.php looks like this...
... Some data to translate ...
'Ime' : 'Personal name or bussiness'
'Email' : 'Email address
... Some other data to translate
So, when twig tried to render this...
form_label(form.ime, 'Ime')
it rendered the translation for key Ime. Same thing happend to email field. Everything else is in order. I just changed the label value to 'Search for name' or something like that.
As to how to limit translations to only one form, I don't know. I read Translations section but couldn't find anything.