2
votes

I'am follow the 'how to' implement registration form in Symfony 2 I created the Registration and RegistrationType, like the tutorial.

I created the User entity and UserType as well. Everything is equal, but I recived an Exception submiting the form.

Catchable Fatal Error: Argument 1 passed to My\Bundle\Form\Model\Registration::setUser() must be an instance of My\Bundle\Entity\User, array given

Becasue in the Registration.php:

public function setUser(User $user)
{
    $this->user = $user;
}

$user is not a User, is an array. This array have the inputs data:

array (size=2)
'email' => string '[email protected]' (length=15)
'password' => string 'password' (length=8)

Why the form is not 'matching' the entity?

In my UserType form I configured the data_class:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class'        => 'My\Bundle\Entity\User',
    ));
}

UserType:

class UserType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('email', 'text')
        ->add('password', 'repeated', array(
            'first_name'    => 'password',
            'second_name'   => 'confirm',
            'type'          => 'password'
        ))
        ;
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class'        => 'My\Bundle\Entity\User',
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'user';
}
}

RegistrationType:

class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('user', new UserType());
    $builder->add('terms', 'checkbox', array('property_path' => 'termsAccepted'));
    $builder->add('Register', 'submit');
}

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

Registration:

class Registration {

/**
 * @Assert\Type(type="My\Bundle\Entity\User")
 * @Assert\Valid()
 */
protected $user;

/**
 * @Assert\NotBlank()
 * @Assert\True()
 */
protected $termsAccepted;



public function setUser(User $user)
{
    $this->user = $user;
}

public function getUser()
{
    return $this->user;
}

public function getTermsAccepted()
{
    return $this->termsAccepted;
}

public function setTermsAccepted($termsAccepted)
{
    $this->termsAccepted = (bool) $termsAccepted;
}
}

The Action:

public function registerProcessAction(Request $request){

    $em = $this->getDoctrine()->getManager();

    $form = $this->createForm(new RegistrationType(), new Registration());

    $form->handleRequest($request);

    if ($form->isValid()) {
        $registration = $form->getData();
        $em->persist($registration->getUser());
        $em->flush();

        return $this->redirectToRoute(...);
    }
    else{
        die('invalid');
    }

User entity:

/**
 * Class User
 * @ORM\Entity
 * @UniqueEntity(fields="email", message="Email already taken")
 */
class User{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;


/**
 * @ORM\Column(type="string", length=150, unique=true)
 * @Assert\NotNull()
 * @Assert\Email()
 */
private $email;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank()
 * @Assert\Length(max = 4096)
 */
private $password;

/** GETTERS **/
public function getId()
{
    return $this->id;
}

public function getEmail()
{
    return $this->email;
}

public function getPassword()
{
    return $this->password;
}

/** SETTERS **/

public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

public function setPassword($password)
{
    $this->password = password_hash($password, PASSWORD_DEFAULT);

    return $this;
}

}
1
You said it - it doesn't match because it's an array, and the method expects User object. What does your $user variable contains? Can you edit your post and show how you populate that? - Artamiel
@Artamiel, I updated the question adding the var_dump of the $user variable. I don't populate this variable, I supose that is a thing of the Symfony Form code. Thanks - Carlos Vázquez
pls show the content of your action where you are bind an user from the form - Evgeniy Kuzmin
Are you using Symfony 2.6+? Before that it was setDefaultOptions() instead of configureOptions() - Konstantin Pereiaslov
@KonstantinPereyaslov That's it! Now I update to 2.7.0 and use configureOptions() function. Thank you! - Carlos Vázquez

1 Answers

2
votes

Before Symfony 2.6 this method was setDefaultOptions(), not configureOptions(). So the solution would be to upgrade to Symfony 2.6/2.7 or read documentation for your version.