symfony 3 UndefinedOptionsException - 从数据库下拉列表

时间:2016-09-14 09:44:55

标签: php symfony symfony-forms

我有两个实体学生课程,我想创建一个表单,显示学生详细信息和课程的下拉列表/ chocelist,并为学生分配一个课程。这些类也应来自DB表,因此控制器就是这样。

public function studentAddClassAction( $id, Request $request )
{
    // get the student from the student table
    $em     = $this->getDoctrine()->getManager();
    $student= $em->getRepository('PIE10Bundle:Student')->find($id);

    // new class object and create the form
    $class  = new Classes;
    $form   = $this->createForm(ClassesType::class, $class);
    $form->handleRequest($request);

    if( $form->isSubmitted() && $form->isValid() )
    {
        // form operation - update student row with the classID
    }

    return $this->render(
                        'PIE10Bundle:student:layout_student_addclass.html.twig',
                        array(
                            'student'=> $student,
                            'title'  => 'Add Class',
                            'tables' => 1,
                            'form'   => $form->createView()
                        )
    );
}

和我的 ClassesType 如下所示

class ClassesType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('classes',
                      ClassesType::class,
                      array(
                            'class'     => 'PIE10Bundle:Classes',
                            'property'  => 'className',
                            'expanded'  => false,
                            'multiple'  => false
                    ));
        $builder->add('Add Class',
                      SubmitType::class,
                      array('attr'  =>  array('class' => 'btn btn-primary',
                                              'style' => 'margin:15px 0;')) );
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'PIE10Bundle\Entity\Classes',

        ));
    }
}

当我尝试访问此表单时,我收到以下 500内部服务器错误 - UndefinedOptionsException 错误

  

选项" class","扩展","多个","属性"不存在。定义的选项包括:"操作"," allow_extra_fields"," attr"," auto_initialize"," block_name",& #34; by_reference","复合","约束"," csrf_field_name"," csrf_message"," csrf_protection&# 34;," csrf_token_id"," csrf_token_manager"," data"," data_class"," description",&# 34;禁用"," empty_data"," error_bubbling"," error_mapping"," extra_fields_message"," inherit_data&#34 ;," invalid_message"," invalid_message_parameters"," label"," label_attr"," label_format",&#34 ;映射","方法"," post_max_size_message"," property_path"," required"," translation_domain" ," trim"," validation_groups"。

所以我需要知道出了什么问题以及如何解决这个问题。如果需要任何其他信息,请告诉我。感谢

2 个答案:

答案 0 :(得分:1)

@echo %%A | find /i "\" 更改:

ClassesType

为:

$builder->add('classes', ClassesType::class, ...

还添加:

$builder->add('classes', EntityType::class, ...

位于文件顶部,以供其使用。

目前您正在使用use Symfony\Bridge\Doctrine\Form\Type\EntityType; 本身(如果不是错误,可能会导致无限循环)。此类型不会延伸ClassesType,因此您使用的选项(EntityTypeclassexpandedmultiple)不存在。

同样不推荐property,您应该使用property,除非您使用的是相当旧版本的symfony。

参考:EntityType Field

答案 1 :(得分:0)

使用@Yoshi回答我改变了

$builder->add('classes', ClassesType::class, ...

$builder->add('classes', EntityType::class, ...

并添加了

use Symfony\Bridge\Doctrine\Form\Type\EntityType; ClassesType 文件,然后更新控制器,如下所示

public function studentAddClassAction( $id, Request $request )
{
    // get the student from the student table
    $em     = $this->getDoctrine()->getManager();
    $student= $em->getRepository('PIE10Bundle:Student')->find($id);

    // new class object and create the form
    $class  = $em->getRepository('PIE10Bundle:Classes')->findAll();
    $form   = $this->createForm(ClassesType::class, $class);
    $form->handleRequest($request);

    if( $form->isSubmitted() && $form->isValid() )
    {
        // form operation - update student row with the classID
    }

    return $this->render(
                        'PIE10Bundle:student:layout_student_addclass.html.twig',
                        array(
                            'student'=> $student,
                            'title'  => 'Add Class',
                            'tables' => 1,
                            'form'   => $form->createView()
                        )
    );
}

然后 ClassesType 如下

class ClassesType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('classes',
                      EntityType::class,
                      array(
                            'class'     => 'PIE10Bundle:Classes',
                            'expanded'  => false,
                            'multiple'  => false
                    ));
        $builder->add('Add Class',
                      SubmitType::class,
                      array('attr'  =>  array('class' => 'btn btn-primary',
                                              'style' => 'margin:15px 0;')) );
    }
}
@Yoshi回答帮我解决了错误