【问题标题】:Pass user class to Symfony 4 form将用户类传递给 Symfony 4 表单
【发布时间】:2020-01-09 12:39:51
【问题描述】:

我在此列中有 StudentCourse 实体

/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\User\RegisteredUser", inversedBy="studentCourses")
 * @ORM\JoinColumn(nullable=false)
 */
private $student;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Course\Course", inversedBy="studentCourses")
 * @ORM\JoinColumn(nullable=false)
 */
private $course;

然后我有表单类型

namespace App\Form\StudentCourse;

use App\DataHolder\StudentCourse\StudentCourseNewDetails;
use App\Entity\Course\Course;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class NewStudentCourseType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
       $builder
            ->add('course', EntityType::class, [
                'class' => Course::class,
                'choice_label' => 'title'
            ])
            ->add('student', HiddenType::class, [
                'data'=>$options['student']
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => StudentCourseNewDetails::class
        ]);
    }
}

在控制器中

$form = $this->createForm(NewStudentCourseType::class, $scnd, [
                'student' => $user
        ]);

当我尝试显示这个错误时,我得到了

“学生”选项不存在。定义的选项有:“action”、“allow_extra_fields”、“allow_file_upload”、“attr”、“attr_translation_parameters”、“auto_initialize”、“block_name”、“block_prefix”、“by_reference”、“compound”、“constraints”、“csrf_field_name "、"csrf_message"、"csrf_protection"、"csrf_token_id"、"csrf_token_manager"、"data"、"data_class"、"disabled"、"empty_data"、"error_bubbling"、"error_mapping"、"extra_fields_message"、"help"、 “help_attr”、“help_html”、“help_translation_parameters”、“inherit_data”、“invalid_message”、“invalid_message_parameters”、“label”、“label_attr”、“label_format”、“label_translation_parameters”、“mapped”、“method”、“post_max_size_message "、"property_path"、"required"、"translation_domain"、"trim"、"upload_max_size_message"、"validation_groups"。

【问题讨论】:

    标签: forms symfony4


    【解决方案1】:

    问题在于您的configureOptions()。向其中添加一个新索引student

        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults([
                'data_class' => StudentCourseNewDetails::class,
                'student' => null
            ]);
        }
    }
    

    【讨论】:

    • StudentCourseNewDetails 是作为 StudentCourse 实体的数据转换对象的类
    • 你是对的,但在代码中 createForm 中的另一个问题必须是 $user->getId() 而不是用户作为对象
    • 如果你这样做$form = $this->createForm(NewStudentCourseType::class, $scnd, ['student' => $user->getId()]);,它应该可以工作。你有任何错误吗?
    • 是的,此代码正在运行,但是当提交表单时 StudentCourseNewDetails 实例在学生 ID 中但我需要用户对象
    • 好的。这个新问题似乎有点超出了这个问题的范围。看看this answer。我相信它可以解决您面临的同样问题。如果没有,请添加一个新问题,以保持这个问题的范围。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 2021-01-29
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 2019-05-25
    相关资源
    最近更新 更多