使用fosuserbundle重写更改密码表单


Overriding change password form using fosuserbundle

我使用FOSuserBundle,我已经覆盖了注册和登录表单,它们工作得很好,但覆盖更改密码表单不起作用。它仍然是从vendor ' friendsofsymfony ' user-bundle ' FOS ' UserBundle ' Form ' Type ' ChangePasswordFormType

中的原始类中读取的

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:        addd@hous.fr
            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

您需要在配置中设置更改密码表单。Yml以及注册和配置文件。

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