hemoglobina Posted December 6, 2006 Share Posted December 6, 2006 Hello fellow coders.I'm using PEAR::HTML_QuickForm and it has a function called: addFormRule(). Here is a snip from the manual[code]Synopsisvoid HTML_QuickForm::addFormRule (mixed $rule)Parametermixed $rule: A valid callback[/code]Ok so far... here is an example of its usage:[code]// the callback functionfunction cmpPass($fields){ print_r($fields); return true;}$form->addFormRule('cmpPass');[/code]Very simple, right? The problem I'm facing is that I'm using HTML_QuickForm from inside my own class, which means that I cannot simply define this by doing "$form->addFormRule('cmpPass');" because 'cmpPass' is not in the same scope.I believe that what I had to do was something like this:[code]"$form->addFormRule('$this->cmpPass');"[/code]But this is also not working...Does anyone know how can I solve this?Thank you for your help,HEMOglobina Link to comment https://forums.phpfreaks.com/topic/29604-callback-from-inside-a-class-html_quickform/ Share on other sites More sharing options...
willfitch Posted December 6, 2006 Share Posted December 6, 2006 Actually, if the cmpPass function is just that, an function, then yes, it can be called. Link to comment https://forums.phpfreaks.com/topic/29604-callback-from-inside-a-class-html_quickform/#findComment-135856 Share on other sites More sharing options...
keeB Posted December 6, 2006 Share Posted December 6, 2006 On your classes constructor line, add something like this:[code]<?php$this->quickForm = new HTML_QuickForm();?>[/code]Then call it in your code as..[code]<?php$this->quickform->addFormRule("zomg!");?>[/code]Good luck- Keeb Link to comment https://forums.phpfreaks.com/topic/29604-callback-from-inside-a-class-html_quickform/#findComment-135913 Share on other sites More sharing options...
hemoglobina Posted December 6, 2006 Author Share Posted December 6, 2006 Finally managed to make it work.Thank you for your help guys, but the problem could not be solved by doing:[code]<?php$this->quickForm = new HTML_QuickForm();$this->quickform->addFormRule("zomg!");?>[/code]Some how this would still out of scope. Here is how I solved it... but I have no clue why it worked:[code]<?phpclass MyClass { function MyClass() { // ... code ... $this->form = new HTML_QuickForm('frmTest', 'post'); // ... more code ... $this->form->addFormRule( array( $this, 'cmpPass') ); } function cmpPass($fields) { // now the scope is preserved and I'm able to do things like this: $element = $this->form->getElement('myTextArea'); // ... more code ... }}?>[/code] Link to comment https://forums.phpfreaks.com/topic/29604-callback-from-inside-a-class-html_quickform/#findComment-136030 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.