Zend \ Form:在Zend / Form / Fieldset.php中的非对象上调用成员函数insert()

时间:2013-04-15 18:45:32

标签: forms zend-framework2

我正在学习如何使用Zend Framework 2(2.1.4)表单并遇到此错误。

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

我不想使用表单自动连接到数据库,实际上我只想使用表单来帮助验证,并且将从一个值数组中拉出并填充它。如何关闭表单对象中的数据库连接?

我习惯于处理ZF1表单,所以这个新表单系统令人困惑。一旦我想到它,我们在视图脚本中使用表单元素进行格式化的方式就会很好。那些旧的装饰者很痛苦。无论如何,对我来说,使用表单而不处理绑定的数据库对象会很好。这可能吗?除了简单的表单之外,使用InputFilterAwareInterface类需要一个模型类似乎太复杂了。虽然一步一步,我甚至无法显示表格。

我感谢任何帮助。

以下是我的控制器,表单和视图脚本:

表格类:

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'
            ),
        ));
    }
}

控制器类:

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查看脚本:

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

1 个答案:

答案 0 :(得分:21)

由于表单未正确设置,因此会发生此类错误消息。正如您在上面的代码中看到的那样,__construct()函数不会调用父构造函数。因此,内部“引导”不会发生,并且会发生错误。

在处理Zend\Form\Form和/或Zend\Form\Fieldset时,您必须确保始终致电父母构造函数。

parent::__construct('client-form');