Sephen
Sephen

Reputation: 1111

ZF2 Zend\Form\FormElementManager::get was unable to fetch or create an instance for Zend\Form\Element\Datetime

When i run the following code in a form class on my test domain:

$dateformat = "Y-m-d H:i";
$this->add(array(
    'name' => 'startdatetime',
    'type' => 'Zend\Form\Element\Datetime',
    'attributes' => array(
        'id' => 'startdatetime',
        'data-format' => "YYYY-MM-DD HH:mm",
        'min' => date($dateformat, $nowdate),
        'max' => date($dateformat, $futuredate),
        'step' => '5', // minutes; default step interval is 1 mint
    ),
    'options' => array(
        'label' => _('Start date and time'),
        'help-block' => _('Enter start date and time (YYYY-MM-DD HH:MM)'),
    ),
));
$this->get('startdatetime')->setFormat($dateformat);

I get this error:

Zend\Form\FormElementManager::get was unable to fetch or create an instance for Zend\Form\Element\Datetime

When running this locally there are no errors and all works fine? Also the Text and Textarea form elements that precedes it, work fine.

I just can't wrap my head around it... Any ideas?

Thank in advance!

Upvotes: 0

Views: 1226

Answers (1)

AlexP
AlexP

Reputation: 9857

The date time element is registered with the form element manager as an invokable class called datetime. You can also use the class' fully qualified class name, which is of course case sensitive, Zend\Form\Element\DateTime.

Therefore, the only change you need to make is to uppercase the T in Datetime

'type' => 'Zend\Form\Element\DateTime',

Or use the actual service name, which as with all services, is case insensitive.

'type' => 'datetime', 

Upvotes: 2

Related Questions