Symfony 2.3.6嵌套表单 [英] Symfony 2.3.6 nested forms

查看:136
本文介绍了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将呈现您想要的那七个输入,请检查 http://symfony.com/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 http://symfony.com/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嵌套表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆