Issues

ZF-8576: name of single element is not correct

Issue Type: Bug Created: 2009-12-18T02:13:51.000+0000 Last Updated: 2010-04-16T12:35:56.000+0000 Status: Resolved Fix version(s): Reporter: Hans Müller (anno) Assignee: Christian Albrecht (alab) Tags: - Zend_Form

Related issues: Attachments: - ZF-8576.patch

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

Posted by Menno Luiten (mluiten) on 2009-12-18T04:33:26.000+0000

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?

Posted by Hans Müller (anno) on 2009-12-18T05:44:08.000+0000

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.

Posted by Menno Luiten (mluiten) on 2009-12-18T06:04:40.000+0000

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?

Posted by Hans Müller (anno) on 2009-12-18T06:32:45.000+0000

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.

<pre class="literal">

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");

}

Posted by Hans Müller (anno) on 2009-12-18T06:35:08.000+0000

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

<pre class="literal">
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");

}

Posted by Menno Luiten (mluiten) on 2009-12-18T07:03:11.000+0000

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):

<pre class="highlight">
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");

}

Posted by Menno Luiten (mluiten) on 2009-12-20T10:29:32.000+0000

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?

Posted by Christian Albrecht (alab) on 2010-03-16T04:39:56.000+0000

Menno, i think your solution is correct.

Posted by Christian Albrecht (alab) on 2010-04-02T04:24:58.000+0000

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:

<pre class="highlight">
...
$sub_form->setElementsBelongTo("sub_form_name_" . $i);
...

Please respond if this issue could be closed now.

Posted by Christian Albrecht (alab) on 2010-04-16T12:34:33.000+0000

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.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts