ZF2处理fieldset的依赖项不起作用

时间:2015-02-22 09:24:09

标签: php forms zend-framework2

我在fiedset类中关注Handling dependencies文档以获取使用服务。

这是我的表格类。

namespace Occupancy\Form;

use Zend\Form\Form;
use Zend\Stdlib\Hydrator\ClassMethods;

class OccupancyForm extends Form
{
    public function __construct()
    {
        parent::__construct('occupancy');

        $this->setAttribute('method', 'post');
        $this->setHydrator(new ClassMethods());

        $this->add(array(
            'name' => 'code',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'required' => 'required',
                'class' => 'form-control',
                'placeholder' => 'Code',
            ),                
        ));  

        $this->add(array(
            'type' => 'Zend\Form\Element\Collection',
            'name' => 'combinations',
            'options' => array(
                'count' => 1,
                'should_create_template' => true,
                'allow_add' => true,
                'target_element' => array(
                    'type' => 'CombinationFieldset',
                ),
            ),
        ));

    }
}

这是我的字段集类

namespace Occupancy\Form;

use Occupancy\Model\Combination;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;

class CombinationFieldset extends Fieldset implements InputFilterProviderInterface
{       
    private $supplementService = null;

    private $supplements = null;

    private static $adultCount = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4');
    private static $childCount = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4');

    public function __construct($options)
    {
        parent::__construct('combinations');

        $this->setObject(new Combination());

        $this->supplementService = $options['SupplementService'];

        $this->add(array(
            'name' => 'adult',
            'type' => 'Zend\Form\Element\Select',
            'attributes' => array(
                'required' => 'required',
                'class' => 'form-control ',
            ),
            'options' => array(
                'value_options' => self::$adultCount,
            ),
        ));

        $this->add(array(
            'name' => 'child',
            'type' => 'Zend\Form\Element\Select',
            'attributes' => array(
                'required' => 'required',
                'class' => 'form-control ',
            ),
            'options' => array(
                'value_options' => self::$childCount,
            ),
        ));

        $this->add(array(
            'name' => 'supplements',
            'type' => 'Zend\Form\Element\Select',
            'attributes' => array(
                'class' => 'form-control ',
                'multiple' => 'multiple',
                'data-plugin-selectTwo' => 'test'
            ),
            'options' => array(
                'value_options' => $this->getSupplements(),
            ),
        ));
    }

    public function getInputFilterSpecification()
    {
        return array(
            'supplements' => array(
                'required' => false,
            ),
        );
    }

    private function getSupplements()
    {
        if (isset($this->supplements)) {
            return $this->supplements;
        }    

        $supplements = $this->supplementService->fetchAll();

        $options = array();

        foreach ($supplements as $supplement) {
            $options[$supplement->getId()] = $supplement->getName();
        }

        $this->supplements = $options;

        return $options;
    }
}

这是模型类

namespace Occupancy;

use Occupancy\Form\CombinationFieldset;
use Occupancy\Form\OccupancyForm;
use Occupancy\Model\OccupancyService;
use Zend\ModuleManager\Feature\FormElementProviderInterface;

class Module implements FormElementProviderInterface
{
    public function getConfig()
    {
        return include __DIR__.'/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__.'/src/'.__NAMESPACE__,
                ),
            ),
        );
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'OccupancyService' => function ($sm) {
                    $apiAdapter = $sm->get('ApiAdapter');

                    return new OccupancyService($apiAdapter);
                },
                'OccupancyForm' => function () {
                    return new OccupancyForm();
                },
            ),
        );
    }

    public function getFormElementConfig()
    {
        return array(
            'factories' => array(
                'CombinationFieldset' => function ($sm) {

                    $serviceLocator = $sm->getServiceLocator();

                    $options = array(
                        'PropertyService' => $serviceLocator->get('PropertyService'),
                        'SupplementService' => $serviceLocator->get('SupplementService'),
                    );

                    return new CombinationFieldset($options);
                }
            )
        );
    }
}

这就是我在控制器中获取表单对象的方法。

private function getOccupancyForm()
    {
        if (!$this->occupancyForm) {
            $sm = $this->getServiceLocator();
            $this->occupancyForm = $sm->get('OccupancyForm');

            $formManager = $this->getServiceLocator()->get('FormElementManager');
            $this->occupancyForm = $formManager->get('OccupancyForm');
        }

        return $this->occupancyForm;
    }

但它显示了这个错误。

有人能找到这个问题吗?

  

Zend \ Form \ FormElementManager :: get无法获取或创建   CombinationFieldset的实例

0 个答案:

没有答案