Saving render result to use it again - forms

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.

Related

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.

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

FOSUserBundle issues with plainPassword on twig form

I am defining my own twig layout for new user registration and I have everything laid out the way I want it with the exception of the plainPassword field from the FOSUserBundle.
<p class="left">
{{ form_widget(form.plainPassword) }}
</p>
<div class="clearfix"></div>
The code above displays both the password and verification block. I would like to break this up into the 4 elements of form.plainPassword.label, form.plainPassword.field, form.plainPassword2.label, and form.plainPassword2.field. I cannot figure out what to put in the form_label() and form_widget() calls.
<p class="left">
{{ form_label( ??? ) }}
{{ form_widget( ??? ) }}
</p>
<p class="left">
{{ form_label( ??? ) }}
{{ form_widget( ??? ) }}
</p>
<div class="clearfix"></div>
I am assuming this can be done.
I had the same problem. My solution (seems to be official :) :
{{ form_label (form.plainPassword.first) }}
{{ form_widget (form.plainPassword.first) }}
{{ form_label (form.plainPassword.second) }}
{{ form_widget (form.plainPassword.second) }}
Hope it can helps !
This blog post shows how to output a repeated field in twig.
http://blogsh.de/2011/10/19/how-to-use-the-repeated-field-type-in-symfony/
But in short this worked for me:
{{ form_label (form.plainPassword.children['New Password']) }}
{{ form_widget (form.plainPassword.children['New Password']) }}
{{ form_label (form.plainPassword.children['Confirm Password']) }}
{{ form_widget (form.plainPassword.children['Confirm Password']) }}
I have to say I'm sure using .children isnt the best/official way of doing it, but it works!

Symfony2 validation form without class

I'm trying to make a search form without an entity.
Controller:
public function SearchFormAction() {
$collectionConstraint = new Collection(array(
'size' => new MinLength(3),
));
$searchform = $this->createFormBuilder(null, array(
'validation_constraint' => $collectionConstraint,
))
->add('min_range')
->add('max_range')
->add('bedrooms')
->add('bathrooms')
->add('size')
->add('user')
->getForm()
;
return $this->render("RealBundle:User:search.html.twig", array(
'searchform' => $searchform->createView(),
));
}
View:
<div id="dialog" title="Advanced Search">
<form action="{{ path('searchresults') }}" method="post" {{ form_enctype(searchform) }} id="frmSearch">
<fieldset>
<h3>Properties</h3>
<div class="form-search-item">
{{ form_label(searchform.min_range, 'Price Range') }} {{ form_widget(searchform.min_range) }} to {{ form_widget(searchform.max_range) }}
{{ form_widget(searchform.min_range) }}
</div>
<div class="form-search-item">
{{ form_label(searchform.bedrooms, 'Bedrooms') }}: {{ form_widget(searchform.bedrooms) }}
</div>
<div class="form-search-item">
{{ form_label(searchform.bedrooms, 'Bathrooms') }}: {{ form_widget(searchform.bathrooms) }}
</div>
<div class="form-search-item">
{{ form_label(searchform.bedrooms, 'Size') }}: {{ form_widget(searchform.size) }}
</div>
<h3>User</h3>
<div class="form-search-item">
{{ form_label(searchform.user, 'User') }}: {{ form_widget(searchform.user) }}
</div>
{{ form_rest(searchform) }}
<input type="submit" value="Search">
</fieldset>
</form>
I've try with another validations like MinLength, MaxLenght, Type and nothing works for me, what am I doing wrong?
I want to validate, range, bedrooms, bathrooms, size as integers, and a minLenght for user.
Tnx and sorry for my english.
Your validation seems to be working in my test. But you're missing the error messages in the template.
You need
{{ form_errors(form) }}
to render global errors, and then for each field you can display its errors, eg
{{ form_errors(form.size) }}
Then as if by magic you should see your error messages. Although not having seen your controller I can't be sure you're binding and calling isValid.
If you're still having problems then please post your controller too.