FOSUserBundle issues with plainPassword on twig form - forms

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!

Related

symfony2 and twig: get properties of form field

I am trying to create a form with sub-fields with symfony2.
In twig I render the form as
{{ form_start(form) }}
{{ form_errors(form) }}
<div>
{{ form_label(form) }}
{{ form_errors(form) }}
{% for field in form %}
{{ form_widget(field) }}
{% endfor %}
</div>
{{ form_end(form) }}
However, I want to add some customization depending on the field I am rendering.
What I want to achieve is something like this:
{{ form_start(form) }}
{{ form_errors(form) }}
<div>
{{ form_label(form) }}
{{ form_errors(form) }}
{% for field in form %}
{% if field.label == "myvalue" %} <-- this code is not working
{# do something here #}
{{ form_widget(field) }}
{% endif %}
{% endfor %}
</div>
{{ form_end(form) }}
I am not able to access the label of each of my sub-fields in twig.
I think it is possible with something like
{{ field.vars.something }}
, but I did not manage to find any clear documentation about this.
Can someone please help?
Thank you!
Edit:
I actually found the answer to my question:
It was indeed just
{{ field.vars.label }}
and
{% if field.vars.label == "myvalue" %}
{# do something here #}
{{ form_widget(field) }}
{% endif %}
did the trick.
However, I am still looking for some good documentation about this "vars" attribute in twig, and what can be retrieved with it.
Thanks!
You'll find more information at http://symfony.com/doc/current/reference/forms/twig_reference.html#more-about-form-variables
On that page you'll find a list of common form vars. You might also create custom vars by implementing the buildView method of a FormType. You can read an example at http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html#adding-the-extension-business-logic
Hope it'll help

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

prototype field is empty after form(form) but set when set before the form(form) symfony

when I follow this tutorial: http://symfony.com/doc/current/cookbook/form/form_collections.html
and I render the
<ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e }}">
...
</ul>
the prototype stay empty if I put it after the {{ form(form)}} but gets filled If I put it i before the {{ form(form) }} tag. any one an idea why this is and how to solve it.
thanks
The tag {{ form(form) }} is supposed to output all your form, so there is nothing to output after this tag.
If the tag {{ form(form) }} does not output the prototype, then it was not configured right in the form type class.
But if you chose to output prototype by using form_widget, you should not use form(form) and should output the form by parts:
{{ form_start(form) }}
{{ form_errors(form) }}
<div>
{{ form_row(form.another_form_property) }}
</div>
<div>
<ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e }}">
...
</ul>
</div>
{{ form_end(form) }}

multiple form in symfony2

I have a form for adding comments, and i want to know how can i multiply this form on the same page.
I tried this code but it does not work :
{% for statut in statuts %}
<div>
{{ statut.sujet }}
<form action="" method="post" {{ form_enctype(formC) }}>
{{ form_errors(formC) }}
{{ form_errors(formC.commentaire) }}
{{ form_widget(formC.commentaire) }}
<div><input type="submit" class="btn btn-info"/><div>
{{ form_rest(formC) }}
</form>
</div>
{% endfor %}
Does somebody know how to do that ? or any suggestions
You need to do this in the controller like get all statuts then run a for loop for each statuts and get comment form for each statut field store them in array and return the array.
In your twig template then run a for loop for the same.

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.