Symfony:参数传递到窗体保持默认

tf7tbtn2  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(105)

与最新的Symfony,我有一个自定义表单,其中一个字段是根据实体的教义请求.为了构造这个请求它需要一个参数id通过控制器,与选项'postedBy'

class StudyDecomposedType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            
            ->add('subjects', EntityType::class,[
                'class' => Subject::class,
                'choice_label' => 'identifier',
                'required' => false,
                'multiple' => true,
                'query_builder' => function (EntityRepository $er) use ($options): QueryBuilder {
                    return $er->createQueryBuilder('a')
                        ->leftJoin("a.studies", "s")
                        ->leftJoin(Studyaccess::class, 'sa', 'WITH', 's=sa.study')
                        ->where('(sa.user = :userid AND (sa.r = 1 OR sa.w = 1)) OR (a.owner= :userid)')
                        ->setParameter('userid', $options['postedBy']);
                        
                        
                },
                ])
            ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            
                'postedBy' => 0,
            

        ]);

        $resolver->setAllowedTypes('postedBy', 'int');
        
    }

在我的控制器中,我这样做的形式:

$form = $this->createForm(StudyDecomposedType::class, 
                   $options= ['postedBy' => $this->getUser()->getId(),] 
        );

但是,无论我在选项中输入什么值,请求中的值都是我在默认情况下输入的值(这里为0)。
有人能帮帮我吗,我完全迷路了
参数userid应该是通过从crontroller调用$options给出的,但该值从未更改。我在控制器中验证了id,我查看了生成的sql,userid始终为默认值

efzxgjgh

efzxgjgh1#

RISKForm()方法期望选项作为第三个参数,但您将它们作为第二个参数发送,用于发送数据以填充表单字段。
通过传递null作为第二个参数(这是实际的默认值)来修复顺序:

$form = $this->createForm(StudyDecomposedType::class, null, [
    'postedBy' => $this->getUser()->getId(),
]);

如果你使用PHP 8+,你可以使用命名参数:

$form = $this->createForm(StudyDecomposedType::class, options: [
    'postedBy' => $this->getUser()->getId(),
]);

相关问题