Display a a related entity property value as a form label in symfony
symfony
Solution
I've made some guesses as to what your entity looks like, but I think this is roughly what you want, something 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()
)
)
});
}
Problem
This stems from this question but my question has changed slightly: Odd many-to-many form rendering with symfony and doctrine My entities are Formula one-to-many with FormulaColor many-to-one with Color. Formula (id, code, name) FormulaColor (formula_id, color_id, percentage) Color (id, code, name) A formula can have one or more colors and each color makes up a percentage of that formula. I'm trying to make a Formula edit type that will show the percentage fields for a given formula and a label or title for each percentage field that is Color->Name for a label. I am already showing the percentage fields for a formula, but I want to label each one with the color name. How can I do this? Will I have to somehow use the querybuilder? I have a FormulaAddEditType that looks like 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 a 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 to 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 was able to get the results I want in a form event subscriber. The subscriber looks like this: ``` class AddPercentFieldSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents() { // Tells the dispatcher that you want to listen on the form.pre_set_data // event and that the preSetData method should be called. return array(FormEvents::PRE_SET_DATA => 'preSetData'); } public function preSetData(FormEvent $event) { $data = $event->getData(); $form = $event->getForm(); // If it's not a new Formula, then I want to show the percentage fields. if ($data) { $form->add('percentage', 'text', array( 'label' => $data->getColor()->getCode(), )); } } } ```