Zend setElementsBelongTo()对子表单元素的影响

art*_*lar 10 php zend-framework zend-form

我在子表单($ requestSubform)中有一个子表单($ fileUploadSubform).我在父子窗体($ requestSubform)上调用了setElementsBelongTo("requestRow [$ rowNumber]").

    $requestSubform= new Zend_Form_Subform();
    $requestSubform->setElementsBelongTo("requestRow[$rowNumber]");

    // add elements to $requestSubform

    // now create the file upload subform
    $fileUploadSubform= new Zend_Form_SubForm();
    $fileUploadSubform->addElement('file', 'fileName')
            ->setLabel('File'); 

    $fileUploadSubform->addElement('text', 'fileDesc')
            ->setLabel('File Description'); 

    $requestSubform->addSubForm($fileUploadSubform, 'fileUpload');

    $this->view->field = $requestSubform->__toString();

    // pass it as json via ajax back to javascript
Run Code Online (Sandbox Code Playgroud)

呈现表单时,$ fileUploadSubform fileDesc元素的名称和ID如下所示

name="requestRow[1][requestRow][1][fileUpload][fileDesc]"
id="requestRow-1-fileUpload-fileDesc"
Run Code Online (Sandbox Code Playgroud)

为什么我在setElementsBelongTo()函数中设置的值重复两次?

先感谢您!

[2015年8月13日更新]

作为临时解决方法,我只是从子子表单($ fileUploadSubform)调用setElementsBelongTo()而不是父子表单($ requestSubform)

[2015年8月17日更新]

我已经尝试了以下来自http://zend-framework-community.634137.n4.nabble.com/Improved-array-support-for-Zend-Form-td667215.html的代码,因为它在该帖子中说该子表单元素Tobelong工作正常.

    $form = new Zend_Form();
    $form->setElementsBelongTo('foobar');

    $form->addElement('text', 'firstName')
    ->getElement('firstName')
    ->setLabel('First Name')
    ->setRequired(true);

    $form->addElement('text', 'lastName')
    ->getElement('lastName')
    ->setLabel('Last Name')
    ->setRequired(true);

    $subForm = new Zend_Form_SubForm();
    $subForm->setElementsBelongTo('foobar[baz]');
    $subForm->addElement('text', 'email')
    ->getElement('email')
    ->setLabel('Email Address');

    $subSubForm = new Zend_Form_SubForm();
    $subSubForm->setElementsBelongTo('foobar[baz][bat]');
    $subSubForm->addElement('checkbox', 'home')
    ->getElement('home')
    ->setLabel('Home address?');
    $subForm->addSubForm($subSubForm, 'subSub');

    $form->addSubForm($subForm, 'sub')
    ->addElement('submit', 'save', array('value' => 'submit'));
    print_r($form->__toString());
Run Code Online (Sandbox Code Playgroud)

但这就是我为$ subForm和$ subFubForm的元素所获得的.

<input id="foobar-foobar-baz-email" type="text" value="" name="foobar[foobar][foobar][baz][email]">

<input id="foobar-foobar-baz-foobar-baz-bat-home" type="checkbox" value="1" name="foobar[foobar][foobar][baz][foobar][foobar][baz][foobar][baz][bat][home]">
Run Code Online (Sandbox Code Playgroud)

[2015年8月24日更新]

我终于弄明白了这个问题.

就是这条线

$this->view->field = $additionalInfoSubform->__toString();
Run Code Online (Sandbox Code Playgroud)

之前有一些元素没有显示,这就是我添加该行的原因.直到现在我才明白那些没有出现的元素是那些没有ViewHelper装饰器集的元素.因此,当我将ViewHelper设置为装饰器并删除上述字段并调用子窗体的setElementsBelongTo()时,无需从该子窗体的层次结构的根开始,那么它就可以工作.

spe*_*bus 4

我不熟悉,但从它的外观来看,表单层次结构是隐式的。我的意思是您在使用时不必声明完整的“路径” setElementsBelongTo()。将其视为文件夹结构,您只需命名当前工作目录中的子文件夹。

所以当你声明:

$form = new Zend_Form();
$form->setElementsBelongTo('foo');

$subForm = new Zend_Form_SubForm();
$subForm->setElementsBelongTo('bar');
$subForm->addElement('text', 'email')

$form->addSubForm($subForm, 'sub');
Run Code Online (Sandbox Code Playgroud)

这被解释为 put emailintobarbarinto foo,又名:

name="foo[bar][email]"
Run Code Online (Sandbox Code Playgroud)

文档说:

setElementsBelongTo (第 1367 行)
设置属于访问的数组元素名称
: public
Zend_Form setElementsBelongTo (string $array)
string $array

来自http://framework.zend.com/apidoc/1.9/Zend_Form/Zend_Form.html#setElementsBelongTo

还:

Zend_Form::setElementsBelongTo($array)
使用此方法,您可以指定表单的所有元素所属的数组的名称。您可以使用 getElementsBelongTo() 访问器确定名称。

来自http://framework.zend.com/manual/1.12/en/zend.form.advanced.html

措辞可能有点不清楚,但它可能支持我的理论。因此,在使用 时$form->setElementsBelongTo('foo'),添加到 的任何内容都$form将隐式成为 的元素foo,因此必须在处理子元素的foo后续调用中将其排除。setElementsBelongTo()