Reputation: 5567
I am trying to develop a form that lets students select courses from several groups. It is defined as follows:
So I want the form to allow students to select courses, but the courses are organized in those groups with instructions about how many to pick. The credit hours part will be validated with custom Validation rules.
Here's an idea of the code I'm thinking of (omitting a lot of the namespaces/Doctrine mapping/etc.).
The entities are sort of like this:
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;
}
But I'm trying to figure out how to put this in a form. I don't mind just binding the form to an array and sorting it out later to put it in an Application
.
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';
}
}
In the end, I want a form that looks like this:
Science: select at least 4 credit hours
Math: select at least 4 credit hours
{GROUP NAME}: {GROUP DESCRIPTION}
Etc.
Will this method work? Is there a better way so that I don't have to bind that form to an array and then re-assemble it after validation?
Upvotes: 1
Views: 1792
Reputation: 5567
The solution was actually simple after seeing this answer Symfony 2 Forms entity Field Type grouping
I just customized the rendering of the form. The best part is, I get all the helpful parts of Symfony forms (the data binding), but I can completely customize the rendering.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('courses', 'entity', array(
'class' => 'MyMainBundle:Course',
'property' => 'name',
'multiple' => true,
'expanded' => true
))
->add('save', 'submit')
;
}
There's a cool trick in the twig template to just rendering it myself. I did have to pass in the groups of courses.
The trick is to use {% 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) }}
Upvotes: 2