Php Symfony2表单中EntityType字段的动态组
我正在尝试开发一个表格,让学生从几个小组中选择课程。其定义如下:Php Symfony2表单中EntityType字段的动态组,php,forms,symfony,Php,Forms,Symfony,我正在尝试开发一个表格,让学生从几个小组中选择课程。其定义如下: 课程有名称/描述/学时 课程属于对学时有名称/说明/限制的组(例如,您必须从该组中选择5学时,或者您最多只能从该组中选择10学时) 课程组被组织成一个项目,可以有其他领域 因此,我希望表格允许学生选择课程,但课程是按分组组织的,并有关于选择多少课程的说明。学分部分将使用自定义验证规则进行验证 下面是我想到的代码的想法(省略了许多名称空间/原则映射等) 实体有点像这样: class Offering { // has m
- 课程有名称/描述/学时
- 课程属于对学时有名称/说明/限制的组(例如,您必须从该组中选择5学时,或者您最多只能从该组中选择10学时)
- 课程组被组织成一个项目,可以有其他领域
class Offering
{
// has multiple CourseGroups
private $groups;
}
class CourseGroup
{
// which Offering it belongs to
private $offering;
private $name;
private $maxHours;
}
class Course
{
// which CourseGroup it belongs to
private $group;
private $name;
private $hours;
}
// User submits an application with chosen courses from multiple groups
class Application
{
// ...
private $courses;
}
// Joins the Applications to Courses (N to N)
class ApplicationCourse
{
// which Application
private $application;
// which Course
private $course;
}
但我想弄清楚如何把它变成一种形式。我不介意将表单绑定到一个数组,然后将其排序,将其放入应用程序中
class ApplicationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...other fields
;
// Add the groups of courses to choose
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function(FormEvent $event) use ($offering) {
$form = $event->getForm();
// Here I would like to add 1 EntityType per Offering
// The EntityType allows the user to select multiple Courses within that group
$i = 0;
foreach ($offering->groups as $group) {
// Maybe add the $group as a static block in the twig template here
// Ideally the form should show the group name/description
// I'll probably borrow from this class https://github.com/genemu/GenemuFormBundle/blob/master/Form/Core/Type/PlainType.php
// This adds a group of checkboxes to select a course from this group
$form->add('course_'.$i, 'entity', array(
'class' => 'Acme\DemoBundle\Entity\Course',
'property' => 'name',
'multiple' => true,
'expanded' => true,
'query_builder' => function(EntityRepository $er) use ($group) {
// imagine this ia a query that selects all courses in the $group
return $er->createGroupCoursesQueryBuilder($group);
},
);
$i++;
}
}
);
}
public function getName()
{
return 'task';
}
}
最后,我想要一个如下所示的表单:
class Offering
{
// has multiple CourseGroups
private $groups;
}
class CourseGroup
{
// which Offering it belongs to
private $offering;
private $name;
private $maxHours;
}
class Course
{
// which CourseGroup it belongs to
private $group;
private $name;
private $hours;
}
// User submits an application with chosen courses from multiple groups
class Application
{
// ...
private $courses;
}
// Joins the Applications to Courses (N to N)
class ApplicationCourse
{
// which Application
private $application;
// which Course
private $course;
}
科学:选择至少4个学时
- [x] SCI100(2小时)
- []SCI101(2小时)
- []SCI102(2小时)
数学:选择至少4个学时
- []MTH100(4小时)
- [x] MTH101(4小时)
{组名称}:{组描述}
- [{选定?}]{课程名称}{课程时数}
等等
这种方法有效吗?有没有更好的方法,使我不必将表单绑定到数组,然后在验证后重新组装它?看到这个答案后,解决方案实际上很简单
我刚刚定制了表单的呈现。最棒的是,我得到了Symfony表单的所有有用部分(数据绑定),但我可以完全自定义渲染
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('courses', 'entity', array(
'class' => 'MyMainBundle:Course',
'property' => 'name',
'multiple' => true,
'expanded' => true
))
->add('save', 'submit')
;
}
在细枝模板中有一个很酷的技巧,就是自己渲染它。我确实必须通过这几组课程
诀窍是使用{%do form.courses.setRendered%}
{{ form_start(form) }}
{{ form_errors(form) }}
{% for group in academics.courseGroups %}
<section>
<h2>
{{ group.name }}
</h2>
<p>{{ group.description }}</p>
{% for course in group.courses %}
<div class="form-group">
<div class="course">
<div class="checkbox">
<label for="my_mainbundle_application_course_courses_{{ course.id }}">
<input type="checkbox"
id="my_mainbundle_application_course_courses_{{ course.id }}"
name="my_mainbundle_application_course[courses][]"
value="{{ course.id }}"
{% if course.id in form.courses.vars.value and form.courses.vars.value[course.id] %}checked="checked"{% endif %}>
<span class="name">{{ course.name }}</span>
<span class="subject">({{ course.subject }})</span>
-
<span class="credits">{{ course.credits }} hours</span>
</label>
</div>
</div>
</div>
{% endfor %}
</section>
{% endfor %}
{% do form.courses.setRendered %}
{{ form_row(form.save) }}
{{ form_end(form) }}
{{form_start(form)}
{{form_errors(form)}}
{academics.courseGroups%中的组为%s}
{{group.name}
{{group.description}}
{组中课程的百分比。课程%}
{{course.name}
({{course.subject}})
-
{{course.credits}}小时
{%endfor%}
{%endfor%}
{%do form.courses.setRendered%}
{{form_行(form.save)}
{{form_end(form)}}
这些问题很有帮助:设置渲染属性的选项也很有帮助!