I can create a form view and even edit a submitted one.
But how can I do if I need for example to edit ONLY an user email.
Here's basically what I try:
{{ form_start(form) }}
{{ form_widget(form.id)}}
{{ form_widget(form._token) }}
{{ form_widget(form.email) }}
{{ form_widget(form.submit) }}
{{ form_end(form, {'render_rest': false}) }}
When I submit the form and dump the user after called $form->handleRequest($request), all the rest fields are reset to them default values except the ones I rendered in my twig view (id email).
So what the right way to achieve my goal?
I'm wondering what if I had differents FormType accordingly?
Dont know if this is the recommended way but it works!
Related
I am using the FOSUserBundle. Basically I overrided the registration controller and handler, and i created a customised registration form. What I want to do is to stay on the same page if there's an error during registration failure.
I managed to stay on the same page but the form reloads but doesn't show the error.
So basically how can I generate these errors while staying on the page without clearing the form fields.
Thank you very much for your help.
EDIT: Try this one. Call the form_errors so it prompts the error message within your registration page.
<form id="sign_up" action="{{ path('your_registration_route') }}" method="POST">
{{ form_errors(form) }}
{{ form_widget(form.username) }}
{{ form_errors(form.username) }}
{{ form_widget(form.email) }}
{{ form_errors(form.email) }}
{{ form_widget(form.plainPassword.first) }}
{{ form_errors(form.plainPassword.first) }}
{{ form_widget(form.plainPassword.second) }}
<button class="btn" type="submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}">SIGN UP</button>
{{ form_rest(form) }}
I'm currently trying to only render specific parts of a form but for some reason, the whole form keeps getting rendered as you can see below I've used the form start and end and the whole form is still being rendered even though it should only render the start and end of the form tags.
{{ form_start(searchForm) }}
{{ form_end(searchForm) }}
Also whenever I use the code below all that happens is that the elements are being moved to the top of the form and the elements which shouldn't be rendered are rendered below.
{{ form_start(searchForm) }}
{{ form_widget(searchForm.title) }}
{{ form_widget(searchForm.title) }}
{{ form_end(searchForm) }}
As described in the doc, for don't render unrendered fields:
{{ form_end(form, {'render_rest': false}) }}
Hope this help
I have a form that I display row by row in Twig. On a particular row, I try to pass HTML attributes as options, like this :
{{ form_row(form.classic_text, {'attr': {'class': 'test'}}) }}
The option is passed to the form, as I can see the class being applied to the form. However, when I put a dump right after the above code, like this :
{{ form_row(form.classic_text, {'attr': {'class': 'test'}}) }}
{{ dump(form.classic_text) }}
the attr array is empty. Any idea why ? Thanks for reading, I can provide more information in needed.
I suppose it's just because you are trying to access the attr array outside the scope where it exists
Right now I'm rendering two forms ( one for company and one for it's tags ) and it looks like this:
<h3>Company</h3>
{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_row(form.city) }}
{{ form_row(form.street) }}
{{ form_row(form.postalcode) }}
{{ form_row(form.buildingnumber) }}
{{ form_row(form.vatid) }}
{{ form_row(form.tags) }}
<button id="test">Test</button>
{{ form_row(tags_form.title) }}
{{ form_row(form.save) }}
{{ form_end(form) }}
I want users to be able to add another input ( after clicking test button ) {{ form_row(form.tags) }} so they can add multiple tags with one form, but right now my tags form looks like this:
$builder
->add('title',null,array(
'label' => 'tag.title',
'required' => false
));
and I don't really know how to set this up. I tried with the simplest solution:
$('#test').on('click',function(e) {
e.preventDefault();
$('#fourcreate_portalbundle_companytags_title').clone().appendTo('form');
});
but that way submitting form creates entity only from the second input.
EDIT: I forgot to add - it has to be done with two forms, because first form contains list of currently available tags and the second form is to let users add their own.
You should not use two forms, but have a collection of tags_form in form.tags. The sample in the Cookbook is about adding tags.
The browser sends a "clobbered" form where two or more name attribute of two or more input elements have the same value. Hence, the back-end gets only the last value. I can not be more specific because I am not familiar with that part of Symfony.
If you wish to clone input elements, and have their values submitted correctly, you must at least modify the name attributes before submitting the form. Also, watch out for non-unique id attributes, as that violates the HTML (DOM?) standard.
e.g
var clone = $('input[id="original"]').clone();
clone.attr('name', clone.attr('name') + '1');
I have this in template
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
{{ form_widget(form) }}
The form is appearing as
fos_user_registration_form_username --input box
fos_user_registration_form_email
fos_user_registration_form_plainPassword_first
fos_user_registration_form_plainPassword_second
But it want to have simple labels like Username , Email etc. How to do that
You can parse the individual form fields instead of the form as a whole.
{{ form_widget(form.fos_user_registration_form_username) }}
In this way, you can parse a single form element. Make sure to end with
{{ form_rest(form) }}
to output any not yet parsed fields (such as the csrf protection token).
using the above approach you can add your own labels to the fields.
FOSUserBundle uses the translator system. You need to as seen in the docs here:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/1.2.0/Resources/doc/index.md
Add this:
# app/config/config.yml
framework:
translator: ~
To your config file (app/config/config.yml).
This will tell symfony to replace all the label values with the values found in the translator file (FOSUserBundle.en.yml). Then the form will always print "Username" instead of "fos_user_registration_form_username".