Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend\Form: Call to a member function insert() on a non-object in Zend/Form/Fieldset.php

I am learning how to use Zend Framework 2 (2.1.4) forms and running into this error.

Call to a member function insert() on a non-object in ... /Zend/Form/Fieldset.php on line 178

I don't want use the form to automatically connect to a database, in fact I only want to use the form to help validate and will pull from and populate it with an array of values. How do I turn off the database connectivity in the form objects?

I am used to dealing with the ZF1 forms so this new form system is confusing. Once I thought about it though, the way we can use the form elements in our view scripts for formatting is going to be nice. Those old decorators were a pain. Anyway, for me, it would be nice to use the forms without dealing with bound database objects. Is this possible? It just seems so overly complicated to need a model class using InputFilterAwareInterface classes in addition to a simple form. One step at a time though, I can't even get the form to display.

I appreciate any help.

Below are my controller, form, and view scripts:

Form class:

namespace FBWeb\Form;
use Zend\Form\Form;
use Zend\Form\Element;
class ClientForm extends Form
{
    public function __construct()
    {
        $this->setAttribute('method', 'post');

        $this->add(array(
            'name' => 'client',
            'type' => 'Zend\Form\Element\Text',
            'options' => array(
                'label' => 'Client Name',
            ),
            'attributes' => array(
                'type' => 'text',
            ),

        ));

        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type'  => 'submit',
                'value' => 'Add'
            ),
        ));
    }
}

Controller class:

namespace FBWeb\Controller;
use Zend\Debug\Debug;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use Zend\Http\Request;
use FBWeb\Form\ClientForm;

class ClientController extends AbstractActionController
{

    public function indexAction()
    {
        $clientform = new ClientForm();
        return array('form' => $clientform);
    }
}

index.phtml view script:

<div id="clientformtable">
<?php 
$form = $this->form;
$form->setAttribute('action','/app/client/add');
$form->prepare();
echo $this->form()->openTag($form);
$client = $form->get('client');
echo $this->formRow($client);
echo $this->form()->closeTag();
?>
</div>
like image 414
gregthegeek Avatar asked Apr 15 '13 18:04

gregthegeek


1 Answers

This, and similar error messages, happen due to the fact that the form isn't properly set up. As you can see within the code above the __construct() function doesn't call the parents constructor. Therefore the internal "bootstrapping" doesn't happen and the error occurs.

You have to make sure to always call the parents constructor when dealing with Zend\Form\Form and/or Zend\Form\Fieldset.

parent::__construct('client-form');
like image 99
Sam Avatar answered Nov 15 '22 07:11

Sam