FOSUserBundle - 如何在 edit.html.twig 中调用新密码并确认新密码?

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

我目前正在使用 Symfony 2.8 开发一个项目。我被这个 FOSUserBundle 配置文件困住了,我不知道 "new password" 和 "new password confirmation" 表单的名称是什么。我想在我的 edit.html.twig.

中获取 "new password" 和 "new password confirmation" 的表单变量

我试过了,但 Symfony 无法识别它。

{% 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 %}

它抛出这个异常:

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".

这是我的控制器:

<?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);
    }
}

这是我的个人资料表单类型:

    <?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'))
        ;
    }
    }

更新: 我看到 ProfileFormType.php 上没有 "new_password" 和 "new_password_confirmation",因为它在 ChangePasswordFormType.php .如何在 edit.html.twig?

的 1 个树枝文件中调用这些表单

我早就找到答案了。这是:

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

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