2

我有这个成员课程:

class Member {
/**
 * @ODM\Id
 */
protected $id;

/**
 * @ODM\String
 */
protected $fName;

/**
 * @ODM\String
 */
protected $lName;

/**
 * @ODM\String
 */
protected $email;


/**
 * @ODM\String
 */
protected $password;

//getters & setters

public function __toString()
{
    return $this->getFName().$this->getLName();
}

而这个 FormType 的形式:

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class MemberType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('fName', 'text')
            ->add('lName', 'text')
            ->add('password', 'password')
            ->add('email', 'email')
            ->add('sigIn', 'submit');
        $builder->add('terms', 'checkbox', array('property_path' => 'termsAccepted'));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\MainBundle\Document\Member',
        ));
    }

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

在控制器中,我使用它来呈现表单:

public function indexAction()
{
    $form = $this->createForm(new MemberType(), 'member');

    return $this->render('AcmeMainBundle:Default:index2.html.twig',
        array('register'=>$form->createView()));
}

它看起来可以工作,但是当我检查它时,我在浏览器中看到了这个错误:

The form's view data is expected to be an instance of class
Prexter\MainBundle\Document\Member, but is a(n) string. You can avoid this error
by setting the "data_class" option to null or by adding a view transformer that
transforms a(n) string to an instance of Prexter\MainBundle\Document\Member.

我已将 data_class 设置为 null,但没有任何改变!请帮助...

4

1 回答 1

2

您应该传递 的实例Member作为 的第二个参数Controller#createForm(),而不是字符串“member”。例如:

$form = $this->createForm(new MemberType(), new Member());
于 2013-08-13T08:03:49.550 回答