gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:16:05 27 4
gpt4 key购买 nike

我正在学习如何使用 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 表格,所以这个新的表格系统令人困惑。不过,一旦我想到这一点,我们可以在 View 脚本中使用表单元素进行格式化的方式会很好。那些老装饰师很痛苦。无论如何,对我来说,使用表单而不处理绑定(bind)的数据库对象会很好。这可能吗?除了简单的表单之外,还需要一个使用 InputFilterAwareInterface 类的模型类似乎过于复杂。但是一次一步,我什至无法显示表单。

我很感激任何帮助。

下面是我的 Controller 、表单和 View 脚本:

表格类:
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 类:
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>

最佳答案

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

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

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

关于forms - Zend\Form : Call to a member function insert() on a non-object in Zend/Form/Fieldset. php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16022555/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com