Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “我如何发送?”;额外的;是否使用FormBuilder将数据转换为Symfony 3.2.x表单?_Php_Symfony_Symfony Forms - Fatal编程技术网

Php “我如何发送?”;额外的;是否使用FormBuilder将数据转换为Symfony 3.2.x表单?

Php “我如何发送?”;额外的;是否使用FormBuilder将数据转换为Symfony 3.2.x表单?,php,symfony,symfony-forms,Php,Symfony,Symfony Forms,我在Symfony 3.2.7控制器中有以下方法: public function updateAction(Request $request) { $em = $this->getDoctrine()->getManager(); // this is the original entity without modified values $entity = $em->getRepository('QuoteBundle:Quote

我在Symfony 3.2.7控制器中有以下方法:

public function updateAction(Request $request)
{
    $em        = $this->getDoctrine()->getManager();
    // this is the original entity without modified values
    $entity    = $em->getRepository('QuoteBundle:Quote')->find($request->query->get('id'));

    // the entity passed to the form has the values modified coming from the request    
    $form = $this->createForm(CFProgramLevelQuoteType::class, $entity);
    ...
}
以下是表格:

class CFProgramLevelQuoteType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('CFProgramLevel', EntityType::class, [
                'class'         => 'QuoteBundle:CFProgramLevel',
                'choice_label'  => 'description',
                'placeholder'   => 'Please select a program',
            ]);
    }
    ...
}
我将使用查询生成器从
QuoteBundle:CFProgramLevel
中筛选一些值,因此我需要从
$entity
中获取未修改的ID并将其发送到表单。这就是我目前想到的解决方案:

public function updateAction(Request $request)
{
    $em        = $this->getDoctrine()->getManager();
    $entity    = $em->getRepository('QuoteBundle:Quote')->find($request->query->get('id'));
    $entity_id = $entity->getCfProgramLevel()->getcfProgramLevelId();   
    $form = $this->createForm(CFProgramLevelQuoteType::class, $entity);
    ...
}
但是我如何才能将
实体id
传递到表单并在那里使用它呢?如果有更好的方法来实现这一点,我愿意听取意见。我在谷歌上找不到任何有用的东西,所以非常感谢你的帮助

更新

尝试下面答案中提供的解决方案时,我也无法使其工作:

// controller
$form = $this->createForm(CFProgramLevelQuoteType::class, $entity, ['entity_id' => $entity_id]);

// form
public function buildForm(FormBuilderInterface $builder, array $options)
{
    dump($options);
    die();
    ...
}

public function setDefaultOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(['data_class' => Quote::class, 'entity_id' => null]);
    $resolver->setRequired('entity_id');
}
选项“实体id”不存在。定义的选项是:“操作”, “允许额外字段”、“属性”、“自动初始化”、“块名称”, “按引用”、“复合”、“约束”、“csrf\u字段\u名称”, “csrf_消息”、“csrf_保护”、“csrf_令牌_id”, “csrf_令牌管理器”、“数据”、“数据类”、“禁用”、“空数据”, “错误冒泡”、“错误映射”、“额外字段\u消息”, “继承\u数据”、“无效\u消息”、“无效\u消息\u参数”, “标签”、“标签属性”、“标签格式”、“映射”、“方法”, “post_max_size_message”、“property_path”、“required”, “翻译域”、“修剪”、“上传最大大小消息”, “验证组”


您可以为表单类型创建自己的自定义选项,如中所述

通过实体\u id:

$form = $this->createForm(CFProgramLevelQuoteType::class, $entity, [
    'entity_id' => $entity_id
]);

您可以为表单类型创建自己的自定义选项,如中所述

通过实体\u id:

$form = $this->createForm(CFProgramLevelQuoteType::class, $entity, [
    'entity_id' => $entity_id
]);

这是一个完全有效的示例(也非常类似于@striker)。如果没有拼写错误,请仔细检查。否则请尝试
php bin/console缓存:清除

控制器操作:

public function testAction(Request $request)
{
    $form = $this->createForm(TestType::class, null, [
        'option1' => 6
    ]);
    return $this->render('default/test.html.twig',[
        'form' => $form->createView()
    ]);
}
TestType.php

class TestType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('test', IntegerType::class, [
            'data' => $options['option1']
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setRequired('option1');
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'appbundle_test';
    }
}

这是一个完全有效的示例(也非常类似于@striker)。如果没有拼写错误,请仔细检查。否则请尝试
php bin/console缓存:清除

控制器操作:

public function testAction(Request $request)
{
    $form = $this->createForm(TestType::class, null, [
        'option1' => 6
    ]);
    return $this->render('default/test.html.twig',[
        'form' => $form->createView()
    ]);
}
TestType.php

class TestType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('test', IntegerType::class, [
            'data' => $options['option1']
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setRequired('option1');
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'appbundle_test';
    }
}

这对我不起作用
选项“entity\u id”不存在。定义的选项是…
您的方法setDefaultOptions甚至没有被调用,因为它在symfony 2.7之后就被弃用了。您必须使用configureOptions方法。请仔细检查代码这对我不起作用
选项“entity\u id”不存在。定义的选项是…
您的方法setDefaultOptions甚至没有被调用,因为它在symfony 2.7之后就被弃用了。您必须使用configureOptions方法。请仔细检查代码。在这种情况下,您可能会尝试使用表单实体查询生成器:@AndrewNolan我不知道这会有什么帮助……因此,您希望在更新之前获取原始的
getcfProgramLevelId
,并具有一个基于
getcfProgramLevelId
值的数据库筛选结果的选择字段?可能会在这种情况下,请尝试使用表单实体查询生成器:@AndrewNolan我不知道这在这里有什么帮助……因此,您希望在更新之前获取原始的
getcfProgramLevelId
,并具有一个选择字段,其中包含根据
getcfProgramLevelId
值从数据库筛选的结果?