Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 + FOSUserBundle registration form not submitting, only reloading

I've been at my wit's end for a few days now with this issue and I can't seem to find the problem.

Overview:

  • I'm building a Symfony2 website using FOSUserBundle as the user manager, and Propel as the database provider.
  • I've successfully overridden the login template and I can log in and out successfully from a modal view.
  • I've added 6 custom fields to my schema.xml, and the model builds correctly (ie. I can query these fields in a tabulated User Manager).
  • I can successfully register as a new user in the default route for registration on my overridden form, and the data shows up in my database as expected.

The Issue:

I have moved the registration form to its final position inside a modal view in my 'user manager dashboard'. My form loads and displays correctly, but for whatever reason refuses to submit data to the controller for processing. Instead it simply loads the default route for registration ('/register') and displays the form again but without any of my css, js, or base twig template. When I re-enter information in that form, it simply reloads with none of the info going in to the database.

I am convinced the issue lies in either (or both) how I am submitting the form or how I have classed the form.

Note I have changed the name of my classes to the statutory AcmeBundle for obvious reasons :)

Can anyone shed some light on this? I am literally about to implode over this issue!

Thank you in advance :D

EDIT

I now think this could possibly be a routing issue as I have verified my form builds and collects all its values correctly?

app/config.yml

#FOS UserBundle Configuration
fos_user:
    db_driver: propel # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: FOS\UserBundle\Propel\User
    registration:
        form:
            type: acme_user_registration

#Services
services:
    acme_user.registration.form.type:
        class: Acme\UserBundle\Form\Type\RegistrationFormType
        arguments: [%fos_user.model.user.class%]
        tags:
            - { name: form.type, alias: acme_user_registration }
    acme_user.form.handler.registration:
            class: Acme\UserBundle\Form\Handler\RegistrationFormHandler
            arguments: ["@fos_user.registration.form", "@request", "@fos_user.user_manager", "@fos_user.mailer", "@fos_user.util.token_generator"]
            scope: request

app/routing.yml is standard FOSUserBundle config as per docs

# FOSUserBundle Routing
fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /profile

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /register

fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: /resetting

fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: /profile

Acme/UserBundle/Controller/RegistrationController.php

<?php

namespace Acme\UserBundle\Controller;

use FOS\UserBundle\Controller\RegistrationController as BaseController;
use Symfony\Component\HttpFoundation\Response;


class RegistrationController extends BaseController
{
    public function render($view, array $parameters = array(), Response $response = null)
    {
        return parent::render($view, $parameters, $response);
    }

}

Acme/AdminBundle/Resources/views/_addUser.html.twig this is where I call my modal in

{% include "FOSUserBundle::layout.html.twig" %}

<!-- Modal -->
<div id="registerUser" class="reveal-modal xlarge"  data-reveal>

    <h2 align="center">Add New User</h2>
    <h6 align="center" style="color: red">All Fields are Required</h6>

    {% block fos_user_content %}
        {% render url('fos_user_registration_register') %}
    {% endblock %}

    <a class="close-reveal-modal">&#215;</a>

</div>

app/Resources/FOSUserBundle/views/Registration/register.html.twig Overidden as per docs

{% block fos_user_content %}
    {% include 'FOSUserBundle:Registration:register_content.html.twig' %}
{% endblock %}

app/Resources/FOSUserBundle/views/Registration/register_content.html.twig Overidden as per docs

<form data-abide class="fos_user_registration_register" action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="post" >
            <fieldset>
                <legend>Personal Details</legend>

                {# First Name #}
                <div>
                    <label for="form_first_name">First Name</label>
                    <input type="text" id="form_first_name" name="form[first_name]" required pattern="[a-zA-Z]+"/>
                    <small class="error">This field is required</small>
                </div>

                {# Last Name #}
                <div>
                    <label for="form_last_name">Surname</label>
                    <input type="text" id="form_last_name" name="form[last_name]" required pattern="[a-zA-Z]+">
                    <small class="error">This field is required</small>
                </div>

                etc...
                etc...
                etc...

            </fieldset>
            <div>
                <input type="submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}" />
            </div>
</form>
like image 731
Chris Avatar asked Aug 03 '14 15:08

Chris


2 Answers

You have to change your register_content.html.twig :

{{ form_start(form, {'attr': {'class': 'fos_user_registration_register', 'id': 'register-form'} }) }}
{{ form_label(form.username) }}
{{ form_errors(form.username) }}        
{{ form_widget(form.username) }}
{{ form_label(form.email) }}
{{ form_errors(form.email) }}       
{{ form_widget(form.email) }}
{{ form_label(form.plainPassword.first, null, {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
        {{ form_errors(form.plainPassword.first) }}
         {{ form_widget(form.plainPassword.first, { 'attr': {'class': 'form-control'} }) }}
         {{ form_label(form.plainPassword.second, null, {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
        {{ form_errors(form.plainPassword.second) }}

         {{ form_widget(form.plainPassword.second, { 'attr': {'class': 'form-control'} }) }}
         <input type="submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}" />

         {{ form_rest(form) }}

this works with me .You should see twig documentation about forms it will help you .

like image 191
meherzi.sahbi Avatar answered Oct 04 '22 20:10

meherzi.sahbi


In the excerpt of register_content, the inclusion of form_rest function has not been explicitly introduced (There are etc. after the "surname" field).

{{ form_rest(form) }}

Calling this function takes care of adding required hidden fields like the one in charge of preventing crsf attacks.

Besides, not having included a call to form_errors prevents you from realizing if there are validation errors related to any fields (like the aforementioned csrf token if it were to be invalid for instance).

{{ form_errors(form) }}

In order to clarify what could possibly be going wrong, you may want to access the logs generated by the form submission. For example, when relying on your (Symfony2) development environment:

tail -fn 100 app/logs/dev.log 

You might find an error of some level telling you what the next stage of your problem solving session could be.

We could have a chat to live debug if needed.

like image 31
Thierry Marianne Avatar answered Oct 04 '22 21:10

Thierry Marianne