for the sf application, I want to remove the password check from the edit profile form in FOSUserBundle.
Just deleting the "current" field by overriding the profile form still causes the "Password is invalid" error message. This is called by the ProfileFormHandler class from FOSUSerBundle with the following code:
$this->form->setData(new CheckPassword($user));
So, I redefined the form handler and replaced the above code with
$this->form->setData($user);
So far this works, and my form type is displayed, and the form handler processes the form, but I get the following error
The CSRF token is invalid. Please try to resubmit the form
Indeed, the csrf token is no longer added to the form. Honestly, I do not know what I did wrong, (
thanks ben
Here is the full form code, handler, and template:
<?php
namespace Application\Sonata\UserBundle\Form\Type;
use Symfony\Component\Form\FormBuilder;
class ProfileFormType extends \FOS\UserBundle\Form\Type\ProfileFormType
{
private $class;
public function __construct($class)
{
$this->class = $class;
}
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('first_name')
->add('last_name')
->add('phone')
->add('location','room13_geo_location')
->add('birthday','birthday')
->add('smoker')
->add('newsletter')
;
}
public function getName()
{
return 'balkanride_user_profile';
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => $this->class,
'intention' => 'profile',
);
}
}
-
<?php
namespace Application\Sonata\UserBundle\Form\Handler;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Form\Model\CheckPassword;
class ProfileFormHandler
{
protected $request;
protected $userManager;
protected $form;
public function __construct(Form $form, Request $request, UserManagerInterface $userManager)
{
$this->form = $form;
$this->request = $request;
$this->userManager = $userManager;
}
public function process(UserInterface $user)
{
$this->form->setData($user);
if ('POST' === $this->request->getMethod())
{
$this->form->bindRequest($this->request);
if ($this->form->isValid())
{
$this->onSuccess($user);
return true;
}
$this->userManager->reloadUser($user);
}
return false;
}
protected function onSuccess(UserInterface $user)
{
$this->userManager->updateUser($user);
}
}
-
{% extends "ApplicationSonataUserBundle::layout.html.twig" %}
{% block page_body %}
<section>
<form id="ProfileForm" action="{{ path('fos_user_profile_edit') }}" {{ form_enctype(form) }} method="POST" class="fos_user_profile_edit">
{{ form_widget(form) }}
<div>
<div class="form-actions">
<input type="submit" value="{{ 'profile.edit.submit'|trans }}" class="btn btn-primary" />
<a href="{{path('fos_user_profile_show')}}" class="btn">{{ 'profile.edit.cancel'|trans }}</a>
</div>
</div>
</form>
</section>
{% endblock page_body %}