Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.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 Symfony从buildForm向表单注入参数_Php_Symfony_Symfony Forms - Fatal编程技术网

Php Symfony从buildForm向表单注入参数

Php Symfony从buildForm向表单注入参数,php,symfony,symfony-forms,Php,Symfony,Symfony Forms,我使用Symfony以防您需要将容器已知参数传递给自定义表单类型,您可以按照上面尝试的方式(显然是通过参数注入)。但是,如果要将数据从控制器向下传递到表单类型,可以通过$options(最后一个)参数(在buildForm中)传递数据: 特征形式 然后,在TagFeatureType中应配置选项: TagFeatureType 最后,将其包含在buildForm中: public function buildForm(FormBuilderInterface $builder, array $o

我使用Symfony以防您需要将容器已知参数传递给自定义表单类型,您可以按照上面尝试的方式(显然是通过参数注入)。但是,如果要将数据从控制器向下传递到表单类型,可以通过
$options
(最后一个)参数(在
buildForm
中)传递数据:

特征形式

然后,在
TagFeatureType
中应配置选项:

TagFeatureType

最后,将其包含在
buildForm
中:

public function buildForm(FormBuilderInterface $builder, array $options)

    // ....
    $someCustomParam = $options['some_custom_param'];

    // .... 

    $builder->add('tag', EntityType::class, array(
        'choice_label' => 'name',
        'class' => 'AppBundle:Tag',
        'query_builder' => function(TagRepository $tr) use ($someCustomParam) 
        {
            return $tr->findObjectNotMine($someCustomParam);
        }
    );

    // ....
}
这样做的明显缺点是路径中的所有表单都需要
setRequired
setDefault


希望这有助于…

如果您需要将容器已知参数传递给自定义表单类型,您可以按照上面尝试的方式进行(显然是通过参数注入)。但是,如果要将数据从控制器向下传递到表单类型,可以通过
$options
(最后一个)参数(在
buildForm
中)传递数据:

特征形式

然后,在
TagFeatureType
中应配置选项:

TagFeatureType

最后,将其包含在
buildForm
中:

public function buildForm(FormBuilderInterface $builder, array $options)

    // ....
    $someCustomParam = $options['some_custom_param'];

    // .... 

    $builder->add('tag', EntityType::class, array(
        'choice_label' => 'name',
        'class' => 'AppBundle:Tag',
        'query_builder' => function(TagRepository $tr) use ($someCustomParam) 
        {
            return $tr->findObjectNotMine($someCustomParam);
        }
    );

    // ....
}
这样做的明显缺点是路径中的所有表单都需要
setRequired
setDefault


希望这有帮助…

在TagFeatureType中,您可以通过构造函数注入传入参数:

class TagFeatureType extends AbstractType
{
    private $tagRepository;

    public function __construct(TagRepository $tagRepository)
    {
        $this->tagRepository = $tagRepository;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $tr = $this->tagRepository;
        // ...
        ->add('tag', EntityType::class,
            array(
                'choice_label' => 'name',
                'class' => 'AppBundle:Tag',
                'query_builder' => function(TagRepository $tr){
                    return $tr->findObjectNotMine();
                }
            )
        )
    }
}

有一种方法将检查表单类型是否已在服务容器中注册,然后将其注入,而不是创建新实例。这应该确保标记存储库存在。

在TagFeatureType中,您可以通过构造函数注入传入参数:

class TagFeatureType extends AbstractType
{
    private $tagRepository;

    public function __construct(TagRepository $tagRepository)
    {
        $this->tagRepository = $tagRepository;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $tr = $this->tagRepository;
        // ...
        ->add('tag', EntityType::class,
            array(
                'choice_label' => 'name',
                'class' => 'AppBundle:Tag',
                'query_builder' => function(TagRepository $tr){
                    return $tr->findObjectNotMine();
                }
            )
        )
    }
}

有一种方法将检查表单类型是否已在服务容器中注册,然后将其注入,而不是创建新实例。这应该确保标记存储库存在。

是的,它工作正常。我不知道我只能为一个表单更改额外选项。我将您的帖子设置为答案,您是否可以编辑您的答案,因为您通过$this访问参数,它在Symfony 2.8中不起作用(可能以后会起作用?)。我必须通过$options访问。在那之后,你的答案将是完美的!您甚至更新了闭包;)是的,很好用。我不知道我只能为一个表单更改额外选项。我将您的帖子设置为答案,您是否可以编辑您的答案,因为您通过$this访问参数,它在Symfony 2.8中不起作用(可能以后会起作用?)。我必须通过$options访问。在那之后,你的答案将是完美的!您甚至更新了闭包;)
public function buildForm(FormBuilderInterface $builder, array $options)

    // ....
    $someCustomParam = $options['some_custom_param'];

    // .... 

    $builder->add('tag', EntityType::class, array(
        'choice_label' => 'name',
        'class' => 'AppBundle:Tag',
        'query_builder' => function(TagRepository $tr) use ($someCustomParam) 
        {
            return $tr->findObjectNotMine($someCustomParam);
        }
    );

    // ....
}
class TagFeatureType extends AbstractType
{
    private $tagRepository;

    public function __construct(TagRepository $tagRepository)
    {
        $this->tagRepository = $tagRepository;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $tr = $this->tagRepository;
        // ...
        ->add('tag', EntityType::class,
            array(
                'choice_label' => 'name',
                'class' => 'AppBundle:Tag',
                'query_builder' => function(TagRepository $tr){
                    return $tr->findObjectNotMine();
                }
            )
        )
    }
}