1

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:

Circular reference detected in the "password" type (defined in class "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.

Any ideas folks?

3 Answers 3

4

You defined PasswordType form type field. 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.

The solution is to rename your password type to something like 'userpassword' or you can use repeated field type instead. It will add two fields and do comparison validation of thier values.

Sign up to request clarification or add additional context in comments.

2 Comments

The value "password" in the first option of $builder->add() is the field name, not it's type.
@MrPablo i know. "set the first password field to type "password"" so you set the second argument which is its type.
1

You can use repeated field type instead of your solution, see http://symfony.com/doc/2.0/reference/forms/types/repeated.html

4 Comments

Looked at using repeat, but there are currently issues with the form errors it produces, so I am staying away form it for now :) I figured out the answer, but I cannot post it for 8 hours, but here it is: The issue seems to be from using "password" as the getName() value. I changed this to "pass" and now I can use "password" to set the type, i.e. $builder->add('password', 'password');
@MrPablo that's what i told you in my answer ;p
@MrPablo That's exactly what meze said ;o
Sorry, but it had nothing to do with the $builder->add() part. Meze said I had defined the field type as password, but if you check, it is set to "null". The issues was getName(). Using getName() as "password" was causing the problem, I changed that and THEN I changed $builder->add('password', null) to $builder->add('password', 'password') and it worked. Meze was wrong.
0

The issue seems to be from using "password" as the getName() value.

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, i.e.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.