如何在Symfony 3.0中的FormType上注入容器?
我的services.yml文件:
services: advertiser.form.report: class: App\AdvertiserBundle\Form\ReportType arguments: ["@service_container"]
在动作控制器中:
$report = $this->get( 'advertiser.form.report' ); $form = $this->createForm( $report );
我收到了这个错误:
Expected argument of type “string”,“App\AdvertiserBundle\Form\ReportType” given
请改用表格工厂.
原文链接:https://www.f2er.com/php/136991.html在配置中:
services: advertiser.form.report: class: 'Symfony\Component\Form\Form' factory: ['@form.factory','create'] arguments: [App\AdvertiserBundle\Form\ReportType,null,{container: '@service_container'}]
在您的表单中添加容器到可能的表单选项:
/** * @param OptionsResolver $resolver */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'container' => null,//The rest of options ]); }
并根据需要使用它:
public function buildForm(FormBuilderInterface $builder,array $options) { $container = $options['container']; //The rest of logic }
在您的控制器中,只需使用表单:
$this->get('advertiser.form.report'); //It's a ready-to-use form,you don't need to call $this->createForm()