WashichawbachaW WashichawbachaW - 10 months ago 66
PHP Question

FOSUserBundle - How to call new password and confirm new password in edit.html.twig?

I'm currently working on a project with Symfony 2.8. I am stuck with this FOSUserBundle profile where I don't know what's the name of "new password" and "new password confirmation" forms. I want to get the form variables of "new password" and "new password confirmation" in my edit.html.twig.

I tried this but Symfony doesn't recognize it.

{% extends "UserBundle::layout.html.twig" %}
{% block fos_user_content %}
<div class="row clearfix">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="card">
<div class="header">
<h2>
Change Profile
</h2>
</div>
<div class="body">
<form action="{{ path('fos_user_edit') }}" {{ form_enctype(form) }} method="POST" class="fos_user_edit">
{{ form_errors(form) }}
<label for="username">Username</label>
<div class="form-group">
<div class="form-line">
{{ form_widget(form.username, { 'attr': { 'type': 'text', 'id': 'username', 'class': 'form-control', 'placeholder': 'New Username' } }) }}
</div>
</div>
<label for="email">Email</label>
<div class="form-group">
<div class="form-line">
{{ form_widget(form.email, { 'attr': { 'type': 'email', 'class': 'form-control', 'name': 'email', 'placeholder': 'email' } }) }}
</div>
</div>
<label for="password">Current Password</label>
<div class="form-group">
<div class="form-line">
{{ form_widget(form.current_password, { 'attr': { 'type': 'password', 'class': 'form-control', 'name': 'current_password', 'placeholder': 'Current Password' } }) }}
</div>
{{ form_errors(form.current_password) }}
</div>
<label for="new_password">New Password</label>
<div class="form-group">
<div class="form-line">
{{ form_widget(form.new_password, { 'attr': { 'type': 'password', 'class': 'form-control', 'name': 'new_password', 'placeholder': 'New Password' } }) }}
</div>
{{ form_errors(form.new_password) }}
</div>
<label for="new_password_confirmation">Confirm New Password</label>
<div class="form-group">
<div class="form-line">
{{ form_widget(form.new_password_confirmation, { 'attr': { 'type': 'password', 'class': 'form-control', 'name': 'new_password_confirmation', 'placeholder': 'Confirm New Password' } }) }}
</div>
{{ form_errors(form.new_password_confirmation) }}
</div>
<input type="checkbox" id="remember_me" class="filled-in">
<label for="remember_me">Remember Me</label>
<br>
<button type="submit" class="btn btn-primary m-t-15 waves-effect" value="{{ 'profile.edit.submit'|trans({}, 'FOSUserBundle') }}" />UPDATE</button>
{{ form_rest(form) }}
</form>
</div>
</div>
</div>
</div>
{% endblock fos_user_content %}


It throws this exception:

Neither the property "new_password" nor one of the methods "new_password()", "getnew_password()"/"isnew_password()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView".


This is my controller:

<?php

namespace UserBundle\Controller;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Bundle\FrameworkBundle\Controller\Controller as ProfController;

class ProfileController extends ProfController
{
public function showAction()
{
$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.');
}

return $this->container->get('templating')->renderResponse('UserBundle:Profile:show.html.'.$this->container->getParameter('fos_user.template.engine'), array('user' => $user));
}

public function editAction()
{
$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.profile.form');
$formHandler = $this->container->get('fos_user.profile.form.handler');

$process = $formHandler->process($user);
if ($process) {
$this->setFlash('fos_user_success', 'profile.flash.updated');

return new RedirectResponse($this->getRedirectionUrl($user));
}

return $this->container->get('templating')->renderResponse(
'UserBundle:Profile:edit.html.'.$this->container->getParameter('fos_user.template.engine'),
array('form' => $form->createView())
);
}

/**
* Generate the redirection url when editing is completed.
*
* @param \FOS\UserBundle\Model\UserInterface $user
*
* @return string
*/
protected function getRedirectionUrl(UserInterface $user)
{
return $this->container->get('router')->generate('fos_user_profile_show');
}

/**
* @param string $action
* @param string $value
*/
protected function setFlash($action, $value)
{
$this->container->get('session')->getFlashBag()->set($action, $value);
}
}


And this is my profile form type:

<?php

/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Security\Core\Validator\Constraint\UserPassword as OldUserPassword;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;

class ProfileFormType extends AbstractType
{
private $class;

/**
* @param string $class The User class name
*/
public function __construct($class)
{
$this->class = $class;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
if (class_exists('Symfony\Component\Security\Core\Validator\Constraints\UserPassword')) {
$constraint = new UserPassword();
} else {
// Symfony 2.1 support with the old constraint class
$constraint = new OldUserPassword();
}

$this->buildUserForm($builder, $options);

$builder->add('current_password', 'password', array(
'label' => 'form.current_password',
'translation_domain' => 'FOSUserBundle',
'mapped' => false,
'constraints' => $constraint,
));
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => $this->class,
'intention' => 'profile',
));
}

public function getName()
{
return 'fos_user_profile';
}

/**
* Builds the embedded form representing the user.
*
* @param FormBuilderInterface $builder
* @param array $options
*/
protected function buildUserForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
;
}
}


UPDATE: I see that there is no "new_password" and "new_password_confirmation" on the ProfileFormType.php because it's in the ChangePasswordFormType.php. How can I call these form in 1 twig file which is the edit.html.twig?

Answer Source

I already found the answer long time ago. Here it is:

{{ form_widget(form.new.first }}
{{ form_errors(form.new.first }}

{{ form_widget(form.new.second }}
{{ form_errors(form.new.second }}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download