I was reading up of Data Transformations I modified it somewhat in my code. I am getting
Could not load type "csv2tags"
I have a custom form type
class CSV2TagsType extends AbstractType {
public function buildForm(FormBuilder $builder, array $opts) {
$builder->appendClientTransformer(new CSV2TagsTransformer());
}
public function getParent() {
return 'text';
}
public function getName() {
return 'csv2tags';
}
}
Using the transformer:
class CSV2TagsTransformer implements DataTransformerInterface {
/**
* @var EntityManager
*/
protected $em;
public function __construct(EntityManager $em) {
$this->em = $em;
}
// ...
}
In services.yml
services:
jm.blog.csv2tagsTransformer:
class: JM\BlogBundle\Form\DataTransformer\CSV2TagsTransformer
arguments: ["@doctrine.orm.entity_manager"]
A few questions:
- I moved
EntityManager
to the transformer instead of the form type, is that OK? - I didn't declare an alias for the transformer service like in the docs. I thought the alias for the form type was from the
AbstractType::getName()
function? Do I need to declare my custom form type as a service too - I moved the
EntityManager
to the transformer. Did I do the right thing when I didnew CSV2TagsTransformer()
without theEntityManager
?
Regarding your main problem, in your
services.yml
file, you are supposed to tag your service asform.type
, like this:For the other problems, I have no idea.
I will just complete the answer given by @greg0ire which is almost right.
First, the service you defined should be your form type (
CSV2TagsType
) and not your transformer (CSV2TagsTransformer
) because it is the form type that is used by the form builder. Since the form builder expect anAbstractType
, it will not work with your service definition since the data transformer is not anAbstractType
. As @greg0ire said, you must tag your custom form type with theform.type
tag.The value returned by the function
getName
in your custom form type must match the alias you gave (cvs2tags
) or the service id you defined (jm.blog.csv2tagsType
). This is the way Symfony can find your custom type. It tries to find the service id given by thegetName()
method.For you data transformer, there is a litte problem. You are doing
new CSV2TagsTransformer()
but you are not passing the entity manager. This will result in an error. You cannot do it this way because there is no way to inject the data transformer in a service way.What you need to do is to inject the entity manager in you custom form type and then pass it to the data transformer when you instantiate it.
This way, you inject the entity manager in your form type, which is possible because the form type is defined as a service. And in the form type, you instantiate the data transformer and pass to the construct the entity manager that have been injected into the form type.
Another way to go would be to also declare you data transformer as a service and then inject it in the form type. This way, you do not instantiate a data transformer yourself but you use the one injected in the constructor of the form type. So, if you don't need the entity manager in the form type, you can omit to inject it. Here an example of this alternative:
Here a small resume of the answers to your question:
getName()
function must returned the id that should be used. It can be the alias defined, or the service id directly. Usually, people use the alias as the value returned bygetName
.new CSV2TagsTransformer()
because this way, you a not sending the entity manager to your class and since it can't accept null value, it will fail in PHP.Hope this helps.
Regards,
Matt