Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
297 views
Welcome To Ask or Share your Answers For Others

1 Answer

The form post values are stored under the name of the form in the request. For example, if you've overridden the getName() method of ContactType() to return "contact", you would do this:

$postData = $request->request->get('contact');
$name_value = $postData['name'];

If you're still having trouble, try doing a var_dump() on $request->request->all() to see all the post values.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...