Strange issue with Symfony2 form

时间:2016-08-31 12:12:29

标签: symfony symfony-forms

When I reload page I get true as result of method call isSubmited().

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
    var_dump($form->getData());
    die;
}

And output is:

...["submit"]=> NULL...

What should be the problem here?

On page load, form should not be submitted.

2 个答案:

答案 0 :(得分:1)

The handleRequest method grabs the POST’ed data from the request, processes it, and runs any validation. And actually, it only does this for POST requests so on a GET request, $form->isSubmitted() returns false.

Tip: If you have a form that’s submitted via a different HTTP method, set them method.

For more details: http://symfony.com/doc/current/reference/forms/types/form.html#method

答案 1 :(得分:0)

You should check if request is POST:

if ($request->isMethod('POST')) {
    if ($form->isValid()) {
        var_dump($form->getData());
        die;
    }
}

Submitting a form in Symfony's view is binding data to it, which you did by $form->handleRequest($request); if request is a POST.