简体   繁体   English

Symfony2-无法设置密码表单字段来键入“ password”

[英]Symfony2 - Cannot set password form field to type “password”

I have build a form class to generate a form that will allow a user to type in a new password twice in order to change it. 我已经建立了一个表单类来生成一个表单,该表单将允许用户键入两次新密码以进行更改。

code: 码:

<?php

namespace UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\CallbackValidator;

class PasswordType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('password', null);
        $builder->add('confirmPassword', null, array('label' => 'Confirm Password', 'property_path' => false));
        $builder->addValidator(new CallbackValidator(function($form)
            {
                if($form['confirmPassword']->getData() != $form['password']->getData()) {
                    $form['confirmPassword']->addError(new FormError('Passwords must match.'));
                }
            }));
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'UserBundle\Entity\User',
        );
    }

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

Now, this class works pretty well, BUT my issues is that when I set the first password field to type "password" I get this error: 现在,该类运行良好,但是我的问题是,当我将第一个密码字段设置为键入“ password”时,出现此错误:

Circular reference detected in the "password" type (defined in class "UserBundle\\Form\\Type\\PasswordType"). 以“密码”类型(在类“ UserBundle \\ Form \\ Type \\ PasswordType”中定义)检测到循环引用。

And I cannot leave it set to "null" as it will use a normal text input field, which is not ideal. 而且我不能将其设置为“ null”,因为它将使用普通的文本输入字段,这并不理想。

Any ideas folks? 大家有什么想法吗?

You defined PasswordType form type field. 您定义了PasswordType表单类型字段。 Then you added a field of Password type inside it. 然后,您在其中添加了一个密码类型字段。 That caused another call to PasswordType::buildForm() to add another field of this type and it goes forever. 这就导致了对PasswordType :: buildForm()的另一次调用,以添加此类型的另一个字段,并且该字段永远存在。

The solution is to rename your password type to something like 'userpassword' or you can use repeated field type instead. 解决方案是将密码类型重命名为“ userpassword”,也可以使用repeated字段类型。 It will add two fields and do comparison validation of thier values. 它将添加两个字段并对其值进行比较验证。

您可以使用重复的字段类型代替解决方案,请参见http://symfony.com/doc/2.0/reference/forms/types/repeated.html

The issue seems to be from using "password" as the getName() value. 问题似乎出在使用“密码”作为getName()值。

Before: 之前:

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

After: 后:

public function getName()
{
    return 'changePassword'; // basically anything other than 'password'
}

I changed this to "pass" and now I can use "password" to set the type, ie 我将其更改为“ pass”,现在可以使用“ password”设置类型,即

$builder->add('password', 'password');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM