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.
Related
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.
In Drupal 8, the form twig is like that :
<form{{ attributes }}>
{{ children }}
</form>
I would like display all fields exept one.
Usually we use the without filter but children is a string so that won't work.
<form{{ attributes }}>
{{ children|without('unwanted') }}
</form>
In the form twig, we can access to the element variable which is an array. But still, the without filter doesn't work here.
<form{{ attributes }}>
{{ element|without('unwanted') }}
</form>
I know I can display all the fields 1 by 1 with {{ element.field }}, But if my form has new fields they will not be printed until I update the form twig. I want to keep the dynamical purpose.
I do not want to update the form code in php to keep the layout in twig.
Any idea ?
Try this:
{% for key in element|keys %}
{{ children|without(key) }}
{% endfor %}
Hope that works out...
We found a quite good answer :
Create the custom template
{#
/**
* #file
*
* Available variables
* - form
*
* #see template_preprocess_mycustomform()
*
* #ingroup themeable
*/
#}
{{ form }}
Declare the template in the theme function
'mycustomform' => [
'render element' => 'form',
],
Apply the template to the form
$form['#theme'] = 'mycustomform';
Result : In the new template, we can use the without twig function
{{ form|without('myfield') }}
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?
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.
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