【问题标题】:ZF2 Fieldsets and Form BindingZF2 字段集和表单绑定
【发布时间】:2013-03-28 12:35:05
【问题描述】:

我正在尝试使用带有两个字段集的表单创建一个页面,每个字段集应填充不同的表。

我可以像相册教程中一样轻松创建一个表单,并像这样绑定数据:

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

我的 PageForm 类如下:

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

但如果我将这些元素放入字段集中,则绑定不再有效,此外我需要将每个字段集绑定到单独的表,并且一旦提交表单,它们需要保存到单独的表中。

我该怎么做,我想我可以使用两种形式来做,但这可能不是正确的方法(如果我正确理解字段集的概念)?

【问题讨论】:

    标签: zend-framework2 zend-form


    【解决方案1】:

    您必须在每个 Fieldset 中使用 setObject 并为其提供水合器。例如:

    <?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');
    // ...
    

    【讨论】:

    • 谢谢!我会试试看。
    • 好的,我想我仍然缺少一些东西,当我确实需要编辑产品时,我应该把绑定放在哪里?由于我无法将任何内容绑定到字段集,我该如何告诉表单将​​特定对象绑定到它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多