OK, this is a newbie question, but I can't find the answer anywhere. In a controller in Symfony2, I want to access the POST value from one of my forms. In the controller I have:
public function indexAction()
{
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$form = $this->get('form.factory')->create(new ContactType());
$form->bindRequest($request);
if ($form->isValid()) {
$name_value = $request->request->get('name');
Unfortunately $name_value
isn't returning anything. What am I doing wrong? Thanks!
The field data can be accessed in a controller with: Listing 12-34
In addition, the data of an unmapped field can also be modified directly: Listing 12-35
page 164 symfony2 book(generated on October 9, 2013)
what worked for me was using this:
I access the ticketNumber parameter for my multipart post request in the following way.
If you are newbie, welcome to Symfony2, an open-source project so if you want to learn a lot, you can open the source !
From "Form.php" :
getData() getNormData() getViewData()
You can find more details in this file.
Symfony 2.2
this solution is deprecated since 2.3 and will be removed in 3.0, see documentation
gives you an array for the form parameters
from symfony2 book page 162 (Chapter 12: Forms)
[...] sometimes, you may just want to use a form without a class, and get back an array of the submitted data. This is actually really easy:
You can also access POST values (in this case "name") directly through the request object, like so:
Be advised, however, that in most cases using the getData() method is a better choice, since it returns the data (usually an object) after it's been transformed by the form framework.
When you want to access the form token, you have to use the answer of Problematic
$postData = $request->request->get('contact');
because thegetData()
removes the element from the arraySymfony 2.3
since 2.3 you should use
handleRequest
instead ofbindRequest
:see documentation
There is one trick with
ParameterBag::get()
method. You can set$deep
parameter totrue
and access the required deep nested value without extra variable:Also you have possibility to set a default value (2nd parameter of
get()
method), it can avoid redundantisset($form['some']['deep']['data'])
call.