Symfony2 passing variables $_post - php

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.

Related

Add variables to a form in Symfony2

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', ''));

Getting $_POST fields before loading form php

I want to get all of the fields that will be submitted in $_POST, but before the page is loaded
$data['event'] = $_POSTKEYSFORFORM; //i don't care if they are empty
$this->load->view('event_v/form',$data);
In my template I have values set to $event-key, which causes an error obviously if the key does not exist.
Thanks!
From what I know you cannot "grab the keys" of the POST before you send the POST request.
The controller has no way of knowing what's loaded in the view itself.
If it's that critical to you, You can pass the POST fields as an array to the view, have them echo out in the view, and so have perfect control(if they change, they change in one place only).
It's far from ideal as it's not dynamic to the amount of fields.
Other dirty hacks include having JS auto submit the form once and then collect the POST fields.
Why do you need the post fields without values though, Answering that question might make it easier for us to help out.

Multi-select does not retain value after post in codeigniter

I have a form in my application with a multi select. I'm using CI's form helper to build my forms, so the build of the element looks like this:
return form_multiselect('authors[response][]', $faculty->get_all_for_multiselect(),
$pre_selected, $additional_attributes);
This is all well and good if the items are in the database ($pre_selected gets existing responses). However, I'm also running the form through CI's form validation, and when that happens, if validation fails, then the multi select loses the values that had been selected.
I'm sure this is something simple that I'm just over looking, so hopefully someone can help me out here.
Adding more information
The field is marked as required so it is going through the validator (although it will always pass as I'm automatically selecting the current user).
(I'm assuming $pre_selected is an array of values?)
You can reset selected values after failed form submit using the $_POST array.
Since you're already using $pre_selected, you should be able to use the following:
return form_multiselect('authors[response][]', $faculty->get_all_for_multiselect(),
array_unique(array_merge($pre_selected, $_POST['response'])), $additional_attributes);

Prepopulating Yii form, from GET/POST

My problem is I have an HTML page that includes a short form on it. What I'd like is when this page posts/gets into my Yii model form, to be able to grab and pre-populate the empty form for the model with the values from the incoming form...
I dont think I can use the pagination widget because my initial page is HTML. Is there some way I can just pull this POST value in if it's set rather than the model.
Im still new to Yii so if this is a simple answer, I apologize
Edit: To clarify, my initial form has a few values from my main page form. The HTML page has 3 fields, say first name, last name, email address. That form then posts in to my full page form, which asks for additional information to complete the model. I'm hoping though that I can pre-populate the first and last name in the new php/yii form.
Basically I'm hoping when I first render _form.php I can grab a GET/POST value and assign it to the current model, or add it as a default value on the form.
"I'm hoping ... I can grab a GET/POST value and assign it to the current model"
You sure can. Do this in your controller and you'll be set:
$model->first_name = $_GET['first_name'];
Of course, you may also want to validate those values and set a model scenario to make sure you don't end up with bad data being passed in by an attacker. Otherwise you could run into XSS attacks. But the ability to directly assign model attributes is nice and powerful ...

Using same php file to show form and receive the form's data

I have a php page that generates a form. The action attribute of the form is the page itself. After the user submits the form, the same page is loaded, but this time a POST variable is set, so the page runs another script to deal with the incoming data from the form. I do this by using a conditional fork:
if(isset($_POST['var'])){
generate form
}else{
insert $_POST data into database
}
I'd like to know if this is ok or a bad idea.
I agree with Ignacio. Other than that it looks like a fairly standard approach if you don't need more complexity. One very important thing: make sure you are validating and sanitizing that data before it goes into the database.
The bad part is setting the action attribute to the script. Omitting it completely indicates to the browser that it should be posted to the same URL.
You might even want to go to the extent of checking whether the data was submitted thru AJAX to differentiate it from a regular form submission:
if ( $_SERVER['X_REQUESTED_WITH']=='XMLHttpRequest' )
// AJAX

Categories