Search code examples
formsinputzend-framework2

Just Created a form with ZF2 and its giving the error


I have got the following error when i have access my new created form:

Zend\Form\Factory::create expects the $spec["type"] to implement one of Zend\Form\ElementInterface, Zend\Form\FieldsetInterface, or Zend\Form\FormInterface; received Hidden

Here is the my AlbumForm.php file :

 namespace Album\Form;

 use Zend\Form\Form;

 class AlbumForm extends Form
     {
 public function __construct($name = null)
 {
     // we want to ignore the name passed
     parent::__construct('album');

     $this->add(array(
         'name' => 'id',
         'type' => 'Hidden',
     ));
     $this->add(array(
         'name' => 'title',
         'type' => 'Text',
         'options' => array(
             'label' => 'Title',
         ),
     ));
     $this->add(array(
         'name' => 'artist',
         'type' => 'Text',
         'options' => array(
             'label' => 'Artist',
         ),
     ));
     $this->add(array(
         'name' => 'submit',
         'type' => 'Submit',
         'attributes' => array(
             'value' => 'Go',
             'id' => 'submitbutton',
         ),
     ));
 }
}

And here is the model Album.php code for the form:

// Add content to these methods:
 public function setInputFilter(InputFilterInterface $inputFilter)
 {
     throw new \Exception("Not used");
 }

 public function getInputFilter()
 {
     if (!$this->inputFilter) {
         $inputFilter = new InputFilter();

         $inputFilter->add(array(
             'name'     => 'id',
             'required' => true,
             'filters'  => array(
                 array('name' => 'Int'),
             ),
         ));

         $inputFilter->add(array(
             'name'     => 'artist',
             'required' => true,
             'filters'  => array(
                 array('name' => 'StripTags'),
                 array('name' => 'StringTrim'),
             ),
             'validators' => array(
                 array(
                     'name'    => 'StringLength',
                     'options' => array(
                         'encoding' => 'UTF-8',
                         'min'      => 1,
                         'max'      => 100,
                     ),
                 ),
             ),
         ));

         $inputFilter->add(array(
             'name'     => 'title',
             'required' => true,
             'filters'  => array(
                 array('name' => 'StripTags'),
                 array('name' => 'StringTrim'),
             ),
             'validators' => array(
                 array(
                     'name'    => 'StringLength',
                     'options' => array(
                         'encoding' => 'UTF-8',
                         'min'      => 1,
                         'max'      => 100,
                     ),
                 ),
             ),
         ));

         $this->inputFilter = $inputFilter;
     }

     return $this->inputFilter;
 }

Here is the Stack Trace:

#0 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\Form\Form.php(143): Zend\Form\Factory->create(Array)
#1 C:\xampp\htdocs\zendtest\module\Album\src\Album\Form\AlbumForm.php(22): Zend\Form\Form->add(Array)
#2 C:\xampp\htdocs\zendtest\module\Album\src\Album\Controller\AlbumController.php(41): Album\Form\AlbumForm->__construct()
#3 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\Mvc\Controller\AbstractActionController.php(87): Album\Controller\AlbumController->addAction()
#4 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#5 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#6 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\EventManager\EventManager.php(208): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#7 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\Mvc\Controller\AbstractController.php(108): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#8 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\Mvc\DispatchListener.php(113): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#9 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#10 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#11 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\EventManager\EventManager.php(208): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#12 C:\xampp\htdocs\zendtest\vendor\ZF2\library\Zend\Mvc\Application.php(297): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#13 C:\xampp\htdocs\zendtest\public\index.php(14): Zend\Mvc\Application->run()
#14 {main}

How I can resolve the above mentioned error and made the module work fine. let me know if any further code is needed to be explore the issue.


Solution

  • We fixed issue in comments, but I post answer so if someone has similar problem it may be helpful.

    First of all. Check if your Zend library is up to date.
    If so, then try to check if problem is only with one certain input (comment/remove temporary) or it is global problem. Maybe you just did typo in type of input (Zend would probably throw exception that given type does not exist).

    If you get deprecated info:

    You are retrieving the service locator from within the class [...]

    Keep in mind that Zend in future version will remove getServiceLocator() method, so using service locator in controller is not a recommended. You should inject your dependencies through factory class.