简体   繁体   English

可重用的自定义Symfony FormType

[英]Reusable Custom Symfony FormType

I have created a custom Symfony FormType which helps to parse date from a string. 我创建了一个自定义的Symfony FormType,它有助于解析字符串中的日期。 (Project is available on my Github https://github.com/ThBM/date-text-type ) (项目可以在我的Github上找到https://github.com/ThBM/date-text-type

In order to be able to use this class again in other Symfony projects, I would like to wrap it into a composer package. 为了能够在其他Symfony项目中再次使用此类,我想将其包装到composer软件包中。

I tried to do the same as a Standard library but I have the following error when I run my Symfony app : Too few arguments to function DateTextType::__constuct. 我尝试做与标准库相同的操作,但是运行Symfony应用程序时出现以下错误:函数DateTextType :: __ constuct的参数太少。

It seems like Dependency Injection is not working : DateStringParserTransform is not injected in the DateTextType construtor when I build my form. 似乎依赖注入不起作用:构建表单时,DateStringParserTransform没有注入到DateTextType构造函数中。 Do you have any lead on how to properly package a Custome Symfony Component? 您对如何正确包装Custome Symfony组件有什么帮助?

Thank you for your help. 谢谢您的帮助。

I solved my problem. 我解决了我的问题。

DataTransformer should not be injected in the custom FormType. DataTransformer不应注入自定义FormType中。 You could just use an instance of it in the buildForm function. 您可以在buildForm函数中使用它的一个实例。

<?php 
public function buildForm(FormBuilderInterface $builder, array $options)
{
    parent::buildForm($builder, $options);
    $builder->addModelTransformer(new DateStringParserTransform());
}

I also follow this link in order to structure my package as a Symfony bundle. 我也遵循此链接,以便将我的软件包构造为Symfony捆绑包。 https://symfony.com/doc/current/bundles . https://symfony.com/doc/current/bundles

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM