我的实体是一对一的公式,Formulacolor与color一对一.
公式(ID,代码,名称)
Formulacolor(formula_ID,color_ID,百分比)
颜色(ID,名称)
公式可以有一种或多种颜色,每种颜色占该公式的百分比.
我正在尝试制作一个公式编辑类型,它将显示给定公式的百分比字段以及每个百分比字段的标签或标题,即color->标签的名称.我已经显示了公式的百分比字段,但我想用颜色名称标记每个字段.我怎样才能做到这一点?我是否必须以某种方式使用querybuilder?
我有一个如下所示的FormulaAddEditType:
public function buildForm(FormBuilderInterface $builder,array $options){ $builder ->add('code',null,array( 'label' => 'Code' )) ->add('name',array( 'label' => 'name' )); $builder->add('formulacolors','collection',array( 'type' => new FormulacolorType(),'allow_add' => true,'allow_delete' => true,'prototype' => true,));}
然后是一个FormulacolorType:
public function buildForm(FormBuilderInterface $builder,array $options){ $builder->add('percentage','number',array( 'label' => new colorAddEditType() ));}
colorAddEditType
public function buildForm(FormBuilderInterface $builder,array( 'label' => 'name' )) ;}
控制器动作
/** * @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() );}
我能够在表单事件订阅者中获得我想要的结果.订阅者看起来像这样:
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(),)); } }}解决方法 我已经猜测了你的实体是什么样的,但我认为这大致是你想要的,例如:
FormulaAddEditType
public function buildForm(FormBuilderInterface $builder,array $options) { $entity=$builder->getData(); $colors=$entity->getcolors()); $builder ->add('code',array( 'label' => 'Code' )) ->add('name',array( 'label' => 'name' )); $colors->map(function ($color) use ($builder) { $builder->add($color->getname(),array( 'label' => $color->getname() ) ) });}总结
以上是内存溢出为你收集整理的在symfony中显示相关的实体属性值作为表单标签全部内容,希望文章能够帮你解决在symfony中显示相关的实体属性值作为表单标签所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)