问题描述
首先,我正在使用 Symfony 组件进行构建.我正在使用 3.4.我正在关注表单教程
这太棒了!我以为我在路上.所以,在我的控制器中,我添加了这一行.
$form = Forms::createFormFactory();
当我尝试加载页面时,一切顺利,没有错误消息,直到我添加了接下来的两行.
->addExtension(new HttpFoundationExtension())->getForm();
我删除了 ->addExtension(new HttpFoundationExtension()) 行并留下了 ->getForm() 认为它会在没有 add 方法调用的情况下进行处理.它没.因此,我进行了备份以查看 IDE 是否会为我键入提示.
在 IDE PHPStorm 中,这些是我可以访问的方法,但不能按照教程使用 getForm
我尝试过的每个教程都以找不到一些不存在的方法而告终.我需要安装什么才能访问 ->getForm() 方法?
更新:我已经向前走了几步.
$form = Forms::createFormFactory()->createBuilder(TaskType::class);
上面的代码加载没有错误.(为什么还是模糊).但下一站是 createView().也不存在.我只得到 create() 的暗示.在此视频中的字里行间阅读有助于完成最后两个步骤.
这就是我现在拥有的.
$session = new Session();$csrfManager = new CsrfTokenManager();$help = new \Twig_ExtensionInterface();$formFactory = Forms::createFormFactoryBuilder()->getFormFactory();$form = $formFactory->createBuilder(TaskType::class)->getForm();//$form->handleRequest();$loader = new FilesystemLoader('../../templates/billing');$twig = 新环境($loader, ['调试' =>真的,]);$twig->addExtension(new HeaderExtension());$twig->addExtension(new DebugExtension());$twig->addExtension($help, FormRendererEngineInterface::class);return $twig->render('requeueCharge.html.twig', ['付款' =>'收费','报表' =>$form->createView()]);
例如,有人知道独立更新吗?大家一直指着二的那个是6岁.在那段时间里,有很多东西被弃用了.所以,这不是一个可以遵循的例子.
你的 Form 类和方法 createFormFactory
必须返回实现 FormBuilderInterface 然后 getForm
方法将可用.您需要创建 formBuilder
对象.
但这不能从静态方法中调用,因为 formBuilder
对象需要依赖于 DI 容器.查看控制器.
如果您需要在 DI 中注册您自己的类并创建具有依赖项的 formBuilder 对象并返回该对象实例.
编辑您不需要使用抽象控制器.您可以创建自己的类,该类在 DI 中注册以获取依赖项.在该类中,您创建方法来创建新的 FormBuilder(..dependencies from DI from your class ...) 并返回该 FormBuilder 的实例.然后你可以通过 DI 在控制器中注入你的类.
示例(未测试)
//DI 中注册的类类 CustomFormFactory{私人 $_factory;私人 $_dispatcher;公共 CustomFormFactory(EventDispatcherInterface $dispatcher, FormFactoryInterface $factory){$_dispatcher = $dispatcher;$_factory = $factory;}公共函数 createForm(?string $name, ?string $dataClass, array $options = []): FormBuilderInterface{//结合 DI 依赖项(工厂..)和您的参数创建实例return new FormBuilder($name, $dataClass, $_dispatcher, $_factory, $options);}}
使用
$factory = $this->container->get('CustomFormFactory');$fb = $factory->createForm();$form = $fb->getForm();
First off, I am building using Symfony components. I am using 3.4. I was following the form tutorial https://symfony.com/doc/3.4/components/form.html which lead me to this pagehttps://symfony.com/doc/current/forms.html#usage
I noticed that Symfony added a Form directory to my application.
This was great! I thought I was on my way. So, in my controller, I added this line.
$form = Forms::createFormFactory();
When I tried loading the page, everything went well with no error messages until I added the next two lines.
->addExtension(new HttpFoundationExtension())
->getForm();
I removed the ->addExtension(new HttpFoundationExtension()) line and left the ->getForm() thinking it would process without the add method call. It did not. So, I backed up to see if the IDE would type hint for me.
In the IDE PHPStorm, these are the methods that I have access to but not getForm per the tutorial
Every tutorial I have tried ends with not being able to find some method that does not exist. What do I need to install in order to have access to the ->getForm() method?
UPDATE:I have made a couple of steps forward.
$form = Forms::createFormFactory()
->createBuilder(TaskType::class);
The code above loads with no errors. (Why is still fuzzy). But next stop is the createView(). None existant also. I only get hinted with create().Reading between the lines in this video help with the last two steps. https://symfonycasts.com/screencast/symfony3-forms/render-form-bootstrap#play
UPDATE 2:
This is what I have now.
$session = new Session();
$csrfManager = new CsrfTokenManager();
$help = new \Twig_ExtensionInterface();
$formFactory = Forms::createFormFactoryBuilder()
->getFormFactory();
$form = $formFactory->createBuilder(TaskType::class)
->getForm();
//$form->handleRequest();
$loader = new FilesystemLoader('../../templates/billing');
$twig = new Environment($loader, [
'debug' => true,
]);
$twig->addExtension(new HeaderExtension());
$twig->addExtension(new DebugExtension());
$twig->addExtension($help, FormRendererEngineInterface::class);
return $twig->render('requeueCharge.html.twig', [
'payments' => 'Charge',
'reportForm' => $form->createView()
]);
Does anyone know of an update standalone for example? The one that everyone keeps pointing two is 6 years old. There have been many things deprecated in that time period. So, it is not an example to follow.
Your Form class and method createFormFactory
must return object that implement FormBuilderInterface then getForm
method will be available. You need create formBuilder
object.
But this can't be called from static method because formBuilder
object need dependency from DI container. Look at controller.
If you want you need register your own class in DI and create formBuilder object with dependencies and return that instance of object.
EDITYou don't need to use abstract controller. You can create your own class which is registered in DI for geting dependencies. In that class you create method which create new FormBuilder(..dependencies from DI from your class ...) and return instance of that FormBuilder. Then you can inject your class in controller via DI.
Example (not tested)
// class registered in DI
class CustomFormFactory
{
private $_factory;
private $_dispatcher;
public CustomFormFactory(EventDispatcherInterface $dispatcher, FormFactoryInterface $factory)
{
$_dispatcher = $dispatcher;
$_factory = $factory;
}
public function createForm(?string $name, ?string $dataClass, array $options = []): FormBuilderInterface
{
// create instance in combination with DI dependencies (factory..) and your parameters
return new FormBuilder($name, $dataClass, $_dispatcher, $_factory, $options);
}
}
Usage
$factory = $this->container->get('CustomFormFactory');
$fb = $factory->createForm();
$form = $fb->getForm();
这篇关于Symfony3 CreateFormFactory 的 ->getForm() 方法在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!