3
votes

I'm trying to create one page with a Form with two fieldsets that should each populate a different table.

I can easily create One form as in the Album tutorial, and bind the data like this:

    $pageForm  = new PageForm();
    $pageForm->bind($page);

with my PageForm class as follows:

class PageForm extends Form
{

    public function __construct($name = null)
    {
        // we want to ignore the name passed
        parent::__construct('page');
        $this->setAttribute('method', 'post');    


        $this->add(array(
            'name' => 'id',
            'attributes' => array(
                'type'  => 'hidden',
            ),
        ));
    } /// and a bunch of other elements

but if I put these elements into fieldsets the bind no longer works, besides I would need to bind each fieldset to a separate table, and they need to save into the separate tables once the form is submited.

How would I go about this, I think I can do it using two forms but that is probably not the right way to go about it (If I understand the concept of fieldsets correctly)?

1

1 Answers

4
votes

you have to use setObject in each Fieldset and provide a hydrator to it. eg:

<?php
// file My/Form/Product.php

namespace My\Form;

use Zend\Form\Fieldset;
use My\Entity\Product as ProductEntity;
use Zend\Stdlib\Hydrator\ClassMethods();

class Product extends Fieldset
{
    public function __construct($name = 'product')
    {
        parent::__construct($name);
        $this->setObject(new ProductEntity())
             ->setHydrator(new ClassMethods());

        $this->add(array(
            'name' => 'name',
            'options' => array('label' => 'Product name'),
        ));

        // Brand fieldset
        $brand = new Brand();
        $this->add($brand);
    }
}



// File My/Form/Brand.php
namespace My\Form;

use Zend\Form\Fieldset;
use My\Entity\Brand as BrandEntity;
use Zend\Stdlib\Hydrator\ClassMethods();

class Brand extends Fieldset
{
    public function __construct($name = 'brand')
    {
        parent::__construct($name = 'brand');
        $this->setObject(new BrandEntity())
             ->setHydrator(new ClassMethods());

        $this->add(array(
            'name' => 'name',
            'options' => array('label' => 'Brand name'),
        ));
    }
}


// File My/Form/ProductForm.php
namespace My\Form;

use Zend\Form\Form;
use My\Entity\Product as ProductEntity;
use Zend\Stdlib\Hydrator\ClassMethods();

class ProductForm extends Form
{
    public function __construct($name = 'product')
    {
        parent::__construct($name);
        $this->setObject(new ProductEntity())
             ->setHydrator(new ClassMethods());

        // Product Fieldset
        // Here, we define Product fieldset as base fieldset
        $product = new Product();
        $product->setUseAsBaseFieldset(true);
        $this->add($product);
    }
}

// In Module.php
// ...
    public function getServiceConfig()
    {
        return array(
            'invokables' => array(
                'My\Form\Product' => 'My\Form\Product',
            ),
        );
    }
// ...

// In Controller
// You don't need to use $form->bind($productEntity), except if you're editing a product.
// The form already has an Object, do you remenber??? "$this->setObject(new ProductEntity())" on form???

// ...
    $form = $this->getServiceLocator()->get('My\Form\Product');
// ...