I have created an update password form , But It doesn't work :
It show this error message : The User object must implement the UserInterface interface.
the code :
Controller :
public function editpasswordlaunchAction(users $users, Request $request) {
//$form = $this->createForm(new usersTypeupdatepassword(), $users);
$changePasswordModel = new ChangePassword();
$form = $this->createForm(new ChangePasswordType(), $changePasswordModel);
$form->handleRequest($request);
if ($form->isValid() && $form->isSubmitted()) {
$em = $this->getDoctrine()->getManager();
$user = $form->getData();
$em->persist($user);
$em->flush();
$this->get('session')->getFlashBag()->add(
'notice', array(
'alert' => 'success',
'title' => 'Success!',
'message' => 'Updated'
)
);
}
return $this->render('socialBundle:profile:edit.html.twig', array(
'passwordupdate' => 'TRUE',
'form' => $form->createView(),
));
Changepassword.php :
<?php
namespace social\socialBundle\Form\Model;
use Symfony\Component\Security\Core\Validator\Constraints as SecurityAssert;
use Symfony\Component\Validator\Constraints as Assert;
class ChangePassword {
/**
* @SecurityAssert\UserPassword(
* message = "Wrong value for your current password"
* )
*/
protected $oldPassword;
/**
* @Assert\Length(
* min = 6,
* minMessage = "Password should by at least 6 chars long"
* )
*/
protected $newPassword;
public function setOldPassword($oldPassword) {
$this->oldPassword = $oldPassword;
}
public function getOldPassword() {
return $this->oldPassword;
}
public function setNewPassword($newPassword) {
$this->newPassword = $newPassword;
}
public function getNewPassword() {
return $this->newPassword;
}
}
and ChangeformType.php :
<?php
namespace social\socialBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ChangePasswordType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('oldPassword', 'password');
$builder->add('newPassword', 'repeated', array(
'type' => 'password',
'invalid_message' => 'The password fields must match.',
'required' => true,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'social\socialBundle\Form\Model\ChangePassword',
));
}
public function getName()
{
return 'change_passwd';
}
}