1
我目前正在使用Symfony 2.8開發一個項目。我堅持使用這個FOSUserBundle配置文件,我不知道「新密碼」和「新密碼確認」表單的名稱是什麼。我想在我的edit.html.twig中獲得「新密碼」和「新密碼確認」的表單變量。FOSUserBundle - 如何在edit.html.twig中調用新密碼並確認新密碼?
我試過這個,但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'))
;
}
}
UPDATE:我發現ProfileFormType.php中沒有「new_password」和「new_password_confirmation」,因爲它在ChangePasswordFormType.php中。我怎樣才能在edit.html.twig的1個twig文件中調用這些表單?