Reputation: 11
I'm starting with symfony 2 and i want display a "choice" type with data from database but I have a problem :
addAction :
public function addAction()
{
$categories = new CategoriesAnnonce();
$form = $this->get('form.factory')->create(new AddFormType($categories),$categories);
return $this->render('AnnoncesBundle::add.html.twig', array(
'form' => $form->createView(),
));
}
AddFormType.php :
<?php
namespace AnnoncesBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints as Assert;
class AddFormType extends AbstractType
{
private $cat;
public function __construct(CategoriesAnnonce $categories)
{
$this->cat = $categories->getNomSousCategorie();
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('titre', 'text')
->add('categories', 'choice', array(
'choices' => $this->cat,
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AnnoncesBundle\Form\Model\Add',
));
}
public function getName()
{
return 'Add';
}
}
?>
Error :
Catchable Fatal Error: Argument 1 passed to AnnoncesBundle\Form\Type\AddFormType::__construct() must be an instance of AnnoncesBundle\Form\Type\CategoriesAnnonce, instance of AnnoncesBundle\Entity\CategoriesAnnonce given, called in /Users/jordan/Desktop/www/Lesbonnesaffaires/src/AnnoncesBundle/Controller/DefaultController.php on line 69 and defined
Upvotes: 1
Views: 1033
Reputation: 60413
It's using the namespace defined in the file instead of the proper namespace to which CategoriesAnnonce
belongs. Add a use
statement:
use AnnoncesBundle\Entity\CategoriesAnnonce
OR change your type hint to the FQCN like:
public function __construct(\AnnoncesBundle\Entity\CategoriesAnnonce, $categories) {
//...
}
Thanks but I have a other problem now:
The form's view data is expected to be an instance of class AnnoncesBundle\Form\Model\Add, but is an instance of class AnnoncesBundle\Entity\CategoriesAnnonce. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class AnnoncesBundle\Entity\CategoriesAnnonce to an instance of AnnoncesBundle\Form\Model\Add.
This happens because you have the data_class set to AnnoncesBundle\Form\Model\Add
:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AnnoncesBundle\Form\Model\Add',
));
}
But you are trying to pass it AnnoncesBundle\Entity\CategoriesAnnonce
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('titre', 'text')
// this cat is an instance of AnnoncesBundle\Entity\CategoriesAnnonce
->add('categories', 'choice', array(
'choices' => $this->cat,
));
}
I think you need to rework your code for all of this. Unfortunately I can't give specifics because I don't know what AnnoncesBundle\Form\Model\Add
and AnnoncesBundle\Entity\CategoriesAnnonce
are, and how they relate or are supposed to interact.
But I suspect that you are confused about creating a form class and creating a custom field type and possibly how you are creating your class hierarchy and what the different components should be doing.
I would review the forms chapter of the book and the custom field type cookbook entry. Then if you have issue create a new question about the specifics with the relevant code from your Entity/Model, Form, and FieldType classes.
Upvotes: 2