0
votes

I try to make a customer area but I have a problem when I try to customize the registration form ..

I have this error

You have requested a non-existent service "fos_user.registration.form".

I followed various tutorials but could not get out a solution here would my various files :

src/Acme/UserBundle/Entity/User.php

    <?php
// src/Acme/UserBundle/Entity/User.php

namespace Acme\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="last_name",type="string", length=100)
     */
    public $lastname;

    /**
     * @ORM\Column(name="phone",type="string", length=12)
     */
    public $phone;

    /**
     * @ORM\Column(name="first_name",type="string", length=100)
     */
    public $firstname;

    /**
     * @ORM\Column(name="newsletter", type="boolean", options={"default":0})
     */
    public $newsletter;

    /**
     * @ORM\Column(name="cnil", type="boolean", options={"default":0})
     */
    public $cnil;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
}

src/Acme/UserBundle/Controller/RegistrationController.php

<?php

namespace Acme\UserBundle\Controller;

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

class RegistrationController extends BaseController
{
    public function registerAction(Request $request)
    {
        $form = $this->container->get('fos_user.registration.form');
        $formHandler = $this->container->get('fos_user.registration.form.handler');
        $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');

        $process = $formHandler->process($confirmationEnabled);
        if ($process) {
            $user = $form->getData();

            /*****************************************************
             * Add new functionality (e.g. log the registration) *
             *****************************************************/
            $this->container->get('logger')->info(
                sprintf('New user registration: %s', $user)
            );

            if ($confirmationEnabled) {
                $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
                $route = 'fos_user_registration_check_email';
            } else {
                $this->authenticateUser($user);
                $route = 'fos_user_registration_confirmed';
            }

            $this->setFlash('fos_user_success', 'registration.flash.user_created');
            $url = $this->container->get('router')->generate($route);

            return new RedirectResponse($url);
        }

        return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}

src/Acme/UserBundle/Form/Type/RegistrationFormType.php

<?php

namespace Acme\UserBundle\Form\Type;

use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;


class RegistrationFormType extends BaseType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        // add your custom field
       $builder
        ->add('username', 'text', array('label' => 'form.username', 'attr' => array('class' => 'form-control'), 'label_attr'=> array('class' => 'col-sm-2 control-label')))
        ->add('lastname', 'text', array('label' => 'form.lastname', 'attr' => array('class' => 'form-control'), 'label_attr'=> array('class' => 'col-sm-2 control-label')))
        ->add('firstname','text', array('label' => 'form.firstname', 'attr' => array('class' => 'form-control'), 'label_attr'=> array('class' => 'col-sm-2 control-label')))
        ->add('phone','text', array('label' => 'form.phone', 'attr' => array('class' => 'form-control'), 'label_attr'=> array('class' => 'col-sm-2 control-label')))
        ->add('email','email', array('label' => 'form.email', 'attr' => array('class' => 'form-control'), 'label_attr'=> array('class' => 'col-sm-2 control-label')))
        ->add('plainPassword', 'repeated', array(
            'type' => 'password',
            'first_options' => array('label' => 'form.password', 'attr' => array('class' => 'form-control'), 'label_attr'=> array('class' => 'col-sm-2 control-label')),
            'second_options' => array('label' => 'form.password_confirmation', 'attr' => array('class' => 'form-control'), 'label_attr'=> array('class' => 'col-sm-2 control-label')),
            'invalid_message' => 'fos_user.password.mismatch',
        ))
        ->add('cnil', 'checkbox', array('label' => 'form.cnil'))
        ->add('newsletter', 'checkbox', array('label' => 'form.newsletter', 'required' => false));
    }

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

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

src/Acme/UserBundle/Resources/views/Registration/register.html.twig

{% extends "AcmeUserBundle::layout_base.html.twig" %}

{% trans_default_domain 'AcmeUserBundle' %}

{% block fos_user_content %}
<div class="row">
    <div class="col-sm-12">

        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Créer mon compte fidélité</h3>
            </div>
            <div class="panel-body">

                <form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="form-horizontal" role="form">
                    {{ form_widget(form._token) }}

                    <div class="form-group">
                        {{ form_label(form.lastname) }}
                        <div class="col-sm-10">
                            {{ form_widget(form.lastname) }}
                        </div>
                    </div>

                    <div class="form-group">
                        {{ form_label(form.firstname) }}
                        <div class="col-sm-10">
                            {{ form_widget(form.firstname) }}
                        </div>
                    </div>

                    <div class="form-group">
                        {{ form_label(form.phone) }}
                        <div class="col-sm-10">
                            {{ form_widget(form.phone) }}
                        </div>
                    </div>

                    <div class="form-group">
                        {{ form_label(form.email) }}
                        <div class="col-sm-10">
                            {{ form_widget(form.email) }}
                        </div>
                    </div>

                    <div class="form-group-separator"></div>

                    <div class="form-group">
                        {{ form_label(form.username) }}
                        <div class="col-sm-10">
                            {{ form_widget(form.username) }}
                        </div>
                    </div>

                    <div class="form-group">
                        {{ form_label(form.plainPassword.first) }}
                        <div class="col-sm-10">
                            {{ form_widget(form.plainPassword.first) }}
                        </div>
                    </div>

                    <div class="form-group">
                        {{ form_label(form.plainPassword.second) }}
                        <div class="col-sm-10">
                            {{ form_widget(form.plainPassword.second) }}
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label"></label>
                        <div class="col-sm-10">
                            <div class="checkbox">
                                <label for="fos_user_registration_form_cnil">
                                    {{ form_widget(form.cnil) }}
                                    {{ 'form.cnil'|trans }}
                                </label>
                            </div>
                            <div class="checkbox">
                                <label for="fos_user_registration_form_newsletter">
                                    {{ form_widget(form.newsletter) }}
                                    {{ 'form.newsletter'|trans }}
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="form-group-separator"></div>

                    <div class="vertical-top">
                        <button type="submit" class="btn btn-info pull-right">{{ 'registration.submit'|trans }}</button>
                        <a href="{{ path('fos_user_security_login') }}" class="btn btn-gray">J'ai déjà un compte</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock fos_user_content %}

{% block javascript_import_on_this_page_first %}
    <link rel="stylesheet" href="{{ asset('js/select2/select2.css') }}">
    <link rel="stylesheet" href="{{ asset('js/select2/select2-bootstrap.css') }}">
    <link rel="stylesheet" href="{{ asset('js/multiselect/css/multi-select.css') }}">
{% endblock javascript_import_on_this_page_first %}

{% block javascript_import_on_this_page_end %}
    <script src="{{ asset('js/datepicker/bootstrap-datepicker.js') }}"></script>
    <script src="{{ asset('js/select2/select2.min.js') }}"></script>
    <script src="{{ asset('js/multiselect/js/jquery.multi-select.js') }}"></script>
{% endblock javascript_import_on_this_page_end %}

src/Acme/UserBundle/Resources/config/services.xml

<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>

        <service id="acme_user.registration.form.type" class="Acme\UserBundle\Form\Type\RegistrationFormType">
            <tag name="form.type" alias="acme_user_registration" />
        </service>

    </services>

</container>

app/config/config.yml

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }



framework:
    #esi:             ~
    translator:      { fallback: %locale% }
    secret:          %secret%
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  "%locale%"
    trusted_proxies: ~
    session:         ~
    fragments:       ~

# Twig Configuration
twig:
    cache:            false
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%

# Assetic Configuration
assetic:
    debug:          %kernel.debug%
    use_controller: false
    bundles:        [ ]
    #java: /usr/bin/java
    filters:
        cssrewrite: ~
        #closure:
        #    jar: %kernel.root_dir%/Resources/java/compiler.jar
        #yui_css:
        #    jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar

# Doctrine Configuration
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        charset:  UTF8
        # if using pdo_sqlite as your database driver, add the path in parameters.yml
        # e.g. database_path: %kernel.root_dir%/data/data.db3
        # path:     %database_path%

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
    transport: %mailer_transport%
    host:      %mailer_host%
    username:  %mailer_user%
    password:  %mailer_password%
    spool:     { type: memory }

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: Acme\UserBundle\Entity\User
    group:
        group_class: Acme\UserBundle\Entity\Group
    registration:
        confirmation:
            enabled:    true
            from_email:
                address:        [email protected]
                sender_name:    Adrien Design
        form:
            type: acme_user_registration

Thank you in advance for your assistance

1

1 Answers

0
votes

According to this issue and after looking at the code of the original RegistrationController, I can confirm there is no such service, and you should do this instead:

$formFactory = $this->get('fos_user.registration.form.factory');
$form = $formFactory->createForm();