Zf2 表单字段集 returns 无字段

Zf2 form fieldset returns no fields

我已经使用 ZF2 和 Doctrine2 完成了很多项目。我按如下方式构建表单:创建表单 class 扩展表单,然后创建字段集并将其设置为基本字段集,然后在字段集中添加我的字段。在 module.php 中,我在 formElementConfig 中为我的表单创建工厂。直到现在它一直以这种方式工作。我创建了一个新项目,突然遇到一个问题,我无法找到发生了什么。这是我的代码

 //module.php
 public function getFormElementConfig()
    {
        return array(
            'factories' => array(
                'OfferForm' => function($sm) {
                    $locator = $sm->getServiceLocator();
                    $form = new \Application\Form\OfferForm();
                    $form->setServiceLocator($locator);
                    return $form;
                },
            )
        );
    }

//Form
class OfferForm extends Form implements ServiceLocatorAwareInterface
{
    protected $serviceLocator;

    public function init()
    {
        $this->setAttributes(array(
            'id' => 'offer',
            'method' => 'post',
            'class'  => 'custom',
            'enctype' => 'multipart/form-data'
        ));

        $this->setAttribute('method', 'post')
            ->setHydrator(new ClassMethodsHydrator(false))
            ->setInputFilter(new InputFilter());

        $this->add(array(
            'name' => 'offer',
            'type' => 'Application\Form\Fieldset\OfferFieldset',
            'options' => array(
                'use_as_base_fieldset' => true
            )
        ));

        $this->add(array(
            'type' => 'Zend\Form\Element\Csrf',
            'name' => 'csrf'
        ));

        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'id' => 'submit',
                'type' => 'submit',
                'value' => $this->getServiceLocator()->getServiceLocator()->get('translator')->translate('Submit offer'),
                'class' => 'btn btn-info'
            )
        ));
    }
....

//Fieldset

class OfferFieldset extends Fieldset implements InputFilterProviderInterface, ServiceLocatorAwareInterface
{
    public function init()
    {
        $this->setHydrator(new ClassMethodsHydrator(false))
            ->setObject(new Offer());

        $this->add(array(
            'name' => 'title',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'required' => 'required',
                'class' => 'form-control',
            )
        ));
        ....other fileds
    }
    /**
     * @return array
     */
    public function getInputFilterSpecification()
    {
      ....
    }
}


//Controller

$em = $this->getObjectManager();
        $offer = new Offer();
        $form = $this->getServiceLocator()->get('FormElementManager')->get('OfferForm');
        $form->setHydrator(new DoctrineHydrator($em, 'Application\Entity\Offer'))->bind($offer);
        if ($this->request->isPost()) {
            $form->setData($this->request->getPost());
            if ($form->isValid()) {
                var_dump('ok');
            }
        }
        $form->prepare();
        return new ViewModel(array(
            'form' => $form,
        ));

这种做事方式一直对我有用,直到现在。如果我尝试使用 $this->form->get('offer')->get('title') 在 Veiw 中获取表单元素,它说没有名称为 'title'[ 的字段=12=]

我注意到的一件事是在控制器中调用表单时 ($form = $this->getServiceLocator()->get('FormElementManager')->get('OfferForm'); ) 未调用设置我所有字段的字段集方法 init()。 我试图将数据转储到那里并 die() 应用程序,但它根本没有进入该方法。

我可以提供更多代码,但我认为这就是构建表单的全部内容

您还需要将字段集添加到 formelementmanager 配置中。管理器的初始化程序将调用您的字段集 init() 方法。