Issues

ZF-8576: name of single element is not correct

Description

I have a subform with different elements in it and I tried t ogive one single element out but the name of it was noch correct.

I gave this in my ViewScript out:

$this->form->getSubForm("sub_form_foo")->getElement("foo");

So the name of the element was "foo" but normally it should be "sub_form_foo-foo". I think this is the correct name of it and not only "foo".

Comments

Is the problem that the output of the HTML render of the command above is faulty (that is, outputting an input with attribute name = "foo" instead of "sub_form_foo-foo")?

If you just use the above command and then call getName() on the element, you should indeed get 'foo' (the original name of the element), but calling getFullyQualifiedName() should return 'sub_form_foo-foo'. Could you check that, please?

Yes the first question of you describes the problem excactly.

I tested it with the method "getFullyQualifiedName()" and "getName()" but I only got "foo" as name back. No name like "sub_form_foo-foo" was given back.

Can you post a code snippet where you attach the element to the subform? As far as I know, that's where the $_belongsTo member is set, which should impack the fully qualified name. Also, do you rename the subform anywhere?

I expanded the creation of subforms a little bit. So that you can test more thank one. But it should have an effect on it if you only create one or more because the name of a subform is every time a key value of an array.


class Form_FooBar extends Zend_Form {

protected function CreateForm() {

for($i=0; $i < 2; $i++) {

   $sub_form = new Zend_Dojo_Form_SubForm();

$sub_form->addElement("TextBox",                                            "foo",                                          array("label" => "Foo",                                         "value" => "some_text"));

$this->addSubForm($sub_form, "sub_form_name_" . $i);

}

$this->addElement("SubmitButton",                                               "submit_search",                                                array("label" => "search"));
$this->getElement("submit_search")->removeDecorator("label");

}

I'm sorry this is a better view for it:

class Form_FooBar extends Zend_Form {

protected function CreateForm() {

for($i=0; $i < 2; $i++) {

   $sub_form = new Zend_Dojo_Form_SubForm();

$sub_form->addElement("TextBox",
                      "foo",
                      array("label" => "Foo",
                      "value" => "some_text"));

$this->addSubForm($sub_form, "sub_form_name_" . $i);

}

$this->addElement("SubmitButton",                                               
                  "submit_search",
                  array("label" => "search"));
$this->getElement("submit_search")->removeDecorator("label");

}

The $_belongsTo member is set on attachment of the element to the form, so in this case you create a subform without name, attach an element to it and only then give the subform a name. Maybe that is the problem.

Try something like this (not tested):


class Form_FooBar extends Zend_Form {

protected function CreateForm() {

for($i=0; $i < 2; $i++) {

   $sub_form = $this->addSubForm(new Zend_Dojo_Form_SubForm(), "sub_form_name_".$i);

   $sub_form->addElement("TextBox",
                      "foo",
                      array("label" => "Foo",
                      "value" => "some_text"));

}

$this->addElement("SubmitButton",                                               
                  "submit_search",
                  array("label" => "search"));
$this->getElement("submit_search")->removeDecorator("label");

}

Looking into this a bit further, I think I found some strange patterns regarding names and belongsTo in subforms..

SubForm -> addElement -> Form.addSubForm/setName = does not add name as value for belongsTo SubForm -> Form.addSubForm/setName -> addElement = adds name as value for belongsTo for element

All is moot when rendering the entire form using FormElements decorator, since that propagates the name of the form to the belongsTo of the elements, but when rendering a single element, the behavior could be considered inconsistent.

Either

let setName set belongsTo on all elements already added if isArray = true

remove _setBelongsTo from addElement

Depending on the kind of behavior that is wanted for rendering single elements.

Attached a patch to test this 'issue' and implementation of solution #1. Anyone care to comment?

Menno, i think your solution is correct.

On the other side, after delving into elementsBelongTo and isArray of Zend_Form, it feels wrong to use setName to set belongsTo of childElements, proposed solution for this problem:


...
$sub_form->setElementsBelongTo("sub_form_name_" . $i);
...

Please respond if this issue could be closed now.

Won't fix because this can be solved by rearranging the order of attaching SubForms and Elements and Setting ElementsBelongTo on the SubForm before attaching Elements to the SubForm.