Zend \ Form:在Zend/Form/Fieldset.php中的非对象上调用成员函数insert() [英] Zend\Form: Call to a member function insert() on a non-object in Zend/Form/Fieldset.php

查看:63
本文介绍了Zend \ Form:在Zend/Form/Fieldset.php中的非对象上调用成员函数insert()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我习惯于处理ZF1表单,因此这个新表单系统令人困惑.不过,一旦我考虑了一下,我们就可以在视图脚本中使用表单元素进行格式化了.那些旧的装饰工很痛苦.无论如何,对我来说,使用表单而不处理绑定的数据库对象将是很好的选择.这可能吗?看起来除了简单形式之外,还需要使用InputFilterAwareInterface类的模型类,这太过复杂了.虽然一次只一步,但我什至无法显示该表格.

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.

感谢您的帮助.

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

Below are my controller, form, and view scripts:

表单类:

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

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>

推荐答案

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

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.

在处理Zend\Form\Form和/或Zend\Form\Fieldset时,必须确保始终调用parent构造函数.

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

这篇关于Zend \ Form:在Zend/Form/Fieldset.php中的非对象上调用成员函数insert()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆