How define textarea content on form edit (Symfony2/TWIG) - forms

I want to display a specific content in my editForm textarea so I try like text field but it doesn't work :
{{ form_widget(edit_form.description, { 'attr': {'value': 'content'} }) }} {{ form_errors(edit_form.description) }}
How can I do it in TWIG ?

This should work according to the doc.
{{ form_widget(edit_form.description, { 'value': 'content'}) }} {{ form_errors(edit_form.description) }}
But I've never tested it.

Related

Saving render result to use it again

I'm trying to build a form in my controller, my form contain two "select" options which use the same database table field "villes"
I want to to use this field twice, one time for city departure and another time for city arrival
$form = $this->createFormBuilder($vol)
->add('hor_dep')
->add('villes')
->add('villes')
->add('prix')
->add('reduc', CheckboxType::class, array('required' => true, 'label' => 'Réduction'))
->add('nbr_place')
->getForm();
but when I try to render this form.html.twig
<div class="col-lg-6">
{{ form_label(form.hor_dep,'Heure de départ') }}
{{ form_widget(form.hor_dep) }}
{{ form_label(form.villes,'Ville de départ') }}
{{ form_row(form.villes) }}
{{ form_label(form.hor_arrivee,"Heure d'arrivée") }}
{{ form_widget(form.hor_arrivee) }}
{{ form_label(form.villes,"Ville d'arrivée") }}
{{ form_widget(form.villes) }}
</div>
<div class="col-lg-6">
{{ form_label(form.prix,'Prix') }}
{{ form_widget(form.prix) }}
{{ form_label(form.reduc,'possède une réduction ?') }}
{{ form_widget(form.reduc) }}
{{ form_label(form.nbr_place,'Nombre de place') }}
{{ form_widget(form.nbr_place) }}
</div>
it throws this error
An exception has been thrown during the rendering of a template ("Field "villes" has already been rendered, save the result of previous render call to a variable and output that instead.").
In your case, you have to add two fields in your formType: villeDepart & villeArrivee (like what you did with the heure) because you don't want the first form field be overwritten by the second one.
In your mapping, the two fields will point to the same villes table, but each one will have a different value.

Is there a way to yield to variable section in statamic?

I have a layout with several identical columns which should yield to respective sections:
<div class="column_1">
some column content here
{{ yield:column_1 }}
some more column stuff
</div>
<div class="column_2">
some column content here
{{ yield:column_2 }}
some more column stuff
</div>
...
Now I would like to put that into its own partial:
****** LAYOUT ******
{{ partial:column column_name="column_1" }}
{{ partial:column column_name="column_2" }}
...
****** PARTIALS/COLUMN.HTML ******
<div class="{{ column_name }}">
some column content here
{{ yield to-section="{column_name}" }}
some more column stuff
</div>
****** TEMPLATE *****
{{ section:column_1 }} **Additional stuff for column 1** {{ /section:column_1 }}
{{ section:column_2 }} **Additional stuff for column 2** {{ /section:column_2 }}
But in the documentation I can only find the colon syntax {{ yield:section }} and no hint to a possible variable syntax {{ yield unknown_word_here="section" }}. Most tags have a variable syntax, so I am hoping there is one. Or another solution...
I believe you're looking for the Section tag, which pairs nicely with the Yield tag.

Properly set Symfony's form error rendering position

I try to make a registration form in Symfony 3.4 but it makes me mad that I cannot place errors properly.
This is how I render the form itself:
{{ form_start(registrationForm) }}
{{ form_widget(registrationForm) }}
{{ form_end(registrationForm) }}
And the fields are rendered from a custom template, where I declare that errors should appear under the field:
{% block form_row %}
<div class="form-group">
{{ block('form_label') }}
{{ block('form_widget_simple') }}
{{ block('form_error') }}
</div>
{% endblock %}
However all my errors are appearing above the whole form as li elements in an ul element. I tried to add 'error_bubbling' => false but it won't make any difference. (The error messages come form the Assert annotations on the underlying model object.)
What am I doing wrong?

same customize form for edit and create page symfony

I have started to use symfony.
I am facing problem for create and edit form customization.
I have created a entity with crud. Now I want to customize 'create form' new.html.twig and I am doing with
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_row(form.task) }}
{{ form_row(form.dueDate) }}
{{ form_end(form) }}
My question is for 'edit form': what should I do that I can use the same customized form without duplication?
thanks in advance..
You can set the form action dynamically using a couple of different methods.
First you could pass the action into the template as a variable that is different for each parent template like..
new.html.twig
{{ include('AcmeBundle:Form:_form.html.twig',
{'form': form, 'action': path('create_route') }) }}
edit.html.twig
{{ include('AcmeBundle:Form:_form.html.twig',
{'form': form, 'action': path('edit_route', {'id': form.vars.value.id }) }}
// Or the actual object id if the object has been passed to the template
_form.html.twig
{{ form_start(form, {'action': action }) }}
Or you can check for the presence of an id in your present object and then set the action depending on that. This version does make the template a little less reusable as the action is hardcoded but that would only be an issue if you are planning on having the same form for multiple different pages.
_form.html.twig
{% set action = form.vars.value.id is null
? path('create_route')
: path('edit_route', {'id': form.vars.value.id })
%}
{{ form_start(form, {'action': action }) }}
Note: Strings have been split to multiple lines for readability.

Collection Type Field with a File Field to generate their own respective download link

I having a problem with getting/generating the download link from a Collection Type Field in a form, inside the collection type I have a File Field. Everything works fine with uploading files but in order to generate their own respective download link is where I'm stucked right now.
Is there any way to get the url from the file field? What can I do?
<ul>
<li><ul class="preguntas" data-prototype="{{ form_widget(form.seguimientos.vars.prototype)|e }}">
{% for pregunta in form.seguimientos %}
<li>
{{ form_row(pregunta.seguimientoTipo) }}
{{ form_row(pregunta.fecha_entrega) }}
{{ form_row(pregunta.fecha_prorroga) }}
{{ form_row(pregunta.descripcion) }}
{{ form_row(pregunta.loQueSeEspera) }}
{{ form_row(pregunta.isRecibido) }}
{{ form_row(pregunta.contactos) }}
{{ form_row(pregunta.comentarios) }}
{{ form_row(pregunta.archivo) }}
</li>
{% endfor %}
</ul></li>
This is what I got with {{ dump(pregunta.archivo.vars.value) }}
object(Proxies\__CG__\daci\contratosBundle\Entity\Document)#596 (5) { ["__isInitialized__"]=> bool(true) ["id":protected]=> int(160) ["path":protected]=> string(44) "1feb865f404cba0567e075c76bf6c0b402621e8e.png" ["file":"daci\contratosBundle\Entity\Document":private]=> NULL ["temp":"daci\contratosBundle\Entity\Document":private]=> NULL }
What I want to get is "path" from the object and create the download link
To access to the field I just needed to use {{ pregunta.archivo.value.path }}
From that I created the download link