Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.0.2

Bug #4440 Appended forms does not contain rules and errors
Submitted: 2005-05-26 11:04 UTC
From: php at pinna dot nl Assigned: justinpatrin
Status: Closed Package: DB_DataObject_FormBuilder
PHP Version: 5.0.3 OS: np
Roadmaps: (Not assigned)    
Subscription  


 [2005-05-26 11:04 UTC] php at pinna dot nl
Description: ------------ The problem is that when you append a form via useForm(), the validated rules and appeared errors are ignored... I found that out in a form with the same elementNames with another elementPrefix, so I first thought that was the problem... I digged deep into the original QuickForm.php but nothing found there... anyway, I found out that, (damn I should have started there) the problem was in the appending process. There only elements get appended but not rules, and since these rules are appended in the getForm process....these are ignored. I made a quick solution of which i think does the job Reproduce code: --------------- //APPEND EXISTING FORM ELEMENTS if (is_a($this->_form, 'html_quickform') && $this->_appendForm == true) { // There somehow needs to be a new method in QuickForm that allows to fetch // a list of all element names currently registered in a form. Otherwise, there // will be need for some really nasty workarounds once QuickForm adopts PHP5's // new encapsulation features. reset($this->_form->_elements); //////////////ADDED BY JAN@PINNA.NL /** * To maintain rules from appended forms. * */ $orgErrors = $this->_form->_errors; $orgRules = $this->_form->_rules; $curErrors = $form->_errors; $curRules = $form->_rules; $form->_errors = array_merge($orgErrors,$curErrors); $form->_rules = array_merge($orgRules,$curRules); ////////////ADDED BY JAN@PINNA.NL while (list($elNum, $element) = each($this->_form->_elements)) { $this->_addElementToForm($form, $element); } } Expected result: ---------------- replace code with: //APPEND EXISTING FORM ELEMENTS if (is_a($this->_form, 'html_quickform') && $this->_appendForm == true) { // There somehow needs to be a new method in QuickForm that allows to fetch // a list of all element names currently registered in a form. Otherwise, there // will be need for some really nasty workarounds once QuickForm adopts PHP5's // new encapsulation features. reset($this->_form->_elements); while (list($elNum, $element) = each($this->_form->_elements)) { $this->_addElementToForm($form, $element); } }

Comments

 [2005-05-26 16:31 UTC] justinpatrin
Yes, this is a known issue. Unfortunately, HTML_QuickForm has no public methods for getting any internals, including rules, errors, required fields, or elements. Setting internal variables is not only frowned upon, it's not correct Object Oriented programming. It also introduces possible breakage points if the package changes its internal structure. Now we *could* add some code like that, but I'm leary of it as it's easy to miss something or break something. I would suggest instead that we remove the appendForm option entirely. It's perfectly possible for you to alter the form after you call getForm as such: $do =& DB_DataObject::factory('table'); $fb =& DB_DataObject_FormBuilder::create($do); $form =& $fb->getForm(); $form->addElement('text', 'aField', 'A Field'); $form->addRule('aField', 'fill out A Field', 'required');
 [2005-05-26 19:54 UTC] php at pinna dot nl
Well, that doesnt really do the job for autorules... I made an enhancement on formbuilder for myself (just experimental) to get the constraints from my pgsql database and add them to the form. This works like a charm and makes it possible to very easily do a proof-of-concept of a datamodel... Now I ran into this problem because I wanted to create a listview of a form, with multiple rows. Therefore I already tackled the vertical orientation of the form. With appendForm I can manage that all fields are filled automatically and validation also... This makes it very versatile, at least for me in this experiment.. I hope you keep the appendForm.. Maybe a request for public methods for rules and errors can be made..? Maybe the autorules can be transferred easily because that is manage by FormBuilder... just a thought.
 [2005-05-26 20:12 UTC] justinpatrin
If you're talking about adding multiple FB forms together then you can, of course do this. Just don't use appendForm. $do =& DB_DataObject::factory('table'); $fb =& DB_DataObject_FormBuilder::create($do); $fb->elementNamePrefix = 'table__'; $form =& $fb->getForm(); $do2 =& DB_DataObject::factory('table2'); $fb2 =& DB_DataObject_FormBuilder::create($do2); $fb2->elementNamePrefix = 'table2__'; $fb2->useForm($form); $form2 =& $fb->getForm(); This way the forms for table and table2 are on the same form but table2 comes after table. I think this should work just fine for what you need. Instead of appending the form elements, it simply adds more elements to the current form. Of course you also need to use elementNamePrefix / Postfix to make the element names not collide if the tables have similar field names. As you can see, using useForm without using appendForm is basically equivalent and keeps the previous form, including rules, intact. This is why I feel like appendForm should simply be removed.
 [2005-05-27 07:09 UTC] php at pinna dot nl
Ai caramba.... That looks a lot better... I see now what you mean. Somehow, first without append=true it didn't work properly. Now it does... Sorry for this and thanks for the help. It's a bit confusing for me that appending=false does not mean that the form isnt merged with/added to the other form. Maybe remove append indeed.. :)
 [2005-05-27 16:25 UTC] justinpatrin
This bug has been fixed in CVS. In case this was a documentation problem, the fix will show up at the end of next Sunday (CET) on pear.php.net. In case this was a pear.php.net website problem, the change will show up on the website in short time. Thank you for the report, and for helping us make PEAR better. I've added your changes to CVS, including merging the _required array. Note: this is a hack and should be seen as such. Also, your diff is vs. an old version of FormBuilder. It's always best to get the CVS version and make your changes against that. Append means that the form supplied is to be appended to the FormBuilder created form. Without append the FormBuilder form will be appended to the supplied form. Yes, it is confusing. ;-)