Jump to content

callback from inside a class (HTML_QuickForm)


hemoglobina

Recommended Posts

Hello fellow coders.
I'm using PEAR::HTML_QuickForm and it has a function called: addFormRule(). Here is a snip from the manual
[code]
Synopsis
void HTML_QuickForm::addFormRule (mixed $rule)

Parameter
mixed $rule: A valid callback
[/code]
Ok so far... here is an example of its usage:
[code]

// the callback function
function 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
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]
<?php
class 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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.