Symfony2表单,与实体不匹配

时间:2015-06-01 18:02:54

标签: php forms symfony

我正在关注Symfony 2中的'how to'工具注册表单 我创建了Registration和RegistrationType,就像教程一样。

我也创建了User实体和UserType。 一切都是平等的,但我收到了提交表格的异常。

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

请注册Registration.php:

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

$user不是User,而是array。该数组具有输入数据:

array (size=2)
'email' => string 'email@email.com' (length=15)
'password' => string 'password' (length=8)

为什么表单不匹配实体?

在我的UserType表单中,我配置了data_class:

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

的用户类型:

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';
}
}

注册:

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;
}
}

行动:

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');
    }

用户实体:

/**
 * 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 个答案:

答案 0 :(得分:2)

在Symfony 2.6之前,此方法为setDefaultOptions(),而不是configureOptions()。所以解决方案是升级到Symfony 2.6 / 2.7或阅读您的版本的文档。