未找到表单 class zend 2.3

form class not found zend 2.3

我正在尝试使用 zendform 创建一个简单的表单,这里是表单的代码 class:

<?php
namespace admin\Form;

use Zend\Form\Form;

class Addstudent extends Form
{
    public function __construct($name = null)
    {
        // we want to ignore the name passed
        parent::__construct('addstudent');


        $this->add(array(
            'name' => 'fio',
            'type' => 'Text',
            'options' => array(
                'label' => 'Фио',
            ),
        ));
        $this->add(array(
            'name' => 'gender',
            'type' => 'Zend\Form\Element\Radio',
            'options' => array(
                'label' => 'Пол',
                'value_options' => array(
                    '0' => 'М',
                    '1' => 'Ж',
                ),

            ),
        ));
        $this->add(array(
            'name' => 'birthdate',
            'type' => 'Text',
            'options' => array(
                'label' => 'Дата рождения',
            ),
        ));

        $this->add(array(
            'name' => 'edge',
            'type' => 'Text',
            'options' => array(
                'label' => 'Возраст',
            ),
        ));

        $this->add(array(
            'name' => 'university',
            'type' => 'Text',
            'options' => array(
                'label' => 'Вуз',
            ),
        ));
        $this->add(array(
            'name' => 'group',
            'type' => 'Text',
            'options' => array(
                'label' => 'Группа',
            ),
        ));
        $this->add(array(
            'name' => 'department',
            'type' => 'Text',
            'options' => array(
                'label' => 'Факультет',
            ),
        ));
        $this->add(array(
            'name' => 'grate',
            'type' => 'Zend\Form\Element\Radio',
            'options' => array(
                'label' => 'Курс',
                'value_options' => array(
                    '0' => '1',
                    '1' => '2',
                    '2' => '3',
                    '3' => '4',
                    '4' => '5',
                    '5' => '6',

                ),

            ),
        ));
        $this->add(array(
            'name' => 'enterence',
            'type' => 'Text',
            'options' => array(
                'label' => 'год поступления',
            ),
        ));

        $this->add(array(
            'name' => 'financesource',
            'type' => 'Zend\Form\Element\Radio',
            'options' => array(
                'label' => 'Курс',
                'value_options' => array(
                    '0' => 'Бюджет',
                    '1' => 'Контракт',

                ),

            ),
        ));

        $this->add(array(
            'name' => 'studyform',
            'type' => 'Zend\Form\Element\Radio',
            'options' => array(
                'label' => 'Форма обучения',
                'value_options' => array(
                    '0' => 'Дневная',
                    '1' => 'Заочная',

                ),

            ),
        ));

        $this->add(array(
            'name' => 'homeaddress',
            'type' => 'Text',
            'options' => array(
                'label' => 'Домашний адрес',
            ),
        ));

        $this->add(array(
            'name' => 'actualaddress',
            'type' => 'Text',
            'options' => array(
                'label' => 'Фактический адрес',
            ),
        ));
        $this->add(array(
            'name' => 'phone',
            'type' => 'Text',
            'options' => array(
                'label' => 'Телефон',
            ),
        ));

        $this->add(array(
            'name' => 'workplace',
            'type' => 'Text',
            'options' => array(
                'label' => 'Место работы',
            ),
        ));
        $this->add(array(
            'name' => 'services',
            'type' => 'Zend\Form\Element\Textarea',
            'options' => array(
                'label' => 'услуги',
            ),
        ));

        $this->add(array(
            'name' => 'submit',
            'type' => 'Submit',
            'attributes' => array(
                'value' => 'сохранить',
                'id' => 'submitbutton',
            ),
        ));
    }
}

但后来我尝试向它显示控制器显示致命错误 class 未找到 Addstudent。这是我将其导入控制器的方式

use admin\Model\Admin;
use admin\form\Addstudent;

这是一个动作

public function addstudentAction()
{

    $form = new Addstudent();
    $form->get('submit')->setValue('Сохранить');

    $request = $this->getRequest();
    if ($request->isPost()) {
        $admin = new Admin();
        $form->setInputFilter($admin->getInputFilter());
        $form->setData($request->getPost());

        if ($form->isValid()) {
            $admin->exchangeArray($form->getData());
           // $this->getAlbumTable()->saveAlbum($album);

            // Redirect to list of albums
            return $this->redirect()->toRoute('admin');
        }
    }
    return array('form' => $form);


  //  return array();
}

表单文件位于module/admin/src/admin/form/addstudent.php

我遇到了同样的问题。自动加载器不工作。我的项目没有很多表格,所以我把它放到我的 Bootstrap:

protected function _initAutoload()
{
   require_once APPLICATION_PATH . '/forms/Contact.php';
   require_once APPLICATION_PATH . '/forms/Participant.php';
}

但几天前我解决了我的问题。我的 Zend 库不在项目的文件夹 "library" 中,但上面有几个 adresaries(我在设置和 index.php 中更改了路径)。自从我将它移动到项目文件夹 "library" 后,自动加载器似乎工作正常。

你和其他 forms/tables 有这个问题吗?因为我做到了。

src 文件夹下的所有内容都应使用首字母大写,因此 module/admin/src/admin/form/addstudent.php 应为 module/admin/src/Admin/Form/Addstudent.php,依此类推。

那么你应该改变你的命名空间声明来匹配它,即 namespace Admin\Form;use Admin\Form\Addstudent;

目前您的大小写不一致(例如,您声明 namespace admin\Form; 但控制器中有 use admin\form\Addstudent;),这可能是您问题的原因。