how to make form ignore password fields if blank (symfony2 forms)
On your entity setter for the password property make it like this:
/**
* Set password
*
* @param string $password
* @return User
*/
public function setPassword($password)
{
if (!is_null($password)) {
$this->password = $password;
}
return $this;
}
The trick is to check if the parameter being passed is empty and only setting if it's not.
For someone else's reference, I worked this one out this way.
My formType:
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('username', 'text', array('label' => 'Servernamn '))
->add('plainPassword', 'repeated', array('type' => 'password', 'first_name' => 'Lösenord för server ', 'second_name' => 'Upprepa lösenord för server',));
$builder-> addValidator(new CallbackValidator(function(FormInterface $form){
$username = $form->get('username')->getData();
if (empty($username)) {
$form['username']->addError(new FormError("Du måste ange ett namn för servern"));
}
}));
}
My updateAction:
public function updateServerAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('BizTVUserBundle:User')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Container entity.');
}
$originalPassword = $entity->getPassword();
$editForm = $this->createForm(new editServerType(), $entity);
$request = $this->getRequest();
$editForm->bindRequest($request);
if ($editForm->isValid()) {
$plainPassword = $editForm->get('plainPassword')->getData();
if (!empty($plainPassword)) {
//encode the password
$encoder = $this->container->get('security.encoder_factory')->getEncoder($entity); //get encoder for hashing pwd later
$tempPassword = $encoder->encodePassword($entity->getPassword(), $entity->getSalt());
$entity->setPassword($tempPassword);
}
else {
$entity->setPassword($originalPassword);
}
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('Server'));
}
Thus updating my users password should it be set, otherwise keep the original password.
What I did was to leave the UserType
as is and only remove the password field in the controller:
/**
* @Route("/users/edit/{username}", name="user_edit")
*/
public function editAction(Request $request, User $user)
{
$form = $this->createForm(UserType::class, $user);
// Remove the password field when editing the user
// Otherwise it must be entered each time the user is edited
// We can change the password via a special edit-user-password page
$form->remove('password');
$form->handleRequest($request);
if ($form->isValid()) {
// ...
}
}
This way you can have a single UserType, reusable in the editAction and the newAction.
Be sure to add the line $form->remove('password');
before $form->handleRequest($request);
.
If you need add required option to $options array. Example:
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('username', 'text', array('label' => 'Servernamn '))
->add('plainPassword', 'repeated', array(
'type' => 'password',
'first_name' => 'password',
'second_name' => 'repeat_password',
'required' => false,
));
}