简体   繁体   中英

how to use helper function in form in zf2

I want to populate the select box in the zend form using helper funtion.

following are the helper and form code. myhelper.php

namespace myspace\View\Helper;

use Zend\View\Helper\AbstractHelper,
Zend\ServiceManager\ServiceLocatorInterface as ServiceLocator;
use Zend\Db\ResultSet\ResultSet;

class MyHelper extends AbstractHelper {

protected $serviceLocator;
protected $dbAdapter;
protected $resultData;

public function __construct(ServiceLocator $serviceLocator) {
    $this->serviceLocator = $serviceLocator;
}

public function getMyList() {
    return $states = array(
        'a' => 'a',
        'b' => 'b',
        'c' => 'c', );
}

public function getServiceLocator() {
    return $this->serviceLocator;
} 
}

My form code Myform.php

namespace myspace\Form;

use Zend\Form\Form;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Form\Element;
use masterinfo\View\Helper\MasterHelper;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class UserForm extends Form implements ServiceLocatorAwareInterface {

protected $dbAdapter;
protected $serviceLocator;
public function __construct($args) {
    parent::__construct('user');
    $dbAdapter = $args['dbAdapter']; 
    $this->setDbAdapter($dbAdapter);
    $this->setAttribute('method', 'post');
    $this->setAttribute('class', 'form-horizontal');
    $this->setAttribute('role', 'form');
    $this->setAttribute('enctype', 'multipart/form-data');

    $this->add(array(
        'name' => 'testselect',
        'type' => 'Zend\Form\Element\Select',
        'attributes' => array(
            'class' => 'single-select',
            'id' => 'testselect',
            'required' => 'required',
        ),
        'options' => array(
            'value_options' => /***here I need to call helper function getMyList()****/,
            'empty_option' => 'Select Status'
        ),
    ));
}

function setDbAdapter(AdapterInterface $dbAdapter) {
    $this->dbAdapter = $dbAdapter;
}

function getDbAdapter() {
    return $this->dbAdapter;
}

public function getServiceLocator() {
    return $this->serviceLocator;
}

public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
    $this->serviceLocator = $serviceLocator;
}

}

I am not sure how to call the helper function here. Please help I am relatively new to ZF2.

The code I pasted is sample actually getMyList function is suppose to populate lengthy array and I don't want to put that lengthy array in form as I will be reusing the array at few more places.

got it myself. I can pass the servicelocator from controller.

$form = new \myspace\Form\UserForm(array('dbAdapter' => $dbAdapter,'sm'=>$this->getServiceLocator()));

and then in form

....
....
$this->add(array(
    'name' => 'testselect',
    'type' => 'Zend\Form\Element\Select',
    'attributes' => array(
        'class' => 'single-select',
        'id' => 'testselect',
        'required' => 'required',
    ),
    'options' => array(
        'value_options' => $this->getMyArray($args['sm']),
        'empty_option' => 'Select Status'
    ),
));
....
....
function getMyArray($serviceLocator) {

    $master = new MyHelper($serviceLocator);
    return $master->getMyList();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM