Explicitly print CSRF token field instead of form_rest(form)? - forms

How can I explicitly print CSRF field instead of using {{ form_rest(form) }}?
I need this because I'm going to hide/show some fields based on conditions, however {{ form_rest(form) }} is going to print all of the remaining fields (which is what I'd like to avoid).

It can be done this way:
{{ form_widget(form._token) }}
Also you might want to consider adding fields conditionally in your form type instead of making that kind of decisions in a template.

Related

Grafana Alert Templating

I am struggling to include values into my alerting template
(Using Grafana 9.03 with classic condition alerting)
Query
Alert Evaluation
I tried to use {{ $values.B0 }} which works but only displaying the value. I want to use the Tag "Limit" for example "ImportStatNSC4903"
I did some more tries with:
{{ $values.B0.Limit }}
{{ $values.B0.labels }}
{{ $labels }}
Any ideas what I am doing wrong?
I am struggling to include values into my alerting template. I want a summery that tells me clearly why this alert was triggered
For example:
Limit ImportStatNSC4903 is outside the defined Boundaries

form_widget : Add classes to existing classes

I want to add classes to existing classes with twig, when invoking the form_widget()-function, so I'd like to merge them. But when I use
{{ form_widget(form.field1), {'attr': {'class': 'foo'}} }}
on an object that already has some classes attached to it by the controller, it doesn't add the class(es), but replaces them. According to the documantation this would
render a widget, but add a "foo" class to it
But the formulation isn't clear about, if the classes are really "added" or if they are replaced.
It seems that I would need to have a way to access the existing attributes/classes and merge them together with the ones I want to add. How can I accomplish this?
You might use form.field1.vars.attr.class and something like str1 ~ str2 to merge the already given classes and your new ones.
Putting it all together you get:
{{ form_widget(form.field1), {'attr': {'class': form.field1.vars.attr.class ~ foo'}} }}
this works and solves your problem. Some background:
According to the symfony documentation form.field1.vars.attr is a key based array, with a class-key.
So you can access all existing classes in this way.

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 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.

Symfony 2, double error

Why i have double error when i want to customize my form ?
Template:
<div>
{{ form_errors(edit_form.wiek) }}
{{ form_widget(edit_form.wiek) }}
</div>
HTML:
You probably see the error on the field level, but also on the form level. It can look like you have two errors especially if there's only one field in your form.
Dig into the customizing error output of the form cookbook for more details.
Edit
The doc gives a much better explanation :)
You can also customize the error output for just one specific field type. For example, certain errors that are more global to your form (i.e. not specific to just one field) are rendered separately, usually at the top of your form.