I'm facing a problem like the one posted here but since I have not a UserFormType
I don't know how to solve this. The idea is not to ask for password each time I (logged in as admin and with right permissions) will change a user profile field like email for example. This is my code:
public function editAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('UserBundle:User')->find($id);
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->container->get('fos_user.profile.form.factory');
$form = $formFactory->createForm();
$form->setData($user);
if ('POST' === $request->getMethod())
{
$form->handleRequest($request);
if ($form->isValid())
{
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->container->get('fos_user.user_manager');
$userManager->updateUser($user);
}
}
return array(
'form' => $form->createView(),
'id' => $user->getId()
);
}
Resuming, how do I avoid ask for password each time I want to update a user profile, any help?
So it looks like the problem stems from the fact you're letting FOSUserBundle do everything for you, which is fair enough but makes it harder to tweak behaviour. You can override various things to change how it works, but my own application doesn't use FOSUserBundle at all for managing users after they're registered, you can simply create your own simple form to do it, e.g.:
class UserType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', 'text')
->add('email', 'email')
->add('save', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Your\Bundle\Entity\User',
));
}
/**
* Returns the name of this type.
*
* @return string The name of this type
*/
public function getName()
{
return 'user';
}
}
And then your controller would stay reasonably similar, except no separate Form Factory needed and use your UserType:
public function editAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('UserBundle:User')->find($id);
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
//Don't need this!
//$formFactory = $this->container->get('fos_user.profile.form.factory');
//$form = $formFactory->createForm();
//$form->setData($user);
$form = $this->createForm(new UserType(), $user);
if ('POST' === $request->getMethod())
{
$form->handleRequest($request);
if ($form->isValid())
{
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->container->get('fos_user.user_manager');
$userManager->updateUser($user);
}
}
return array(
'form' => $form->createView(),
'id' => $user->getId()
);
}
Not sure of the detail of your View etc, but you just need to render the Form and things should be golden!
Hope I haven't got the wrong end of the stick.