脚本宝典收集整理的这篇文章主要介绍了php – Symfony 3在FormType中注入容器,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
services: advertiser.form.report: class: App\AdvertiserBundle\Form\ReportType arguments: ["@service_container"]
在动作控制器中:
$report = $this->get( 'advertiser.form.report' ); $form = $this->createForm( $report );
我收到了这个错误:
在配置中:
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()
以上是脚本宝典为你收集整理的php – Symfony 3在FormType中注入容器全部内容,希望文章能够帮你解决php – Symfony 3在FormType中注入容器所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。