动态添加字段到 CollectionType
Dynamically add fields to CollectionType
我必须创建一个学校管理员。当老师必须提交评估结果时,他必须通过评分等级来评估给定的技能。
Evaluation
------一对多------ Scale
------多对一------ Skill
评估类型
class EvaluationType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('scales', CollectionType::class, [
'entry_type' => ScaleType::class
]);
}
}
ScaleType
class ScaleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('value');
}
}
我想动态添加 ScaleType
children 到 EvaluationType::scales
形式 child 给定的 Skill
集合。
这是我迄今为止在控制器操作中尝试过的内容:
public function newEvaluation(Classroom $classroom, Subject $subject, Period $period)
{
$evaluation = new Evaluation();
$evaluation->setClassroom($classroom);
$evaluation->setSubject($subject);
$evaluation->setPeriod($period);
$form = $this->createForm(EvaluationType::class, $evaluation);
$skills =$this->getDoctrine()->getRepository('AppBundle:Skill')->findAll();
foreach($skills as $skill) {
$scale = new Scale();
$scale->setEvaluation($evaluation);
$scale->setSkill($skill);
$form['scales']->add($this->createForm(ScaleType::class, $scale, [
'auto_initialize' => false
]));
}
return $this->render('classrooms/newEvaluation.html.twig', [
'form' => $form->createView(),
'classroom' => $classroom,
'subject' => $subject
]);
}
问题是我在渲染的 scales
字段中只得到一个 sub-form,看起来当我将 children 添加到 $form['scales']
时它会覆盖上一个children.
我想我做的完全错了。我怎样才能做到这一点?
我还指出,我无法先创建 Evaluation
,然后在另一页上创建所有 Scale
。它需要在相同的 controller/page.
中完成
编辑:我要补充一点,我不希望 scales
的 children 是可扩展的,它们必须是由服务器修复,而不是被客户端获取。
解决了,感谢 Artamiel,这是我的新控制器操作:
public function newEvaluation(Classroom $classroom, Subject $subject, Period $period)
{
$evaluation = new Evaluation();
$evaluation->setClassroom($classroom);
$evaluation->setSubject($subject);
$evaluation->setPeriod($period);
$skills = $this->getDoctrine()->getRepository('AppBundle:Skill')->findAll();
foreach ($skills as $skill) {
$scale = new Scale();
$scale->setSkill($skill);
$scale->setEvaluation($evaluation);
}
$form = $this->createForm(EvaluationType::class, $evaluation);
return $this->render('classrooms/newEvaluation.html.twig', [
'form' => $form->createView(),
'classroom' => $classroom,
'subject' => $subject
]);
}
我必须创建一个学校管理员。当老师必须提交评估结果时,他必须通过评分等级来评估给定的技能。
Evaluation
------一对多------ Scale
------多对一------ Skill
评估类型
class EvaluationType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('scales', CollectionType::class, [
'entry_type' => ScaleType::class
]);
}
}
ScaleType
class ScaleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('value');
}
}
我想动态添加 ScaleType
children 到 EvaluationType::scales
形式 child 给定的 Skill
集合。
这是我迄今为止在控制器操作中尝试过的内容:
public function newEvaluation(Classroom $classroom, Subject $subject, Period $period)
{
$evaluation = new Evaluation();
$evaluation->setClassroom($classroom);
$evaluation->setSubject($subject);
$evaluation->setPeriod($period);
$form = $this->createForm(EvaluationType::class, $evaluation);
$skills =$this->getDoctrine()->getRepository('AppBundle:Skill')->findAll();
foreach($skills as $skill) {
$scale = new Scale();
$scale->setEvaluation($evaluation);
$scale->setSkill($skill);
$form['scales']->add($this->createForm(ScaleType::class, $scale, [
'auto_initialize' => false
]));
}
return $this->render('classrooms/newEvaluation.html.twig', [
'form' => $form->createView(),
'classroom' => $classroom,
'subject' => $subject
]);
}
问题是我在渲染的 scales
字段中只得到一个 sub-form,看起来当我将 children 添加到 $form['scales']
时它会覆盖上一个children.
我想我做的完全错了。我怎样才能做到这一点?
我还指出,我无法先创建 Evaluation
,然后在另一页上创建所有 Scale
。它需要在相同的 controller/page.
编辑:我要补充一点,我不希望 scales
的 children 是可扩展的,它们必须是由服务器修复,而不是被客户端获取。
解决了,感谢 Artamiel,这是我的新控制器操作:
public function newEvaluation(Classroom $classroom, Subject $subject, Period $period)
{
$evaluation = new Evaluation();
$evaluation->setClassroom($classroom);
$evaluation->setSubject($subject);
$evaluation->setPeriod($period);
$skills = $this->getDoctrine()->getRepository('AppBundle:Skill')->findAll();
foreach ($skills as $skill) {
$scale = new Scale();
$scale->setSkill($skill);
$scale->setEvaluation($evaluation);
}
$form = $this->createForm(EvaluationType::class, $evaluation);
return $this->render('classrooms/newEvaluation.html.twig', [
'form' => $form->createView(),
'classroom' => $classroom,
'subject' => $subject
]);
}