Je fais un site pour une association en Symfony 3.0 dont un formulaire mise à jour que je surcharge avec celui de FOSUserBundle que voici :
{% trans_default_domain 'FOSUserBundle' %}
{% block body %}
{{ form_start(form, { 'action': path('fos_user_profile_edit'), 'attr': { 'class': 'fos_user_profile_edit', 'class': 'form-group' } }) }}
{{ form_row(form.username, {'attr': {'class': 'form-control', 'value': app.user.username }}) }}
{{ form_row(form.email, {'attr': {'class': 'form-control', 'value': app.user.email }}) }}
{{ form_row(form.nom, {'attr': {'class': 'form-control', 'value': app.user.nom }}) }}
{{ form_row(form.prenom, {'attr': {'class': 'form-control', 'value': app.user.prenom }}) }}
{{ form_row(form.adresse, {'attr': {'class': 'form-control', 'value': app.user.adresse, 'required' : 'false' }}) }}
{{ form_row(form.cp, {'attr': {'class': 'form-control', 'value': app.user.cp, 'required' : 'false' }}) }}
{{ form_row(form.ville, {'attr': {'class': 'form-control', 'value': app.user.ville, 'required' : 'false' }}) }}
{{ form_row(form.tel, {'attr': {'class': 'form-control', 'value': app.user.tel, 'required' : 'false' }}) }}
{{ form_row(form.current_password, {'attr': {'class': 'form-control', 'value': app.user.password}}) }}
<div>
<input type="submit" class="btn btn-warning" value="{{ 'profile.edit.submit'|trans }}" />
</div>
<div>
<a href="{{ path('assoc_homepage') }}"><button type="button" class="btn btn-link">Retour à la page d'accueil</button></a>
</div>
{{ form_end(form) }}
{% endblock %}
Et voici la classe qui m'a servi à créer le formulaire surchargé :
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AppBundle\Form;
use FOS\UserBundle\Util\LegacyFormHelper;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
class ProfileFormType extends AbstractType {
/**
* @var string
*/
private $class;
/**
* @param string $class The User class name
*/
public function __construct($class) {
$this->class = $class;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$this->buildUserForm($builder, $options);
$constraintsOptions = array(
'message' => 'fos_user.current_password.invalid',
);
if (!empty($options['validation_groups'])) {
$constraintsOptions['groups'] = array(reset($options['validation_groups']));
}
$builder->add('current_password', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\PasswordType'), array(
'label' => 'form.current_password',
'translation_domain' => 'FOSUserBundle',
'mapped' => false,
'constraints' => new UserPassword($constraintsOptions),
));
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => $this->class,
'csrf_token_id' => 'profile',
// BC for SF < 2.8
'intention' => 'profile',
));
}
public function getParent() {
return 'FOS\UserBundle\Form\Type\ProfileFormType';
}
// BC for SF < 3.0
/**
* {@inheritdoc}
*/
public function getName() {
return $this->getBlockPrefix();
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix() {
return 'fos_user_profile';
}
/**
* Builds the embedded form representing the user.
*
* @param FormBuilderInterface $builder
* @param array $options
*/
protected function buildUserForm(FormBuilderInterface $builder, array $options) {
$builder
->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
->add('email', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\EmailType'), array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
->add('nom', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextType'), array('attr' => array('label' => 'form.nom', 'translation_domain' => 'FOSUserBundle', 'placeholder' => 'Votre nom', 'class' => 'form-control')))
->add('prenom', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextType'), array('attr' => array('label' => 'form.prenom', 'translation_domain' => 'FOSUserBundle', 'placeholder' => 'Votre prénom', 'class' => 'form-control')))
->add('adresse', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextType'), array('attr' => array('label' => 'form.adresse', 'translation_domain' => 'FOSUserBundle', 'placeholder' => 'Votre adresse', 'class' => 'form-control')))
->add('cp', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextType'), array('attr' => array('label' => 'form.cp', 'translation_domain' => 'FOSUserBundle', 'placeholder' => 'Votre code postal', 'class' => 'form-control')))
->add('ville', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextType'), array('attr' => array('label' => 'form.ville', 'translation_domain' => 'FOSUserBundle', 'placeholder' => 'Votre ville', 'class' => 'form-control')))
->add('tel', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\TextType'), array('attr' => array('label' => 'form.tel', 'translation_domain' => 'FOSUserBundle', 'placeholder' => 'Votre téléphone', 'class' => 'form-control')))
;
}
}
Et voici la configuration de mon FOSUserBundle dans le config.yml :
# Fosuserbundle configuration
fos_user:
db_driver: orm
firewall_name: main
user_class: AppBundle\Entity\Membre
registration:
form:
type: AppBundle\Form\RegistrationType
profile:
form:
type: AppBundle\Form\ProfileFormType
Et celui du services.yml :
services:
app.form.registration:
class: AppBundle\Form\RegistrationType
tags:
- { name: form.type, alias: app_user_registration }
app.form.profile:
class: AppBundle\Form\ProfileFormType
tags:
- { name: form.type, alias: app_user_profile }
Et enfin voici le controller ProfileController qui permet d'éditer un profil et que j'ai surchargé :
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
//namespace FOS\UserBundle\Controller;
namespace AppBundle\Controller;
use AppBundle\Entity\Membre;
use AppBundle\Form\RegistrationFormType;
use AppBundle\Form\ProfileFormType;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Form\Factory\FactoryInterface;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Controller\ProfileController as BaseController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
/**
* Controller managing the user profile.
*
* @author Christophe Coevoet <st...@notk.org>
*/
class ProfileController extends BaseController {
/**
* Show the user.
*/
public function showAction() {
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
// Retrieve flashbag from the controller
$flashbag = $this->get('session')->getFlashBag();
// Set a flash message
$flashbag->add("success", "Votre profil a été modifier avec succès");
return $this->render('@FOSUser/Profile/show.html.twig', array(
'user' => $user,
));
}
/**
* Edit the user.
*
* @param Request $request
*
* @return Response
*/
public function editAction(Request $request) {
$user = $this->getUser();
$em = $this->getDoctrine()
->getManager();
$membre = $em->getRepository('AppBundle:Membre')->find($user);
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
/** @var $dispatcher EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
/** @var $formFactory FactoryInterface */
$formFactory = $this->get('fos_user.profile.form.factory');
$form = $formFactory->createForm(new ProfileFormType('AppBundle\Entity\Membre', array('membre'=>$membre)) , $user);
$form->setData($user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var $userManager UserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_profile_show');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
return $this->render('@FOSUser/Profile/edit.html.twig', array(
'form' => $form->createView(),
));
}
}
Et j'ai cette erreur que voici :
Je sais que lors de l'appel de la méthode createForm() dans la méthode editAction() de mon controller ProfileController attend un tableau en argument, mais je ne sais pas quoi mettre...
Pouvez-vous m'aider ?
Bien cordialement,
Honoré Rasamoelina