在zend框架2中具有依赖注入的嵌套Fieldset

时间:2014-08-23 13:18:26

标签: php zend-framework2 zend-form fieldset

我跟着zend Advanced use of forms来解决我的问题。

方案: 我有两个fieldset。 JudgesFieldset和JudgesCareerFieldset(一名法官有多个职业,所以我需要在法官领域使用集合)。 JudgesCareerFieldset具有doctrine 2对象管理器依赖性,用于创建select元素并在module.php中创建JudgesCareerFieldset的服务,如Advanced use of forms中所述。一切都很好,工作和成功创建形式。代码和示例如下所示。

class JudgesCareerFieldset extends Fieldset implements InputFilterProviderInterface {
private $entityManager;
public function __construct(ObjectManager $entityManager) {
    parent::__construct('judges-career');

    $this->entityManager = $entityManager;

    $this->setHydrator(new DoctrineHydrator($entityManager))
            ->setObject(new Judges());

        //fields of the entity
}

class JudgesFieldset extends Fieldset implements InputFilterProviderInterface {
private $entityManager;
public function __construct(ObjectManager $entityManager) {
    parent::__construct('judges');

    $this->entityManager = $entityManager;

    $this->setHydrator(new DoctrineHydrator($entityManager))
            ->setObject(new Judges());

    //Remaining Fields of the Judge entities
}

public function init() {
    $this->add(array(
         'type' => 'Zend\Form\Element\Collection',
         'name' => 'judgeCareer',
         'options' => array(
             'label' => 'Please Judge Career',
             'count' => 2,
             'should_create_template' => true,
             'allow_add' => true,
             'target_element' => array(
                 'type' => 'CaseLaw\Judges\Form\JudgesCareerFieldset'
             ),
         ),
     ));
}}

和表单代码

class JudgesFieldsetForm extends Form {

public function __construct(ObjectManager $entityManager) {
    parent::__construct('Judges');


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

    $judgesFieldset = new \Caselaw\Judges\Form\JudgesFieldset($entityManager);
    $judgesFieldset->setUseAsBaseFieldset(true);
    $this->add($judgesFieldset);


    $this->add(array(
        'name' => 'submit',
        'attributes' => array(
            'type'  => 'submit',
            'value' => 'Add Judge',
            'id'    => 'submit',
            'class' => 'btn btn-primary'
        ),
    ));
}}

问题: 在我尝试显示集合时的视图脚本中,它将显示此错误“在表单中找不到[judgeCareer]名称的元素”。我怎样才能获得judgeCareer系列?

错误: enter image description here

2 个答案:

答案 0 :(得分:1)

我通过更改JudgesFieldset类中的集合代码解决了我的问题。

$this->add(array(
     'type' => 'Zend\Form\Element\Collection',
     'name' => 'judgeCareer',
     'options' => array(
         'label' => 'Please Judge Career',
         'count' => 2,
         'should_create_template' => true,
         'allow_add' => true,
         'target_element' => array(
             'type' => 'CaseLaw\Judges\Form\JudgesCareerFieldset'
         ),
     ),
 ));

更改为

$this->add(array(
         'type' => 'Zend\Form\Element\Collection',
         'name' => 'judgeCareer',
         'options' => array(
             'label' => 'Please choose categories for this product',
             'count' => 2,
             'should_create_template' => true,
             'allow_add' => true,
             'target_element' => new \Caselaw\Judges\Form\JudgesCareerFieldset($this->entityManager)
         ),
     ));

答案 1 :(得分:0)

您正在尝试添加名为judgeCareer的集合,但您的JudgesCareerFieldset对象的名称为judges-career。更改名称,Zend\Form\Fieldset应找到您的字段集。