Symfony 2.3.6嵌套表单

编程入门 行业动态 更新时间:2024-10-27 17:21:26
本文介绍了Symfony 2.3.6嵌套表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用表单集合来填写每周数据。我有一个实体,有一些数据的一周

I am trying to have a form with a collection of forms that will allow me to fill in weekly data. I have an Entity that is for the week with a few stats

/** * @ORM\Column(type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ protected $week_id; /** * @ORM\Column(type="string") */ protected $area_worked; /** * @ORM\OneToMany(targetEntity="User") */ protected $approved_by; /** * @ORM\OneToMany(targetEntity="DailyStats") */ protected $daily_stats;

然后我有每日统计实体:

Then i have the daily stats entity:

/** * @ORM\Column(type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ protected $day_id; /** * @ORM\ManyToOne(targetEntity="WeeklyStats") */ protected $weekly_stat_id; /** * @ORM\Column(type="float") */ protected $hours_worked; /** * @ORM\Column(type="integer") */ protected $day_of_week;

然后用这两个我想要一个表格,我可以输出到一个表格显示整个星期:

Then with both of these i want a form that i can output into a table showing the whole week:

Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday Hours | | | | | | |

然而,当我把它放入一个表单时:

However when i put this into a form:

//weekly stats form public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('dailyReports', 'collection',array( 'type'=>new DailyStatsForm(), 'options' => array( 'required' => false ), 'allow_add' => true, )); }

这会生成一个空字段集的表单。我可以使用JavaScript来添加一个字段,但是我想知道它是否可能总是生成7天的这个表单以及其他字段的每周统计信息?

this generates a form with an empty field set. I can use the javascript to add a field to it but I want to know if its possible to just always generate the 7 days in a keep for this form along with the other fields for the weekly stats?

有关解决方案的任何建议将不胜感激。

Any suggestions of solutions would be greatly appreciated.

推荐答案

是的,您可以查看文档,如果您将七个DailyStats实体添加到您的周实体,那么symfony2将呈现您想要的那七个输入,请检查 symfony/doc/current/cookbook/form/form_collections.html

Yes you can, look at the documentation, if you add seven DailyStats entities to your week entity then symfony2 will render those seven inputs that you want, please check symfony/doc/current/cookbook/form/form_collections.html

class TaskController extends Controller { public function newAction(Request $request) { $task = new Task(); // dummy code - this is here just so that the Task has some tags // otherwise, this isn't an interesting example $tag1 = new Tag(); $tag1->name = 'tag1'; $task->getTags()->add($tag1); // any new related entity you add represents a new embeded form $tag2 = new Tag(); $tag2->name = 'tag2'; $task->getTags()->add($tag2); // end dummy code $form = $this->createForm(new TaskType(), $task); $form->handleRequest($request); if ($form->isValid()) { // ... maybe do some form processing, like saving the Task and Tag objects } return $this->render('AcmeTaskBundle:Task:new.html.twig', array( 'form' => $form->createView(), )); } }

更多推荐

Symfony 2.3.6嵌套表单

本文发布于:2023-10-10 22:54:20,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   表单   Symfony

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!