我有这个班级成员:
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,但没有任何改变!请帮助...
答案 0 :(得分:2)
您应该传递Member
的实例作为Controller#createForm()
的第二个参数,而不是字符串“member”。例如:
$form = $this->createForm(new MemberType(), new Member());