How to set the value of textarea in symfony - symfony

I am getting a url variable via GET on my controller and want to set it as value of my textarea. Here's my textarea line:
{{ form.myTextArea is defined ? form_row(form.myTextArea) }}

{{ form_row(form.myTextArea, {'value': 'your text'}) }}

Related

Is it possible to pass twig variables into templates?

I'm using the Timber plugin for my Wordpress site. For a little button component, i'm trying to pass the variable {{ site.link }} to use as my href.
Like so:
{% include 'component/icon-button.twig' with { href: '{{ site.theme.link }}/faq' } %}
icon-button.twig
<button class="icon-button" data-href="{{ href }}">text</button>
But that only results in {{ site.link }}/faq being output as is, as a string, not the actual URL.
How would i do this?
That's just because when you use include in twig you don't need to use double {{ again. The correct syntax would be:
{% include 'component/icon-button.twig' with { href: site.theme.link ~ '/faq' } %}
The "~" is used for string concatenation in twig. You were passing the whole thing as a string and nothing more =)

How to change template of controller rendered in twig file (Symfony 3)?

I know that we can render the content of a controller in twig file like this:
{{ render(controller('FOSUserBundle:Security:login',{"baseTemplate": true})) }}
However, I don't know if we can pass the new template so that the controller will use it instead of the default. Anyone tried to override template in this way?
I don't really understand the issue here
If you do
{{ render(controller('FOSUserBundle:Security:login',{"baseTemplate": true})) }}
You could aswell do:
{{ render(controller('FOSUserBundle:Security:login',{"template": "your_template.html.twig"})) }}
Or
{{ render(controller('FOSUserBundle:Security:login',{"templateNumber": "4"})) }}
Where templateNumber is used in a condition inside your controller ?

edit form with some disable field

i have a form with some field and i want user edit only some field and the other field must be only visible .
In my controller i have the form
$element = $this->getDoctrine()->getRepository('AppBundle:Element')->find($id_element);
$form = $this->createForm(ElementType::class, $element, array('user' => $user));
code here`$form->handleRequest($request);
Now in my twig file
{{ form_start(form) }}
{{ form_widget(form.element,{ 'attr':{'disabled':'disabled'}}) }}
In this way when i click submit button say not focusable field so i add
{{ form_widget(form.element,{ 'attr':{'disabled':'disabled'}}) }}
but when i save the field element in database become null
How i can do it ??
Use readonly attribute for it
{{ form_widget(form.element,{ 'attr':{'readonly':'1'}}) }}
Values of disabled controls are not submitted with a form.
https://www.w3.org/TR/PR-html40-971107/interact/forms.html#h-17.7

Translate the value of twig variable

Is it possible to translate the value of twig variables in a template with the 'trans' tag?
Say for instance I am passing a product to my template. This product has a definition with a trans tag e.g {{ product.definition|trans }}. This definition could either be in EN or DE or some other language. How could I translate the definition.
What are you trying to do is not a good way, It would look like this:
messages.en.yml
product:
definition:
some_value1: Some value 1
some_value2: Some value 2
and in template, you would do something like this:
{% set definition_value = product.definition %}
{% set trans_definition = 'product.definition.' ~ definition_value %}
{{ trans_definition|trans }}
it'll work, if it finds the key. What if it cant find it?
That's why you should use DoctrineBehaviors from KnpLabs, which handles all the dynamic translations for you..
If {{ product.definition }} equals 'cellphone' the following should work.
message.language.yml:
'cellphone': This will work!
However if you want to map it with the 'product' key in your message file like this:
product:
'cellphone': This also works
add the key to the twig template like so:
{{('product.'~product.definition)|trans }}

Symfony2 - Twig : Set the default value in the dropdownlist

I have a Form in my twig template.
One of the form fields is a dropdownlist that is created from an Entity in my form builder.
I would like to set the default value in my dropdown list, to be the id 28 for example.
I don't know how to do this in twig.
I tryed this:
{{ form_widget(form.type, {value: 28 } ) }}
But nothing changed, I still have the firs value that is by default.
I know that I can set a default value in the FormType class where I am creating my form builder, but I am looking for a twig way, it seems to me more elegant than to create the object in the formType class.
Ok, found my mistake, the code should be:
{{ form_widget(form.type, {value: "28" } ) }}
I forgot the quotes for the id. And also, there is a need to CTRL + F5 to refresh the page and see the difference.
{{ form_widget(form.type, {value: twig_var ~ "" } ) }}
or
{{ form_row(form.type, {value: twig_var ~ "" } ) }}

Resources