2014-07-05 38 views
1

我正在使用FOSuserBundle,我已覆蓋註冊和登錄表單,它們工作正常,但覆蓋更改密碼錶單不起作用。 它仍然從原始類中廠商\ friendsofsymfony \用戶束\ FOS \ UserBundle \表格\型號\ ChangePasswordFormType讀使用fosuserbundle覆蓋更改密碼錶格

ChangePasswordFormType.php

<?php 

namespace Boutique\UserBundle\Form\Type; 

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

use FOS\UserBundle\Form\Type\ChangePasswordFormType as BaseType; 

class ChangePasswordFormType extends BaseType 
{ 
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(); 
    } 

    $builder->add('current_password', 'password', array(
     'label' => 'actuelle', 
     'translation_domain' => 'FOSUserBundle', 
     'mapped' => false, 
     'constraints' => $constraint, 
    )); 
    $builder->add('plainPassword', 'repeated', array(
     'type' => 'password', 
     'options' => array('translation_domain' => 'FOSUserBundle'), 
     'first_options' => array('label' => 'form.new_password'), 
     'second_options' => array('label' => 'form.new_password_confirmation'), 
     'invalid_message' => 'fos_user.password.mismatch', 
    )); 
} 

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

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

services.yml

boutique_user.change_password.form.type: 
    class: Boutique\UserBundle\Form\Type\ChangePasswordFormType 
    arguments: [%fos_user.model.user.class%] 
    tags: 
     - { name: form.type, alias: boutique_user_change_password } 

config.yml

fos_user: 
db_driver: orm 
firewall_name: main 
user_class: Boutique\UserBundle\Entity\User 
registration: 
    confirmation: 
     from_email: # Use this node only if you don't want the global email address for the confirmation email 
      address:  [email protected] 
      sender_name: no-reply 
     enabled: false # change to true for required email confirmation 
     template: FOSUserBundle:Registration:email.txt.twig 
    form: 
     type:    boutique_user_registration 
     name:    fos_user_registration_form 
     validation_groups: [Registration, Default] 
profile: 
    form: 
     type: boutique_user_profile_edit 

回答

5

您需要在config.yml中設置更改密碼錶單以及註冊和配置文件。

fos_user: 
    change_password: 
     form: 
      type: boutique_user_change_password 
      name: boutique_user_change_password