Hn develop
Hn develop

Reputation: 17

zendframework 2 form populating MultiCheckbox values from a database

i am using zendframework 2 and doctrine 2. i want to populate the values of my MultiCheckbox from values in my database . i got the technique from: https://github.com/doctrine/DoctrineModule/blob/master/docs/form-element.md

namespace Users\Form;
use Zend\Form\Form;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
class addForm extends form implements ObjectManagerAwareInterface
{
protected $objectManager;
public function setObjectManager(ObjectManager $objectManager)
{
    $this->objectManager = $objectManager;
}

public function getObjectManager()
{
    return $this->objectManager;
}
public function __construct($name = null)
{
    parent::__construct('add');
    $this->setAttribute('method', 'post');
    $this->setAttribute('enctype','multipart/formdata');
            $this->add(array(
    'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
    'name' => 'option',
    'options' => array(
    'label' => 'Options Véhicule',
     'object_manager' => $this->getObjectManager(),
                'target_class'   => 'Users\Entity\optionsvehicule',
                'property'       => 'property'
            ,   )));

the error message i received: No object manager was set.

Upvotes: 1

Views: 1354

Answers (2)

kuldeep.kamboj
kuldeep.kamboj

Reputation: 2606

I have tried and found similar error. After some search I found solution posted on https://github.com/doctrine/DoctrineModule/issues/175. Which works.

For implement you need to do some changes like that

In Module.php add method getFormElementConfig :

public function getFormElementConfig()
{
    return array(
        'invokables' => array(
            'addForm' => 'Users\Form\addForm',
        ),
        'initializers' => array(
            'ObjectManagerInitializer' => function ($element, $formElements) {
                if ($element instanceof ObjectManagerAwareInterface) {
                    $services      = $formElements->getServiceLocator();
                    $entityManager = $services->get('Doctrine\ORM\EntityManager');

                    $element->setObjectManager($entityManager);
                }
            },
        ),
    );
}

In Your Form Class addForm.php, replace constructor with init method :

namespace Users\Form;
use Zend\Form\Form;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
class addForm extends form implements ObjectManagerAwareInterface
{
    protected $objectManager;
    public function setObjectManager(ObjectManager $objectManager)
    {
        $this->objectManager = $objectManager;
    }

    public function getObjectManager()
    {
        return $this->objectManager;
    }

    //public function __construct($name = null)
    public function init()
    {
        $this->setAttribute('method', 'post');
        $this->setAttribute('enctype','multipart/formdata');
        $this->add(array(
            'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
            'name' => 'option',
            'options' => array(
                'label' => 'Options Véhicule',
                'object_manager' => $this->getObjectManager(),
                'target_class'   => 'Users\Entity\optionsvehicule',
                'property'       => 'property'
            ,   )));

In Your Controller Class, Call form obejct through Service Locator :

//$form = new addForm();
$forms = $this->getServiceLocator()->get('FormElementManager');
$form = $forms->get('addForm');

Upvotes: 1

AlexP
AlexP

Reputation: 9857

The $objectManager property is undefined.

This is because you call the $this->getObjectManager() method immediately within the __construct() and before you set the variable.

The form depends on the object manager; so you could just add it as a constructor argument which would ensure it is set before the class is used.

Also, the constructor should only really be used for setting up the object's initial properties and state, use init() for modifying form elements.

class addForm extends Form
{
    protected $objectManager;

    public function __construct(ObjectManager $objectManager)
    {
        parent::__construct('add-form');

        $this->objectManager = $objectManager;
    }

    // The form element manager will call `init()` 
    // on the form so we can add the elements in this method
    public function init() {
        //....
        $this->setAttribute('method', 'post');
        $this->setAttribute('enctype','multipart/formdata');

        // $this->add(....
        // more elements added here
    }

}

Last thing is to register a factory that actually does the injection

class Module {

    public function getFormElementConfig() {
        return array(
            'factories' => array(
                'ModuleName\Form\FooForm' => function($formElementManager) {
                    $serviceManager = $formElementManager->getServiceLocator();
                    $objectManager  = $serviceManager->get('ObjectManager'); 

                    $form = new Form\FooForm($objectManager); 

                    return $form;
                },
            ), 
        );
    }
}

Upvotes: 0

Related Questions