如何将表单管理器创建的表单中的选项传递给集合字段集?

时间:2014-12-02 11:08:26

标签: zend-framework2 zend-form

我以这种方式在控制器中创建/调用了表单:

class MyController extends AbstractActionController {
    function IndexAction() {
        $form = $this
                 ->getServiceLocator()
                 ->get('FormElementManager')
                 ->get('MyForm',['option1'=>123);
    }
}

接下来 - 在表单中我可以访问传递的选项:

class MyForm extends Form implements ServiceLocatorAwareInterface
{
    use ServiceLocatorAwareTrait;

    public function init()
    {
        $option1 = $this->getOption('option1');  // I have here "123" !!!


        $this->add(
            [
                'name'    => 'Child',
                'type'    => 'Zend\Form\Element\Collection',
                'options' => [
                    'label'                  => 'Child',
                    'count'                  => 1,
                    'should_create_template' => true,
                    'allow_add'              => true,
                    'template_placeholder'   => '__placeholder__',
                    'target_element'         => [
                        'type' => 'MyFieldset',
                    ],
                ],
            ]
        );
    }
}

但是在悲伤的场景中 - 不:

class MyFieldset extends Fieldset implements ServiceLocatorAwareInterface
{
    use ServiceLocatorAwareTrait;

    public function init()
    {
        $option1 = $this->getOption('option1');  // Empty :(
    }
}

那么 - 如何实现对" option1"的访问在fieldset?

0 个答案:

没有答案