samedi 28 mars 2015

Symfony2 formtype how to pass data

I would like to build a form which contains data from 2 related entities and is able to update both entities with one submit. So far, I got a working form which uses formtype but only with static data (e.g. $foo = 'bar'). I don't get it how to pass the data from my controller to the formtype and using it in my formtype...


This is what I've got:



// AdminController.php
// throws exception
$em = $this->getDoctrine()->getManager();

$article = $em->getRepository('AcmeBlogBundle:Articles')->find($id);

$form = $this->createForm(new ArticlesType(), $article);


This throws an exception:



Expected argument of type "array or (\Traversable and \ArrayAccess)", "string" given


So, I tried doing this



//this is working, but (of course) empty
$article = new Articles();

$form = $this->createForm(new ArticlesType(), $article);


By the way this is my ArticlesType.php



class ArticlesType extends AbstractType {

public function buildForm(FormBuilderInterface $builder, array $options)
{

$builder->add('headline', 'text',
array('label' => false))
->add('articleContent', 'textarea')
->add('state', 'entity',
array('class' => 'Acme\BlogBundle\Entity\Status',
'property' => 'status',
'label' => false))
->add('category', 'entity',
array('class' => 'Acme\BlogBundle\Entity\Categories',
'property' => 'cat_name',
'label' => false))
->add('save', 'submit')
->add('aufstellung', 'collection', array('type' => new AufstellungType()));

}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\BlogBundle\Entity\Articles',
));
}

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

Aucun commentaire:

Enregistrer un commentaire