# Custom Element Factory

**URL:** https://discourse.laminas.dev/t/custom-element-factory/498
**Category:** Components & MVC
**Tags:** zend-form
**Created:** [February 28, 2018, 8:56pm UTC](https://discourse.laminas.dev/t/custom-element-factory/498 "2018-02-28T20:56:28Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![kkg](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kkg/32/302_2.png) [@kkg](https://discourse.laminas.dev/u/kkg)
#### Post date: [February 28, 2018, 8:56pm UTC](https://discourse.laminas.dev/t/custom-element-factory/498/1 "2018-02-28T20:56:28Z")

</div>

I can`t access to CustomElementFactory  
What am I doing wrong?

```auto
### 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
{
}

```

---

<div class="post-metadata">

### Author: ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)
#### Post date: [February 28, 2018, 9:42pm UTC](https://discourse.laminas.dev/t/custom-element-factory/498/2 "2018-02-28T21:42:41Z")

</div>

> [@kkg](#):
>
> @Form\Type(“Application\Form\Element\CustomElement”)

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](https://docs.zendframework.com/zend-form/quick-start/#using-annotations)  
There you can find an example for the type annotation:

```
/**
 * @Annotation\Type("Zend\Form\Element\Email")
 * @Annotation\Options({"label":"Your email address:"})
 */
public $email;

```

---

<div class="post-metadata">

### Author: ![kkg](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kkg/32/302_2.png) [@kkg](https://discourse.laminas.dev/u/kkg)
#### Post date: [February 28, 2018, 9:52pm UTC](https://discourse.laminas.dev/t/custom-element-factory/498/3 "2018-02-28T21:52:57Z")

</div>

This does not change the question

`use Zend\Form\Annotation as Form;` (Fixed)

Thank you for your comment

---

<div class="post-metadata">

### Author: ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)
#### Post date: [February 28, 2018, 10:04pm UTC](https://discourse.laminas.dev/t/custom-element-factory/498/4 "2018-02-28T22:04:46Z")

</div>

The annotation is wrong, not the namespace!

---

<div class="post-metadata">

### Author: ![kkg](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kkg/32/302_2.png) [@kkg](https://discourse.laminas.dev/u/kkg)
#### Post date: [February 28, 2018, 10:08pm UTC](https://discourse.laminas.dev/t/custom-element-factory/498/5 "2018-02-28T22:08:47Z")

</div>

I showed only part of the code.  
There is no Fatal error.

---

<div class="post-metadata">

### Author: ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)
#### Post date: [February 28, 2018, 10:10pm UTC](https://discourse.laminas.dev/t/custom-element-factory/498/6 "2018-02-28T22:10:10Z")

</div>

Replace:

`@Form\Type("Application\Form\Element\CustomElement")`

with:

`@Annotation\Type("Application\Form\Element\CustomElement")`

---

<div class="post-metadata">

### Author: ![kkg](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kkg/32/302_2.png) [@kkg](https://discourse.laminas.dev/u/kkg)
#### Post date: [February 28, 2018, 10:13pm UTC](https://discourse.laminas.dev/t/custom-element-factory/498/7 "2018-02-28T22:13:01Z")

</div>

Nothing changed. Problem still exists  
CustomElement is also defined

---

<div class="post-metadata">

### Author: ![kkg](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kkg/32/302_2.png) [@kkg](https://discourse.laminas.dev/u/kkg)
#### Post date: [February 28, 2018, 10:20pm UTC](https://discourse.laminas.dev/t/custom-element-factory/498/8 "2018-02-28T22:20:28Z")

</div>

The problem is that the Form Factory does not determine the Element Factory

```auto
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

```auto
'form_elements' => [
    'factories' => [
        \Application\Form\Element\CustomElement::class => \Application\Form\Element\CustomElementFactory::class,
    ],
],

```

---

<div class="post-metadata">

### Author: ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)
#### Post date: [February 28, 2018, 10:39pm UTC](https://discourse.laminas.dev/t/custom-element-factory/498/9 "2018-02-28T22:39:24Z")

</div>

> [@kkg](#):
>
> The problem is that the Form Factory does not determine the Element Factory

Fetch the annotation-builder from the service-manager:

```php
$builder = $container->get(Zend\Form\Annotation\AnnotationBuilder::class);

```

…and all is included.

---

<div class="post-metadata">

### Author: ![kkg](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kkg/32/302_2.png) [@kkg](https://discourse.laminas.dev/u/kkg)
#### Post date: [March 1, 2018, 5:01pm UTC](https://discourse.laminas.dev/t/custom-element-factory/498/10 "2018-03-01T17:01:44Z")

</div>

> [@froschdesign](#):
>
> $builder = $container-\>get(Zend\Form\Annotation\AnnotationBuilder::class);

Yes, it works. Thanks a lot!

But, I’m using Doctrine Entity, so I need to use

```auto
$builder = $container->get(DoctrineORMModule\Form\Annotation\AnnotationBuilder::class);

```

This does not work

---

<div class="post-metadata">

### Author: ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)
#### Post date: [March 1, 2018, 5:35pm UTC](https://discourse.laminas.dev/t/custom-element-factory/498/11 "2018-03-01T17:35:18Z")

</div>

Sorry, this is not a ZF component. Please look at module for the correct name to retrieve the builder:

> **[doctrine/DoctrineORMModule](https://github.com/doctrine/DoctrineORMModule)**
>
> Doctrine ORM Module for Laminas. Contribute to doctrine/DoctrineORMModule development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![kkg](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/kkg/32/302_2.png) [@kkg](https://discourse.laminas.dev/u/kkg)
#### Post date: [March 2, 2018, 5:11pm UTC](https://discourse.laminas.dev/t/custom-element-factory/498/12 "2018-03-02T17:11:47Z")

</div>

[http://devblog.x2k.co.uk/using-custom-form-elements-with-the-annotationbuilder-in-zend-framework-2/](http://devblog.x2k.co.uk/using-custom-form-elements-with-the-annotationbuilder-in-zend-framework-2/)

---

<div class="post-metadata">

### Author: ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)
#### Post date: [March 2, 2018, 7:06pm UTC](https://discourse.laminas.dev/t/custom-element-factory/498/13 "2018-03-02T19:06:25Z")

</div>

@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.
