kkg
February 28, 2018, 8:56pm
1
I can`t access to CustomElementFactory
What am I doing wrong?
### Entity
namespace Application;
...
use Zend\Form\Annotation;
...
class SomeEntity
{
...
/**
* @Annotation\Type("Application\Form\Element\CustomElement")
*/
public $custom;
...
}
### module.config.php
...
'form_elements' => [
'factories' => [
\Application\Form\Element\CustomElement::class => function () {
throw new \Exception('It works? No!');
},
],
],
...
### IndexController
function indexAction()
{
...
$class = $this->params()->fromQuery('entity');
$b = new AnnotationBuilder($this->getObjectManager());
$form = $b->createForm($class);
...
}
### CustomElement
namespace Application\Form\Element;
use Zend\Form\Element;
class CustomElement extends Element
{
}
I do not use annotations, but I think there is no @Form\Type
!
Please look at the documentation for the correct annotations: Quick Start - zend-form - Zend Framework Docs
There you can find an example for the type annotation:
/**
* @Annotation\Type("Zend\Form\Element\Email")
* @Annotation\Options({"label":"Your email address:"})
*/
public $email;
kkg
February 28, 2018, 9:52pm
3
This does not change the question
use Zend\Form\Annotation as Form;
(Fixed)
Thank you for your comment
The annotation is wrong, not the namespace!
kkg
February 28, 2018, 10:08pm
5
I showed only part of the code.
There is no Fatal error.
Replace:
@Form\Type("Application\Form\Element\CustomElement")
with:
@Annotation\Type("Application\Form\Element\CustomElement")
kkg
February 28, 2018, 10:13pm
7
Nothing changed. Problem still exists
CustomElement is also defined
kkg
February 28, 2018, 10:20pm
8
The problem is that the Form Factory does not determine the Element Factory
var_dump($form->getFormFactory())
### ouput
...
["\Application\Form\Element\CustomElement"] => object(Zend\Form\ElementFactory)#471 (1) {
["creationOptions":"Zend\Form\ElementFactory":private] => NULL
}
...
But I expected call Exception(āIt works? No!ā)
Because I want to use the following code for my purposes
'form_elements' => [
'factories' => [
\Application\Form\Element\CustomElement::class => \Application\Form\Element\CustomElementFactory::class,
],
],
Fetch the annotation-builder from the service-manager:
$builder = $container->get(Zend\Form\Annotation\AnnotationBuilder::class);
ā¦and all is included.
1 Like
kkg
March 1, 2018, 5:01pm
10
Yes, it works. Thanks a lot!
But, Iām using Doctrine Entity, so I need to use
$builder = $container->get(DoctrineORMModule\Form\Annotation\AnnotationBuilder::class);
This does not work
Sorry, this is not a ZF component. Please look at module for the correct name to retrieve the builder:
@kkg
This article describes the usage without Doctrine, but within a ZF application it is much simple the pull the annotation-builder from the service-manager.