简体   繁体   中英

Accessing variables defined in form type in twig

I don't know what is wrong but twig cannot access variables and I'm getting Method "name, origin, car" for object "Symfony\\Component\\Form\\FormView" does not exist in CarBrandBundle:Default:both.html.twig at line all of them . What am I missing? I believe it is not only twig issue.

Note: This is a 1-to-n relationship and 1 brand can have many cars.

BRAND ENTITY

namespace Car\BrandBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

class BrandEntity
{
    protected $id;
    protected $name;
    protected $origin;

    /**
     * @ORM\OneToMany(targetEntity = "CarEntity", mappedBy = "brand")
     * @var object $car
     */
    protected $car;

    /**
     * Constructor.
     */
    public function __construct()
    {
        $this->car = new ArrayCollection();
    }
}

CAR ENTITY

namespace Car\BrandBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

class CarEntity
{
    protected $id;
    protected $model;
    protected $price;

    /**
     * @ORM\ManyToOne(targetEntity="BrandEntity", inversedBy="car")
     * @ORM\JoinColumn(name="brand_id", referencedColumnName="id")
     * @var object $brand
     */
    protected $brand;
}

CAR FORM TYPE

namespace Car\BrandBundle\Form\Type;

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

class CarType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setAction($options['action'])
            ->setMethod('POST')
            ->add('model', 'text', array('label' => 'Model'))
            ->add('price', 'text', array('label' => 'Price'))
            ->add('button', 'submit', array('label' => 'Add'))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Car\BrandBundle\Entity\CarEntity')
        );
    }

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

BOTH FORM TYPE

namespace Car\BrandBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Test\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class BothType extends AbstractType
{
    public function builder(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->setAction($options['action'])
            ->setMethod('POST')
            ->add('name', 'text', array('label' => 'Name'))
            ->add('origin', 'text', array('label' => 'Origin'))
            ->add('car', 'collection', array('type' => new CarType()))
            ->add('button', 'submit', array('label' => 'Add'))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Car\BrandBundle\Entity\BrandEntity',
            'cascade_validation' => true
        ));
    }

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

CONTROLLER

namespace Car\BrandBundle\Controller;

use Car\BrandBundle\Entity\BrandEntity;
use Car\BrandBundle\Form\Type\BothType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class BothController extends Controller
{
    public function indexAction()
    {
        $form = $this->createForm(new BothType(), new BrandEntity(),
                array('action' => $this->generateUrl('bothCreate')));

        return $this->render('CarBrandBundle:Default:both.html.twig',
                array('page' => 'Both', 'form' => $form->createView()));
    }
}

TWIG

{{ form_label(form.name) }}
{{ form_widget(form.name) }}

{{ form_label(form.origin) }}
{{ form_widget(form.origin) }}


{% for c in form.car %}
   {{ form_row(c.model) }}
{% endfor %}

Your problem lies with your entities. You need getter & setters even with protected properties. In example:

protected $name;
    /**
     * Set name
     *
     * @param string $name
     * @return Brand
     */
    public function setName($name)
    {
        $this->source = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM