Php 使用数组表示法对两个字段使用相同名称的Zend表单

Php 使用数组表示法对两个字段使用相同名称的Zend表单,php,forms,zend-framework,Php,Forms,Zend Framework,使用Zend Framework 1.9。我有一张表格: ... $this->addElement('text', 'field', [ 'label' => 'Name (*)', 'belongsTo' => 'a' ]); $this->addElement('text', 'field', [ 'label' => 'Name (*)', 'belongsTo' =&g

使用Zend Framework 1.9。我有一张表格:

...
    $this->addElement('text', 'field', [
        'label' => 'Name (*)',
        'belongsTo' => 'a'
    ]);
    $this->addElement('text', 'field', [
        'label' => 'Name (*)',
        'belongsTo' => 'b'
    ]);

...
我使用数组表示法生成如下嵌套数组:

array (size=10)
  'a' => 
    array 
      'field' => string '' (length=0)
  'b' => 
    array 
      'field' => string '' (length=0)
$data=[
       "a"=>
            [
             "field"=>"MY CUSTOM TEXT"
            ],
       "b"=>
            [
             "field"=>"MY SECOND CUSTOM TEXT"
            ]
      ]
$form->populate($data)
此符号对我很有用,但当我使用如下数组结构填充表单时:

array (size=10)
  'a' => 
    array 
      'field' => string '' (length=0)
  'b' => 
    array 
      'field' => string '' (length=0)
$data=[
       "a"=>
            [
             "field"=>"MY CUSTOM TEXT"
            ],
       "b"=>
            [
             "field"=>"MY SECOND CUSTOM TEXT"
            ]
      ]
$form->populate($data)
表单未填充

我读过Zend_表单不适用于同名字段,但在我的例子中,我使用数组表示法。我需要使用相同的名称,因为我使用的是数据库中列的名称,所以在我的数据库中有两个表“a”,“b”,它们的列名为“field”


有解决办法吗

您是否尝试过使用子窗体?我有1.11,所以我不知道,但我已经成功地实现了你想要的代码

/**
 * Form class that should be in application/forms/Foo.php
 */
class Application_Form_Foo extends Zend_Form
{
    public function init()
    {
        $subFormA = new Zend_Form_SubForm();
        $subFormA->addElement($subFormA->createElement('text', 'field', array
        (
            'label' => 'Name (*)',
            'belongsTo' => 'a',
        )));

        $subFormB = new Zend_Form_SubForm();
        $subFormB->addElement($subFormB->createElement('text', 'field', array
        (
            'label' => 'Name (*)',
            'belongsTo' => 'b',
        )));

        $this->addSubForm($subFormA, 'a');
        $this->addSubForm($subFormB, 'b');


        $this->addElement($this->createElement('submit', 'send'));
    }
}
和控制器

/**
 * The controller that both process the request and display the form.
 */
class FooController extends Zend_Controller_Action
{
    public function indexAction()
    {
        // Get the form.
        $foo = new Application_Form_Foo();

        // Poppulate the form from the request.
        if ($foo->isValid($this->getRequest()->getParams()))
        {
            $foo->populate($foo->getValues());
        }

        // Set the form to the view.
        $this->view->form = $foo;
    }
}