Search code examples
ajaxformssymfonyfosuserbundle

Why the Symfony form is "not submitted" after data is correctly sent through AJAX?


I want to sent a Symfony form through AJAX (AngularJS).

However, even if data is clearly sent, the form is said by Symfony to be "not submitted" (no error actually, but some debug show that isSubmitted returns false)

Here is a test on FOSUserBundle registration form.

EDIT: although pictures appear small, they are sufficiently resolved so that they are perfectly readable if displayed at their full size.

Here are the logs when I use a standard submission from an HTTP form:

Symfony logs - HTTP form submission

Here are the logs when I use AJAX submission:

Symfony logs - AJAX submission

  • I have disabled CSRF token
  • The method I use from AJAX is "post" so it should be ok since it is the default Symfony expects (and FOSUserBundle does not override that default AFAIK).
  • Other forms (outside of FOSUserBundle scope) are correctly sent with AJAX

Would you have any idea on the matter? Any pointer? Or any other log I could check?

EDIT:

I use the default FOSUserBundle registration controller.

I almost use the default FOSUserBundle registration form, but I have added a (currently) empty child form which I'll need it further in the development.

namespace NONG\SecurityBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class UserRegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options) {}

    public function getParent() {
        return 'fos_user_registration';
    }

    public function getName() {
        return 'nong_user_registration';
    }
}

Headers and data sent with the HTML form (NB. added a dot before star in Accept so that the coloration stays correct):

POST /server/web/testfosuser/register/ HTTP/1.1
Host: mywebsite.com
Connection: keep-alive
Content-Length: 229
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/.*;q=0.8
Origin: http://mywebsite.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36         (KHTML, like Gecko) Chrome/40.0.2214.111             Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://mywebsite.com/server/web/testfosuser/register/
Accept-Encoding: gzip, deflate
Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: PHPSESSID=xxxxxxxxxxxxxxxxxxxxxxxxx


fos_user_registration_form%5Bemail%5D=bidon%40bidon.com&
fos_user_registration_form%5Busername%5D=bidon&
fos_user_registration_form%5BplainPassword%5D%5Bfirst%5D=bidon&
fos_user_registration_form%5BplainPassword%5D%5Bsecond%5D=bidon

Headers sent with the AJAX request: (NB. added a dot before star in Accept so that the coloration stays correct):

POST /server/web/testfosuser/register HTTP/1.1
Host: mywebsite.com
Connection: keep-alive
Content-Length: 229
Accept: application/json, text/plain, */.*
Origin: http://mywebsite.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
Content-Type: application/json;charset=UTF-8 application/x-www-form-urlencoded; charset=UTF-8
Referer: http://mywebsite.com/client/
Accept-Encoding: gzip, deflate
Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: PHPSESSID=xxxxxxxxxxxxxxxxxxxxxxxxx

fos_user_registration_form%5Bemail%5D=bidonqdsfqsdf%40qsdf&
fos_user_registration_form%5Busername%5D=charles222&
fos_user_registration_form%5BplainPassword%5D%5Bfirst%5D=bidon&
fos_user_registration_form%5BplainPassword%5D%5Bsecond%5D=bidon

Solution

  • I finally managed to find the correction:

    The correct URL is ...../register/ with a final / that I forgot when I did the AJAX request.

    However, I'm not sure what are the mechanisms at stake here. The deepest I could go debuging (when using the wrong URL) was in HttpFoundationRequestHandler, where the $request->getMethod() call returned GET (instead of POST), thus not submitting the form.