Saturday, 15 January 2011

php - Accessing variables defined in form type in twig -



php - Accessing variables defined in form type in twig -

i don't know wrong twig cannot access variables , i'm getting method "name, origin, car" object "symfony\component\form\formview" not exist in carbrandbundle:default:both.html.twig @ line of them. missing? believe not twig issue.

note: 1-to-n relationship , 1 brand can have many cars.

brand entity

namespace car\brandbundle\entity; utilize doctrine\orm\mapping orm; utilize 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; utilize doctrine\orm\mapping 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; utilize symfony\component\form\abstracttype; utilize symfony\component\form\formbuilderinterface; utilize 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() { homecoming 'car'; } }

both form type

namespace car\brandbundle\form\type; utilize symfony\component\form\abstracttype; utilize symfony\component\form\test\formbuilderinterface; utilize 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() { homecoming 'both'; } }

controller

namespace car\brandbundle\controller; utilize car\brandbundle\entity\brandentity; utilize car\brandbundle\form\type\bothtype; utilize symfony\bundle\frameworkbundle\controller\controller; class bothcontroller extends controller { public function indexaction() { $form = $this->createform(new bothtype(), new brandentity(), array('action' => $this->generateurl('bothcreate'))); homecoming $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) }} {% c in form.car %} {{ form_row(c.model) }} {% endfor %}

your problem lies entities. need getter & setters protected properties. in example:

protected $name; /** * set name * * @param string $name * @return brand */ public function setname($name) { $this->source = $name; homecoming $this; } /** * name * * @return string */ public function getname() { homecoming $this->name; }

php symfony2 twig

No comments:

Post a Comment