php - symfony2 rendering of a form as form type -
i have 2 entities invoice , customer. have 2 forms invoiceform , customerform. when create new invoice (with invoiceform), want able create new customer (just one). here how (and works):
<?php namespace acme\demobundle\form; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; class customerform extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('firstname') ->add('lastname') ->add('phone'); } public function getname() { return 'customer_create'; } } <?php namespace acme\demobundle\form; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; class invoiceform extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('title') ->add('date') ->add('customer', new customerform()); } public function getname() { return 'invoice_create'; } }
so in view (invoice/new.html.twig) can customize way display attributes of invoiceform (title , date), mean can put form_row(invoiceform.title) in whatever want, can anything. attributes of "subform" invoiceform.customer, can't, it's done automatically (firstname, lastname, phone) because can use invoiceform.customer. see mean?
if, in controller, making "new customerform()", , rendering form, want, since i'm making "new invoiceform()", can't. don't know if explain correctly ^^
thanks :)
ok i've found answer myself, can access subform attributes with:
{{ form_row(form.customer.firstname }} {{ form_row(form.customer.lastname }} {{ form_row(form.customer.phone }}
so can customize need. , also, there way test form type in view:
{{ form.customer.vars.block_prefixes.2 }}
which in case return customer_create name of subform (yeah know did not ask that, needed ^^ maybe else ;) )
thank you!
Comments
Post a Comment