Symfony 2.3.6 nested forms

2019-07-24 14:34发布

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,
        ));
}

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.

1条回答
欢心
2楼-- · 2019-07-24 14:53

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(),
           ));
     }
}
查看更多
登录 后发表回答