使用数据库填充表单集合中的select

时间:2015-04-12 20:06:57

标签: php zend-framework2

我根据manual中的表格收集制作了表单集。然后我创建了一个从数据库填充的选择,也基于manual中的示例。两者都由他们自己工作,但现在我想在表单集合中使用select,然后我得到以下错误:

Catchable fatal error: Argument 1 passed to
Application\Form\ClientSelectFieldset::__construct() must be an instance of 
Application\Model\ClientTable, none given, called in 
/home/path/vendor/zendframework/zendframework/library/Zend/Form/FormElementManager.php
on line 174 and defined in
/home/path/module/Application/src/Application/Form/ClientSelectFieldset.php on line 9

感觉init()运行不正确或者什么东西,我需要做些什么特别的事情让我的选择在表单集合中工作?

Module.php

public function getFormElementConfig() {
    return array(
        'factories' => array(
            'Application\Form\ClientSelectFieldset' => function($sm) {
                $serviceLocator = $sm->getServiceLocator();
                $clientTable = $serviceLocator->get('Application\Model\ClientTable');
                $fieldset = new ClientSelectFieldset($clientTable);
                return $fieldset;
            }
        )
    );
}

的src /应用程序/窗体/ ClientFieldset.php

<?php
namespace Application\Form;

use Application\Model\ClientTable;
use Zend\Form\Fieldset;

class ClientSelectFieldset extends Fieldset {

    public function __construct(ClientTable $clientTable) {
        parent::__construct('clientselectfieldset');

        $options = array();
        $options[] = array('value' => 0, 'label' => "Select client");
        $options[] = array('value' => -1, 'label' => "---------------", 'disabled' => 'disabled');
        foreach($clientTable->fetchAll() as $clientRow) {
            $options[] = array('value' => $clientRow->id, 'label' => $clientRow->name);
        }

        $this->add(array(
            'name' => 'id',
            'type' => 'Zend\Form\Element\Select',
            'options' => array(
                'label' => 'Client',
                'options' => $options,
            ),
        ));
    }

    public function getInputFilterSpecification() {
        return array(
            'id' => array(
                'required' => true,
                'filters'  => array(
                    array('name' => 'Int'),
                ),
            )
        );
    }
}

的src /应用程序/窗体/ InvoiceFieldset.php

<?php
namespace Application\Form;

use Application\Model\Invoice;
use Zend\Form\Fieldset;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;


class InvoiceFieldset extends Fieldset {
    ... more code ...

    public function init() {
        $this->add(array(
            'name' => 'client',
            'type' => 'Application\Form\ClientSelectFieldset'
        ));
    }
}

的src /应用程序/窗体/ InvoiceForm.php

<?php

namespace Application\Form;

use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;

class InvoiceForm extends Form {
    public function __construct() {
        parent::__construct('invoice-form');
        ... more code ...

        $this->add(array(
            'type' => 'Application\Form\InvoiceFieldset',
            'options' => array(
                'use_as_base_fieldset' => true
            )
        ));
        ... more code ...
    }
}

1 个答案:

答案 0 :(得分:0)

解决方案很简单,我不得不将Application\Form\InvoiceFieldsetInvoiceForm的声明从__construct()移到init()函数。

的src /应用程序/窗体/ InvoiceForm.php

<?php

namespace Application\Form;

use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;

class InvoiceForm extends Form {
    public function __construct() {
        parent::__construct('invoice-form');
        ... more code ...
    }

    public function init() {
        $this->add(array(
            'type' => 'Application\Form\InvoiceFieldset',
            'options' => array(
                'use_as_base_fieldset' => true
            )
        ));
    }
}