使用 $form->prepare() Zend Framework 3 时,第 773 行 \zendframework\zend-form\src\Form.php 中为 foreach() 提供的参数无效

Invalid argument supplied for foreach() in \zendframework\zend-form\src\Form.php on line 773 when using $form->prepare() Zend Framework 3

我是新手ZF3

我已经完成搜索表单并使用 zend framework 3 实现了自动完成建议,但我收到一条错误消息 warning : in Invalid argument supplied for foreach() in \zendframework\zend-form\src\Form.php on line 773

然后我删除了 $form->prepare(); 并且没有出现消息错误但是一旦单击并验证了按钮然后错误消息返回

我的addkelas.phtml

<?php
$form->setAttribute('action',$this->url('kelasbimbingan',['action'=>'addkelas']));
$form->prepare();
echo $this->form()->openTag($form);
?>
<p>Type students names:</p>
<div id="prefetch">
<?= $this->formElement($form->get('nama')); ?>
</div>
<br>
<div>
<?php
    echo $this->formSubmit($form->get('submit'))."<br>";
    echo $this->form()->closeTag();
?>
</div>
<?php
echo "<script language='javascript'> var country_list =".$data.";</script>";
$this->headScript()
    ->appendFile('/js/typeahead.bundle.js', 'text/javascript')
    ->appendFile('/js/bloodhound.js', 'text/javascript')
    ->appendFile('/js/autocompletejavascript.js', 'text/javascript');
?>

我的 addkelasAction()

public function addkelasAction()
    {
        $form = new CarimahasiswaForm();

        $data = \Zend\Json\Json::encode($this->getMahasiswaData());

        if ($this->getRequest()->isPost()) {
            //get data, fill in the form with POST data
            // Fill in the form with POST data
            $dataMahasiswa = $this->params()->fromPost();
            $form->setData($dataMahasiswa);
            //validate form
            // print_r($data);
            if($form->isValid()){
                $dataMahasiswa = $form->getData();
                print_r($dataMahasiswa);
            }
        }

        return new ViewModel(['form'=>$form,'data'=>$data]);
    }

和我的表格

<?php

namespace Skripsiku\Form;

use Zend\Form\Form;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilter;
use Zend\Form\Element;


class CarimahasiswaForm extends Form
{
    public function __construct()
    {
        parent::__construct('tambah-kelas');

        $this->getElements();

        $this->add([
            'name'=>'submit',
            'type'=>'submit',
            'attributes'=>[
                'value'=>'Cari Mahasiswa',
                'id'=>'SaveButton',
                'class'=>'btn btn-info btn-md',
            ],
        ]);
    }

    public function getElements()
    {
        $this->add([
            'name'=>'nama',
            'type'=>'text',
            'attributes'=>[
                'placeholder'=>'Ketik Nama Mahasiswa',
                'class'=>'typeahead',
            ],
            'options'=>[
                'label'=>'Cari Mahasiswa :',
            ]
        ]);

    }

    private function addInputFilter() 
    {
        // Create main input filter
        $inputFilter = new InputFilter();        
        $this->setInputFilter($inputFilter);

        $inputFilter->add([
                'name' => 'nama',
                'required' => true,
                'filters'  => [
                    ['name' => 'StringTrim'],                    
                    ['name' => 'StripTags'],                    
                ], 
                'validators' => [
                    [
                        'name'    => 'StringLength',
                        'options' => [
                            'encoding' => 'UTF-8',
                            'min'      => 5,
                            'max'      => 255,
                        ],
                    ],
                ],
            ]);     
    }   
}

?>

请建议.. 抱歉我的英语不好..谢谢

Zend Form 扩展了 declares a getElements function

的 Fieldset
public function getElements()
{
    return $this->elements;
}

The line which throws the exception/error 尝试遍历从以下检索到的值:

 $elements = $fieldset->getElements();

您可能认为在给定的上下文中 $fieldset != $this,但是 zend handles forms like fieldset:

$this->attachInputFilterDefaults($this->filter, $this);

解决方案:不要覆盖 getElements 函数,要么 重命名您当前的函数 要么添加那一块构造函数的代码.