0

I want create a custom element and use the short name for add the element into Form, using the new ServiceManager tecnique for ZF2 V.2.1+

I am try to copy the same sample of the zend documentation step to step but it not works.

When I use the service writting the short name, it raises a exception because service not found:

    Zend\ServiceManager\Exception\ServiceNotFoundException
    File:
    Zend\ServiceManager\ServiceManager.php:456
    Message:
    Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Test

I think I have all classes identically, see follows

This is my custom element:

    namespace SecureDraw\Form\Element;
    use Zend\Form\Element\Text;
    class ProvaElement extends Text {
    protected $hola;
        public function hola(){
            return 'hola';
        }
    }

This is my Module.php I have my invokable service be able to use short name:

    class Module implements FormElementProviderInterface {
        //Rest of class
        public function getFormElementConfig() {
            return array(
                'invokables' => array(
                    'Test' => 'SecureDraw\Form\Element\ProvaElement'
                )               
            );
        } 
    }

In my form I use for add the element, the commented line works ok, but with short name not works:

    $this->add(array(
        'name' => 'prova',
        //'type' => 'SecureDraw\Form\Element\ProvaElement',
        'type' => 'Test',    //Fail
    ));

In my action:

    $formManager = $this->serviceLocator->get('FormElementManager');
    $form    = $formManager->get('SecureDraw\Form\UserForm');
    $prova = $form->get('prova');
    echo $prova->hola();
josepmra
  • 617
  • 9
  • 25
  • Have you specified the use SecureDraw\Form\Element\ or some thing like that , in the top , if not please specify the use of name space which may avoid this problem , the error clearly says that its not able to find the class which you are trying to instantiate – Aravind.HU Mar 31 '13 at 17:15

2 Answers2

1

The problem is that the elements created via FormElementManager have to be created into init method instead __Construct method how it can see in this page.

The zend documentation is badly explained

josepmra
  • 617
  • 9
  • 25
0

Workaround: In your own module, create the following two files:

  1. FormElementManagerConfig with the invokables short names of your custom form elements;
  2. Subclass Form\Factory and override getFormElementManager and pass the config to the FormElementManager constructor;

You then use your own Factory to create your Form, like this (you can pass a very rudimentary, e.g. empty array, or a more or less full-fledged $spec to $factory->createForm()):

$factory = new Factory();
$spec    = array();
$form    = $factory->createForm($spec);

FormElementManagerConfig.php:

class FormElementManagerConfig implements ConfigInterface
{
    protected $invokables = array(
        'anything'          => 'MyModule\Form\Element\Anything',
    );

    public function configureServiceManager(ServiceManager $serviceManager)
    {
        foreach ($this->invokables as $name => $service) {
            $serviceManager->setInvokableClass($name, $service);
        }
    }
}

MyFactory.php:

class Factory extends \Zend\Form\Factory
{
    public function getFormElementManager()
    {
        if ($this->formElementManager === null) {
            $this->setFormElementManager(new FormElementManager(new FormElementManagerConfig()));
        }
        return $this->formElementManager;
    }
} 
Oliver Konig
  • 1,035
  • 2
  • 14
  • 17
  • Is this still necessary? All the documentation make it sound like a `form_elements` factory/invokable should make custom elements available out-of-the-box, but I saw your contribution to ZF Contributors and it looks like nothing has changed. – claytond Feb 20 '15 at 21:14