Search code examples
phpzend-framework2zend-formformcollection

How to pass options/params to formCollection fieldset in ZendFramework 2?


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.

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?

Here's my code:

Controller

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

Form

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

Fieldset

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

Thanks in advance for your help.


Solution

  • 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.

    For example:

    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');