How to concatenate two Twig variables in a loop - php

I'm trying to create dynamically variable name so the result to be as the one below
{{ form_label(form.user_1) }}
{{ form_label(form.user_2) }}
{{ form_label(form.user_3) }}
{{ form_label(form.user_4) }}
Here's what I tried so far
{% for user in users %}
{{ form_label(form.user~'_'~loop.index) }}
{% endfor %}
but get
Argument 1 passed to
Symfony\Component\Form\FormRenderer::searchAndRenderBlock() must be an
instance of Symfony\Component\Form\FormView, string given
What I'm doing wrong?

I think that you have to use the attribute() function.
If this doesn't work with the concatenation in the method parameter, try to concatenate it first in a variable like this :
{% set userIndex = 'user_' ~ loop.index %}
And then you should try this :
{{ form_label(attribute(form, userIndex)) }}

Related

Symfony Assert doesnt working with form

I used Assert to validate fields like
#Assert\NotBlank(message="this field cannot be empty")
$private title;
#Assert\NotBlank(message="this field cannot be empty")
$private description;
#Assert\NotBlank(message="this field cannot be empty")
$private price;
Now, when Im using form in html.twig
{% body block %}
{{ form(form, {"attr": {"novalidate": "novalidate"}}) }}
{% endblock %}
everything is allright, if I have an empty field I got my message, but when Im trying to divide this form like
{% body block %}
{{ form_start(form, {"attr": {"novalidate": "novalidate"}}) }}
{{ form_widget(form.title) }}
{{ form_widget(form.description) }}
{{ form_widget(form.price) }}
{{ form_rest(form) }}
{{ form_end(form, {"attr": {"novalidate": "novalidate"}}) }}
{% endblock %}
I'm getting something like default message that I cannot add an advert, but there are no messages next to my fields. What am I doing wrong?
I've tried use novalidate attribute in every form field but it still doesn't working
You should use {{ form_row(form.title) }} instead {{ form_widget(form.title) }} and so on. Alternatively add {{ form_error(form.title) }} to every {{ form_widget(form.title) }} and so on.
Explanation: form_widget render only form control ie. input box or drop-down. form_row render: form_label - the title of field, form_widget - the control, form_error if needed - the errors attached to field. It also wrap everything on nice div to group related parts.

Is it possible to set/insert a new variable in $_GET | $_POST | $_SESSION | $_COOKIE from TWIG?

I have a requirement which which might require me to set a new variable in TWIG for
{{ app.request.query }} i.e. ($_GET)
{{ app.request.request }} i.e. ($_POST)
{{ app.request.session }} i.e. ($_SESSION)
{{ app.request.cookies }} i.e. ($_COOKIE)
e.g. {% app.request.query.set('myvariable':'value' %} ...
If you want to set the variable while in TWIG here is what I know:
For $_POST variables use this :
{{ app.request.request.add(['var1', 'data1']) }}
{{ app.request.request.get(0) }}
{{ app.request.request.get(1) }}
For $_GET variables use this :
{{ app.request.query.add(['var2', 'data2']) }}
{{ app.request.query.get(0) }}
{{ app.request.query.get(1) }}
For $_COOKIE variables use this :
{{ app.request.cookies.add(['var3' , 'data3']) }}
{{ app.request.cookies.get(0) }}
{{ app.request.cookies.get(1) }}
For $_SESSION variables use this :
{{ app.session.set('var4', 'data4') }}
{{ app.session.get('var4') }} <!-- shows 'data4 -->
Or
{{ app.request.session.set('var4', 'data4') }}
{{ app.request.session.get('var4') }} # shows 'data4
You can easy append element to the array before calling Twig->render in PHP.
Simply write
$_GET['myvariable'] = $value

twig variable inside a function

I am having trouble working around with twig.
I pass a $labelz var on twig, and this $labelz is an array of the form label[0][left], label[1][left], ... label[2][left] so on.
This array, $labelz is actually a _POST var, so when I do some "validation", i want to echo the values stored on this array again back to the form. Hence, I loop it on twig. I can successfully echo the stored value back in a input form using
{% for key, label in labelz %}
<input type="text" name="label[{{ key }}][left]" value="{{ labelz[key]['left'] }}">
{% endfor %}
Now, here is my problem, I am using this function
{% if errors.has('VARIABLE') %}
{{ errors.first('VARIABLE') }}
{% endif %}">
to check if there's an error passed to the page (using errors.has()), then echo the error message if it has (using errors.first()) . Now, with those function I want to do the same to $labelz so I tried using this:
{% for key, label in labelz %}
{% if errors.has(labelz[key]['left']) %}
{{ errors.first(labelz[key]['left']) }}
{% endif %}`
{% endfor %}
but it fails, because as you can see, and note key is a forloop variable of twig, and i have no idea how to make this work. Please help thanks!
turns our ~ solved my problem, it allows me to concatenate string on twig.
Thanks!

Symfony2 Twig Change / Replace Value

I'm new here. I'm also new in working with Symfony2.5.
I want to replace a value with another, better would be to change it via array.
It's hard to explain - here are some code parts:
Recources/Views/register.html.twig:
{% for user in list_user %}
{{ user.Id }}
{{ user.isAdmin }}
{{ user.isActive }}
{% endfor %}
Here I generate the array to send to register.html.twig which looks like this:
Controller/AccountController.php
$user = $this->getDoctrine()
->getRepository('SeotoolMainBundle:User')
->findAll();
return $this->render(
'SeotoolMainBundle:Account:register.html.twig',
array('form' => $form->createView(), 'list_user' => $user)
);
It correctly outputs for isAdmin and isActive 0 or 1. Now I want to replace this output with for example isActive = 1 should Output "Active", isActive = 0 shut Output "Not active".
I hope you understand what I mean and can help me find the correct way to do this.
Thank you guys,
kind regards,
Marvin
Can't you just use an "if"?
{% for user in list_user %}
{{ user.Id }}
{{ user.isAdmin }}
{% if user.isActive %}
Active
{% else %}
Inactive
{% endif %}
{% endfor %}
You can also use the ternary operator, which is more compact.
{% for user in list_user %}
{{ user.Id }}
{{ user.isAdmin }}
{{ user.isActive ? 'Active' : 'Inactive' }}
{% endfor %}

Use Twig variable as a property name of an other Twig variable

I must use a Twig variable as a property for another Twig variable.
In a for loop, I get the properties of a specific entity and want to use those properties to get the property-content for an entity variable in an other for loop.
Some code to make this clear:
{% for entity in entities %}
{{entity.foo}}, {{entity.bar}}<br />
{% for property in specialdynamicproperties %}
{{entity.property}} <!-- property has the content foobar for example, I want to use it as the property accessor for entity -->
{% endfor %}
{% endfor %}
Thanks.
The attribute function is what you are looking for.
Edit:
{{ attribute(object, method) }}
{{ attribute(object, method, arguments) }}
{{ attribute(array, item) }}
{% for object in objects %}
{% for column in columns %}
{{ attribute(object, column) }} {# equivalent to php $object[$column] #}
{% endfor %}
{% endfor %}
Using Twig Attribute Function (Twig > 1.2)

Categories