【问题标题】:How to pass options/params to formCollection fieldset in ZendFramework 2?如何将选项/参数传递给 ZendFramework 2 中的 formCollection 字段集?
【发布时间】:2014-07-20 03:21:12
【问题描述】:

我有一个表单,其中包含一个带有选择元素的 formCollection,我想根据 POST 参数填充值(来自 SQL)。

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

这是我的代码:

控制器

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,
            ]
        ]);
    }
}

提前感谢您的帮助。

【问题讨论】:

    标签: php zend-framework2 zend-form formcollection


    【解决方案1】:

    您需要做的就是从服务管理器获取Request 实例;检查您想要的参数,然后将其“注入”到表单中。

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

    例如:

    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;
                },
    
            ),
        );
    }
    

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

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

    【讨论】:

    • 完美运行!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多