Reputation: 4188
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?
Upvotes: 1
Views: 3607
Reputation: 4188
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');
Upvotes: 0
Reputation: 15087
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.
Upvotes: 4
Reputation: 877
You can use repeated field type instead of your solution, see http://symfony.com/doc/2.0/reference/forms/types/repeated.html
Upvotes: 1