在表单集合中使用填充 select 的数据库
Using a database populated select in a form collection
我在manual. Then I have made a select that is populated from a database and also based on the example from the 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()
不正确 运行 之类的,我需要做一些特别的事情才能让我的 select 在表单集合中工作吗?
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/Application/Form/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/Application/Form/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/Application/Form/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 ...
}
}
解决方案很简单,我必须将 class InvoiceForm
中 Application\Form\InvoiceFieldset
的声明从 __construct()
移动到 init()
函数。
src/Application/Form/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
)
));
}
}
我在manual. Then I have made a select that is populated from a database and also based on the example from the 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()
不正确 运行 之类的,我需要做一些特别的事情才能让我的 select 在表单集合中工作吗?
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/Application/Form/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/Application/Form/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/Application/Form/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 ...
}
}
解决方案很简单,我必须将 class InvoiceForm
中 Application\Form\InvoiceFieldset
的声明从 __construct()
移动到 init()
函数。
src/Application/Form/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
)
));
}
}