php - FOSUserBundle integrate change password form with other forms
Get the solution ↓↓↓Solution:
(I see that this is an 8 months old question, but maybe it helps someone.)
The most simple way is to use a separate form for the password change event if you want to make only one page for the user profile. FOSUserBundle provides this functionality. So if you want to use your own routes and forms, you can just copy the code from the FOS controller and forms, change some parameters (like route names and design), and you are set. There are probably some more sophisticated ways to use this Bundle, but in my opinion this is the easiest and most flexible.
The FOSUserBundle is in the/vendor/friendsofsymfony/user-bundle/FOS/UserBundle/
directory.
The password controller is at/Controller/ChangePasswordController.php
and the forms are in/Resources/views/ChangePassword
.
Here is how I did this in my Settings page. I only use the password change functionality, but I assume, you can have separate actions for the different forms, and then redirect the user back to the original index page with the view.
This is the controller (I only changed the redirect route and the view):
use JMS\SecurityExtraBundle\Annotation\Secure;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\UserBundle\Model\UserInterface;
class SettingsController extends Controller
{
/**
* @Secure(roles="ROLE_USER")
*/
public function indexAction()
{
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
$form = $this->container->get('fos_user.change_password.form');
$formHandler = $this->container->get('fos_user.change_password.form.handler');
$process = $formHandler->process($user);
if ($process) {
$this->get('session')->setFlash('notice', 'Password changed succesfully');
return $this->redirect($this->generateUrl('settings'));
}
return $this->render('AcmeHelloBundle:Settings:password.html.twig', ['form' => $form->createView()]);
}
}
And this is the view (password.html.twig) - the only change here is the route: path('settings')
<form action="{{ path('settings') }}" {{ form_enctype(form) }} method="POST" class="fos_user_change_password">
{{ form_widget(form) }}
<div>
<input type="submit" value="{{ 'change_password.submit'|trans({}, 'FOSUserBundle') }}" />
</div>
</form>
So this is it. Now you have a nice password change form, and all the heavy lifting is taken care by the FOS UserBundle!
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function getclientoriginalname() on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.