Display a a related entity property value as a form label in symfony -
this stems question question has changed slightly: odd many-to-many form rendering symfony , doctrine
my entities formula one-to-many formulacolor many-to-one color.
formula (id, code, name) formulacolor (formula_id, color_id, percentage) color (id, code, name)
a formula can have 1 or more colors , each color makes percentage of formula.
i'm trying make formula edit type show percentage fields given formula , label or title each percentage field color->name label. showing percentage fields formula, want label each 1 color name. how can this? have somehow use querybuilder?
i have formulaaddedittype looks this:
public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('code', null, array( 'label' => 'code' )) ->add('name', null, array( 'label' => 'name' )); $builder->add('formulacolors', 'collection', array( 'type' => new formulacolortype(), 'allow_add' => true, 'allow_delete' => true, 'prototype' => true, )); }
then formulacolortype:
public function buildform(formbuilderinterface $builder, array $options) { $builder->add('percentage', 'number', array( 'label' => new coloraddedittype() )); }
coloraddedittype
public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('code', null, array( 'label' => 'code' )) ->add('name', null, array( 'label' => 'name' )) ; }
the controller action
/** * @route("/formulas/{id}/edit") * @method({"get", "post"}) * @template() */ public function editaction(request $request, $id) { $em = $this->getdoctrine()->getmanager(); $formula = $em->getrepository('prismportalcommonbundle:formula')->find($id); if (!$formula) { throw $this->createnotfoundexception('unable find formula entity.'); } $form = $this->createform(new formulaaddedittype(), $formula); if ($request->ismethod('post')) { $form->bind($request); if ($form->isvalid()) { $em->persist($formula); $em->flush(); return $this->redirect($this->generateurl('prism_portal_admin_dashboard_index')); } } return array( 'formula' => $formula, 'form' => $form->createview() ); }
i able results want in form event subscriber. subscriber looks this:
class addpercentfieldsubscriber implements eventsubscriberinterface { public static function getsubscribedevents() { // tells dispatcher want listen on form.pre_set_data // event , presetdata method should called. return array(formevents::pre_set_data => 'presetdata'); } public function presetdata(formevent $event) { $data = $event->getdata(); $form = $event->getform(); // if it's not new formula, want show percentage fields. if ($data) { $form->add('percentage', 'text', array( 'label' => $data->getcolor()->getcode(), )); } } }
i've made guesses entity looks like, think want, like:
formulaaddedittype
public function buildform(formbuilderinterface $builder, array $options) { $entity=$builder->getdata(); $colors=$entity->getcolors()); $builder ->add('code', null, array( 'label' => 'code' )) ->add('name', null, array( 'label' => 'name' )); $colors->map(function ($color) use ($builder) { $builder->add($color->getname() , null, array( 'label' => $color->getname() ) ) }); }
Comments
Post a Comment