2017-08-29 14 views
1

私は現在、Symfony 2.8のプロジェクトに取り組んでいます。私はこの「FOSUserBundle」のプロフィールで、「新しいパスワード」と「新しいパスワード確認」という名前が何であるのか分からないところです。私はedit.html.twigに "new password"と "new password confirmation"のフォーム変数を取得したいと思います。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: ChangePasswordFormType.phpにあるため、ProfileFormType.phpに「new_password」と「new_password_confirmation」がないことがわかります。 edit.html.twigである1つの小枝ファイルでこれらのフォームをどのように呼び出すことができますか?

答えて

0

私はすでに長い時間前に答えを見つけました。ここにあります:

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

{{ form_widget(form.new.second }} 
{{ form_errors(form.new.second }} 
関連する問題