I have this class for member :
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();
}
And this FormType for its form :
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';
}
}
and in a controller i use this to render the form :
public function indexAction()
{
$form = $this->createForm(new MemberType(), 'member');
return $this->render('AcmeMainBundle:Default:index2.html.twig',
array('register'=>$form->createView()));
}
It looks like to work,but when I check it I see this error in browser :
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.
I have set data_class to null,but nothing changed!please help...
You should pass an instance of
Member
as the second argument forController#createForm()
, instead of the string "member". Eg: