2
votes

Error that appears:

The form's view data is expected to be an instance of class My\FrontendBundle\Entity\Intro, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of My\FrontendBundle\Entity\Intro.

What is wrong?

In DefaultController.php I have

public function formularzAction(){
        $Repo = $this->getDoctrine()->getRepository('MyFrontendBundle:Intro');
        $Register = $Repo->findAll();
        $form = $this->createForm(new EditType(), $Register);
        return array(
            'form' => $form->createView(),
            'rows' => $Register
        );
    }

my EditType.php

<?php
namespace My\FrontendBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class EditType extends AbstractType{
    public function getName(){
        return 'register_form';
    }
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('info', 'textarea', array(
                    'constraints' => array(
                        ),
                    'label' => 'Informacje dodatkowe'
                ))
            ;
    }
    public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'My\FrontendBundle\Entity\Intro'
        ));
        }   
}

my Intro.php:

<?php

namespace My\FrontendBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Intro
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Intro
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="info", type="string", length=2500)
     */
    private $info;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set info
     *
     * @param string $info
     * @return Intro
     */
    public function setInfo($info)
    {
        $this->info = $info;

        return $this;
    }

    /**
     * Get info
     *
     * @return string 
     */
    public function getInfo()
    {
        return $this->info;
    }
}
1

1 Answers

3
votes

Your form is waiting ONE and only ONE INtro entity.

Your findAll()query will return null or an array with 1 or more Intro instances.

You can not create the form with the result of findAll() query.

You'll need to provide an id to find a specific Intro from database or loop over the array findAll() gave you and make an new IntroType instance for each results.