I am trying to develop a form that lets students select courses from several groups. It is defined as follows:
- Courses have a name/description/hours.
- Courses belong to groups, which have a name/description/limits on the hours (e.g. you must choose 5 credit hours from this group, or you can only take up to 10 hours from that group)
- Course Groups are organized into a Program Offering, which could have other fields
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
- [x] SCI100 (2 hours)
- [ ] SCI101 (2 hours)
- [ ] SCI102 (2 hours)
Math: select at least 4 credit hours
- [ ] MTH100 (4 hours)
- [x] MTH101 (4 hours)
{GROUP NAME}: {GROUP DESCRIPTION}
- [{selected?}] {COURSE NAME} {COURSE HOURS}
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?