如何在ZendFramework 2中将选项/参数传递给formCollection字段集? [英] How to pass options/params to formCollection fieldset in ZendFramework 2?

查看:76
本文介绍了如何在ZendFramework 2中将选项/参数传递给formCollection字段集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Form,其中包含一个formCollection,其中包含一个select元素,我希望根据POST参数用这些元素(从SQL中)填充值.

I have a Form which contains a formCollection with a select element that I want to populate with values (from SQL) depending on a POST param.

将此参数从控制器传递到表单不是问题,但是现在我找不到在formCollection的target_element中设置/读取该参数的方法.关于如何使它起作用的任何想法吗?

Passing this param from controller to form wasn't a problem but now I can't find a way to set/read that param in target_element of formCollection. Any ideas on how make that work?

这是我的代码:

控制器

class MyController extends AbstractActionController{
    public function indexAction()
    {
        $form = $this->serviceLocator->get('FormElementManager')->get('Module\Form\myForm');
        $form->init([
            'param' => $this->params()->fromPost('param')
        ]);
    }
}

表格

class myForm extends Form
{
    private $sm;
    public function __construct($sm = null)
    {
        parent::__construct();
        $this->sm = $sm;
    }

    public function init($params=[])
    {
        $this->add([
            'name' => 'choices',
            'type' => 'Zend\Form\Element\Collection',
            'options' => [
                'label' => 'SelectLabel',
                'count' => 1,
                'should_create_template' => true,
                'allow_add' => true,
                'template_placeholder' => '__placeholder__',
                'target_element' => [
                    'type' => 'Module\Form\choicesFieldset',
                    'options' => [
                        'param' => isset($params['param']) ? $params['param'] : 0,
                    ]
                ],
            ],
        ]);
    }
}

字段集

class choicesFieldset extends Fieldset{
    private $sm;
    public function __construct($sm = null){
        parent::__construct();

        $this->sm = $sm;
    }

    public function init(){
        $param = $this->getOption('param');

        $availableChoices = /* SQL_QUERY_BASED_ON_PARAM; */

        $this->add([
            'name' => 'choice_1',
            'type' => 'Select',
            'options' => [
                'label' => 'First choice',
                'value_options' => $availableChoices,
            ]
        ]);
    }
}

预先感谢您的帮助.

推荐答案

所有您需要做的就是从服务管理器中获取Request实例.检查所需的参数,然后将其注入"到表单中.

All you would need to do is fetch the Request instance from the service manager; check the parameter you want and then 'inject' it into the form.

在表单工厂中这样做更有意义;而不是在控制器中重复自己.

It would make more sense to do so in the form factory; rather than repeat yourself within controllers.

例如:

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

            'MyModule\Form\MyForm' => function($formElementManager) {

                $serviceManager = $formElementManager->getServiceLocator();

                $request = $serviceManager->get('Request');

                // defaults to 0 if not set  
                $param   = $request->getPost('the_posted_variable_name', 0); 

                $options = array(
                    'my_custom_option_name' => $param,
                );

                // You should maintain the Zend\Form\Element::__construct() method signuture
                // as it allows for the 'options' to be passed in.
                // Alternatively you could use $form->setOption('param', $options)
                // and inject the options as a soft dependency
                $form = new Form\MyForm('my_form', $options, $serviceManager);

                // ... other form stuff here

                return $form;
            },

        ),
    );
}

现在,您可以在表单中使用以下选项:

Now you can use the option within the form using:

$param = $this->getOption('my_custom_option_name');

这篇关于如何在ZendFramework 2中将选项/参数传递给formCollection字段集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆