I'm using a Symfony 2 in a project, and I need to have dependent selects in my form. Currently I'm following this tutorial showmethecode.es and its working.
But I'm also using options groups in the selects, and I really need to add some extra variables and some logic in the client side.
My question is, how can I add variables to a form (in the client side), and join them in a Type class.
Either you read up on Custom Form Field types and Data transformers:
http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html
http://symfony.com/doc/current/cookbook/form/data_transformers.html
Or you create the form yourself and get the data from the request in the send action.
You should however create a CSRF Token when creating the form and check it when submitting the form:
$this->get('security.csrf.token_manager');
//Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider
$token = $csrf->getToken("bundlenamespace_formname");
And validate it like so:
$this->get('security.csrf.token_manager')->isTokenValid($request->get('_token', ''));
Related
I'm building a website that needs to have public forms where the fields are not statics, and based on the result of a form in the back office. So the only purpose of the back-office form is to specify which fields will be displayed in the related front-end form.
For example, imagine that i have an "Events" entity, with a related "eventsType". This form contains fields like a choiceType "activities" and so on, and is used in the back-office by the admin for creating an event. When this form is submitted, a new event is created.
The page displaying the new event also display a form where each fields is created dynamically based on this "events" data.
This new form is bound to another entity, let's say "Attendees" and people can use this form to register to the event, submitting all the data specifics to this event (using the dynamically generated fields).
For now the only solution i have in mind for that is using the form events, but is there another way? I know there is some bundles out there for related problems like multi-step forms, but is there a bundle which could be used in my case?
What I want to do
I'm trying to build a form in Symfony 2.8 that requests the user's choice between many types of credentials and add an input box to the form it's similar to this one but I use a select option field instead of buttons.
What I did
I was able to create this form with HTML and JavaScript.The user can select more than one credential type only once except the OTV Option that can be selected without limits.
image here
What's my problem
I need a way to store user's data in a session and get them in my controller.
Is it possible to pass submitted data from HTML form to a Symfony controller and if not how can I build a similar form with Symfony's formBuilder ?
CollectionType is probably what you are looking for.
For your purposes, you can think of it as adding rows to the end of a two-column list. The Left Column has credential_type with Name, Email etc in a <select>; and the Right Column allows for your text values.
You'll have two FormTypes: one for the list, and another for the list item. In the list item FormType, you should be able to set a CallbackConstraint to validate e.g. that email values are valid upon form submission.
All user changes are saved at the same time, only when the form as valid.
I think you can do that. There is CollectionType in Symfony form. You can read about it here and how to implement it here
P.S. The main problem in this case is to generate specific names for dynamically created fields to provide a normal handling data in controller.
I'm trying to implement a payment system in my website with Symfony.
I have to add a form, which takes the creditcard number. But I don't want this number coming to my server. So I need to do a form in which no name attribute is specified. So the server cannot take this value and I read the value directly by javascript.
Does Anybody know how to do a form without name?
If I do:
$form = $this->createFormBuilder()
->add('creditNum', 'text')
->getForm();
This automatically creates a name attribute.
You could think about creating two separate forms, one form for your order form, and one form that just contains the credit card field(s). That way you can still handle the payment processing via Javascript, and when the user submits the main order form, the credit card field is not part of it, and won't be submitted to your server.
You'd probably have to use Javascript (or some really tricky CSS) to display the two forms nicely, so you might as well just create the credit card field with Javascript outside of the form itself, and position it so it looks like it's part of the form.
I have a form which upon submit must redirect to another form to complete further details.
My problem is that i cant find out how to pass the post variables from the first form to the second.
In my controller I have something like this :
$urls = $_POST['app_frontbundle_urls']['urls'];
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
// var_dump($urls);die();
return $this->redirect($this->generateUrl('purchase_new'));
I cant use an array to pass the variables in GET as I absolutely need these variables in post as there will be a hidden field and a textarea
How can i do this ?
My var_dump here does print my variables but how to get it to the other controller?
Thanks for your help
Why don't you store them in the session or the request ? And you flush the session/request once you used them.
If you absolutely must have access to the data from earlier form submissions in order to process the next form submission I would use a Controller Event Listener to load the data you just saved and place it inside a service on the Container.
StackOverflow answer explaining how to do this
Symfony Cookbook article on how to do this
However, I would recommend that you redesign your form so that it doesn't need to know the data that was posted previously in order to process itself. Having forms that depend on one another's input is problematic because validation will depend on what was posted previously. If you just need to kick off a process at the end of the form process then just save each result, validating as you go through the various controllers and then in the last form do the extra processing as well as validating that the other forms were completed.
I'm using Zend Framework and I currently have an existing form using zend-form which functions as required.
I want to add another optional text field to it, but allow the user to choose to display the field and also display it multiple times. e.g. A user registration form with an 'alternative emails' area, allowing the user to add further text fields for each of their email addresses.
Unfortunately, I'm not sure how to go about this. I think sub-forms might be the way forward, but not too sure.
Ideally, once the form is submitted I'd want the data in an array so that I can process it for storing in a MySQL table afterwards.
See this excellent post by Jeremy Kendall on dynamically adding fields to a form:
Dynamically Adding Elements to Zend_Form
Upshot is to use jQuery on the client-side to add fields and maintain a registry of the new fields. Then on the server-side, call a new preValidate() method on the form object which checks the posted registry and adds the required fields into the $form object before standard processing - like isValid() and getValues() - is invoked.
Why don't you make an AJAX call to an addfieldAction() method in which you generate the HTML of another textfield and return that? You can still make use of Zend Form and the whole form can be processed the normal way.