Reputation: 586
I'm trying to make my own form for allowing user to edit his profile. I success to add my field at the FosUserBundle
original fields but I have a weird behaviour.
My user can change his password only one time !! If I retry to change it from the form 2 seconds after (after disconnecting then logging in for example) it doesn't work: every field value can be changed except the password.
EDIT : New information, the new password is save only if i change another field in the form.
Have you an idea about that?
Here is my code:
UserType.php
in src/UserBundle/Form
<?php
namespace MDB\UserBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('name')
->add('nbEtoile')
->add('dateInscription')
->add('dateNaissance')
->add('adresse', new \MDB\AdresseBundle\Form\AdresseType())
->add('save', 'submit')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'MDB\UserBundle\Entity\User'
));
}
/**
* @return string
*/
public function getName() {
return 'mdb_userbundle_user';
}
public function getParent()
{
return 'fos_user_registration';
}
}
MDB/src/UserBundle/Resources/config/service.yml
services:
mdb_user.registration.form.type:
class: MDB\UserBundle\Form\Type\RegistrationFormType
tags:
- { name: form.type, alias: mdb_user_registration }
mdb_user.registration_complet:
class: MDB\UserBundle\EventListener\RegistrationConfirmListener
arguments: ["@router"]
tags:
- { name: kernel.event_subscriber }
mdb_user.user.form.type:
class: MDB\UserBundle\Form\Type\UserType
tags:
- { name: form.type, alias: mdb_userbundle_user }
app/config/config.yml
fos_user:
db_driver: orm # Le type de BDD à utiliser, nous utilisons l'ORM Doctrine depuis le début
firewall_name: main # Le nom du firewall derrière lequel on utilisera ces utilisateurs
user_class: MDB\UserBundle\Entity\User
registration:
form:
type: mdb_user_registration
type: mdb_userbundle_user
UserController.php
public function editAction(Request $request) {
if (!$this->get('security.context')->isGranted('ROLE_USER')) {
$session = $request->getSession();
$session->getFlashBag()->add('errorRole', 'Vous devez être connecté pour accéder à cette page');
return $this->render('MDBPlatformBundle:Default:index.html.twig');
}
$user = $this->get('security.context')->getToken()->getUser();
$form = $this->createForm(new UserEditType(), $user);
if ($form->handleRequest($request)->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->flush();
$request->getSession()->getFlashBag()->add('notice', 'Profil bien modifiée.');
return $this->redirect($this->generateUrl('mdb_platform_homepage'));
}
return $this->render('MDBUserBundle::editForm.html.twig', array(
'form' => $form->createView()
));
}
Everything is ok, except this point.
Upvotes: 1
Views: 1143
Reputation: 586
Ok , i have found a small hack. It seems that i cant save the password if there is no change in the User entity. So i created a date type field "dateOfProfilEdit
" and actualize it in my controller each time i save the form.
Hope it will helps
Upvotes: 1