我有一个成员类:
class Member {
/**
* @ODMId
*/
protected $id;
/**
* @ODMString
*/
protected $fName;
/**
* @ODMString
*/
protected $lName;
/**
* @ODMString
*/
protected $email;
/**
* @ODMString
*/
protected $password;
//getters & setters
public function __toString()
{
return $this->getFName().$this->getLName();
}
这个FormType的形式是:
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
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' => 'AcmeMainBundleDocumentMember',
));
}
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
PrexterMainBundleDocumentMember, 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 PrexterMainBundleDocumentMember.
我已经将data_class设置为null,但没有任何更改!请帮忙。。。
您应该传递Member
的实例作为Controller#createForm()
的第二个参数,而不是字符串"member"。例如:
$form = $this->createForm(new MemberType(), new Member());