I am using my own entity that extend fos_user.
I'm extending the User entity actually in 2 distinct tables (investor that extend the table user). Some data such as password and email are stored in user. Investor can access therefor to the Fos_user methods.
I have a form populated with the users's data. I need to be able to update the user with or without the password.
This is how it is done :
if(!empty($form->get('password')->getData())){
$investor->setPlainPassword($form->get('password')->getData());
}
The update is perfectly working except if the password input is empty.
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'password' cannot be null
This is how i declare the input in my form $builder :
->add('password', 'repeated',
array(
'type' => 'password',
'invalid_message' => 'The password fields have to be the same',
'required' => false,
'first_options' => array('label' => 'New password'),
'second_options' => array('label' => 'Confirm the new password')
)
)
And this is my Controller :
public function updateInvestorAction(Request $request)
{
$user = $this->container->get('security.context')->getToken()->getUser();
$investor = $this->getDoctrine()->getRepository('AppBundle:Investor')->findOneBy(array('id' => $user->getId()));
$form = $this->createForm(new UpdateInvestorType(), $investor);
$form->handleRequest($request);
if ($form->isValid()) {
if(!empty($form->get('password')->getData())){
$investor->setPlainPassword($form->get('password')->getData());
}
$em = $this->getDoctrine()->getManager();
$em->persist($investor);
$em->flush();
$session = $this->getRequest()->getSession();
$session->getFlashBag()->add('message', 'Votre profil a été correctement modifié');
return $this->redirect($this->generateUrl('home'));
}
return array(
'form' => $form->createView(),
);
}
How can i update my user without giving a new or the previous password?