Aneeq Tariq
Aneeq Tariq

Reputation: 436

Nested Fieldset with dependency injection in zend framework 2

I followed zend Advanced use of forms to solve my problem.

Scenario: I have two fieldset. JudgesFieldset and JudgesCareerFieldset (one judge has multiple career so i need to use collection in judge fieldset). JudgesCareerFieldset has doctrine 2 object manager dependency for creating select element and create service of JudgesCareerFieldset in module.php as described in the Advanced use of forms. Everything is fine and working and create form successfully. The code and example shown below.

class JudgesCareerFieldset extends Fieldset implements InputFilterProviderInterface {
private $entityManager;
public function __construct(ObjectManager $entityManager) {
    parent::__construct('judges-career');

    $this->entityManager = $entityManager;

    $this->setHydrator(new DoctrineHydrator($entityManager))
            ->setObject(new Judges());

        //fields of the entity
}

and

class JudgesFieldset extends Fieldset implements InputFilterProviderInterface {
private $entityManager;
public function __construct(ObjectManager $entityManager) {
    parent::__construct('judges');

    $this->entityManager = $entityManager;

    $this->setHydrator(new DoctrineHydrator($entityManager))
            ->setObject(new Judges());

    //Remaining Fields of the Judge entities
}

public function init() {
    $this->add(array(
         'type' => 'Zend\Form\Element\Collection',
         'name' => 'judgeCareer',
         'options' => array(
             'label' => 'Please Judge Career',
             'count' => 2,
             'should_create_template' => true,
             'allow_add' => true,
             'target_element' => array(
                 'type' => 'CaseLaw\Judges\Form\JudgesCareerFieldset'
             ),
         ),
     ));
}}

and form code

class JudgesFieldsetForm extends Form {

public function __construct(ObjectManager $entityManager) {
    parent::__construct('Judges');


    $this->setAttribute('method', 'post')
         ->setHydrator(new DoctrineHydrator($entityManager));

    $judgesFieldset = new \Caselaw\Judges\Form\JudgesFieldset($entityManager);
    $judgesFieldset->setUseAsBaseFieldset(true);
    $this->add($judgesFieldset);


    $this->add(array(
        'name' => 'submit',
        'attributes' => array(
            'type'  => 'submit',
            'value' => 'Add Judge',
            'id'    => 'submit',
            'class' => 'btn btn-primary'
        ),
    ));
}}

Problem: In the view script when I tried to display collection it will display this error "No element by the name of [judgeCareer] found in form". How can i get judgeCareer collection ?

Error: enter image description here

Upvotes: 1

Views: 947

Answers (2)

Aneeq Tariq
Aneeq Tariq

Reputation: 436

I solved my problem by changing in collection code in JudgesFieldset class.

$this->add(array(
     'type' => 'Zend\Form\Element\Collection',
     'name' => 'judgeCareer',
     'options' => array(
         'label' => 'Please Judge Career',
         'count' => 2,
         'should_create_template' => true,
         'allow_add' => true,
         'target_element' => array(
             'type' => 'CaseLaw\Judges\Form\JudgesCareerFieldset'
         ),
     ),
 ));

Change to

$this->add(array(
         'type' => 'Zend\Form\Element\Collection',
         'name' => 'judgeCareer',
         'options' => array(
             'label' => 'Please choose categories for this product',
             'count' => 2,
             'should_create_template' => true,
             'allow_add' => true,
             'target_element' => new \Caselaw\Judges\Form\JudgesCareerFieldset($this->entityManager)
         ),
     ));

Upvotes: 1

p2sk
p2sk

Reputation: 1

You are trying to add a collection named judgeCareer, but your JudgesCareerFieldset object has the name judges-career. Change the name and the Zend\Form\Fieldset should find your fieldset.

Upvotes: 0

Related Questions