Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend setElementsBelongTo() effect on subform elements

I have a subform($fileUploadSubform) within a subform ($requestSubform). I called setElementsBelongTo("requestRow[$rowNumber]") on the parent subform ($requestSubform).

    $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

When the form is rendered, $fileUploadSubform fileDesc element' name and id are as follows

name="requestRow[1][requestRow][1][fileUpload][fileDesc]"
id="requestRow-1-fileUpload-fileDesc"

Why is the value I've set in the setElementsBelongTo() function is repeated twice?

Thank you in advance!

[Update 08/13/2015]

As a temporary workaround, I just called setElementsBelongTo() from the child subform ($fileUploadSubform) instead of the parent subform ($requestSubform)

[Update 08/17/2015]

I have tried the following code I got from http://zend-framework-community.634137.n4.nabble.com/Improved-array-support-for-Zend-Form-td667215.html as it says in that post that subform elementsTobelong to is working properly.

    $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());

But here's what I'm getting for the $subForm's and $subFubForm's elements.

<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]">

[Update 08/24/2015]

I finally figured out the problem.

It was this line

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

There were some elements not showing before that's why I added that line. And only now I understand that those elements that were not showing up are those without ViewHelper decorator set. So, when I set the ViewHelper as the decorator and remove the above fields and called setElementsBelongTo() of the subform without having to from the root of the hierarchy just from that subform, then it worked.

like image 760
artsylar Avatar asked Aug 12 '15 05:08

artsylar


1 Answers

I'm unfamiliar with the zend-framework but from the looks of it, the form hierarchy is implicit. By which I mean you don't have to declare the full "path" when using setElementsBelongTo(). Think of it like a folder structure, you would only name the subfolder within the current working directory.

So when you declare:

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

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

$form->addSubForm($subForm, 'sub');

This is interpreted as putting email into bar and bar into foo, aka:

name="foo[bar][email]"

The documentation says:

setElementsBelongTo (line 1367)
Set name of array elements belong to
access: public
Zend_Form setElementsBelongTo (string $array)
string $array

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

Also:

Zend_Form::setElementsBelongTo($array)
Using this method, you can specify the name of an array to which all elements of the form belong. You can determine the name using the getElementsBelongTo() accessor.

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

The wording might be a little unclear but it might support my theory. So when using $form->setElementsBelongTo('foo'), anything added to $form will implicitely become an element of foo and therefore foo must be left out of subsequent setElementsBelongTo() calls that deal with sub-elements.

like image 82
spenibus Avatar answered Oct 26 '22 23:10

spenibus