NYCPHP Meetup

NYPHP.org

[nycphp-talk] QuickForm / PHP (Zend Engine) Bug - callback function never called

Jayesh Sheth jayeshsh at ceruleansky.com
Wed Mar 16 21:41:49 EST 2005


Hello all,

I have come across commonly occuring QuickForm bug that might be due to 
either a QuickForm bug or a Zend Engine bug (unless I am doing something 
really stupid here.)

Okay, here's the gist of it:

If a QuickForm object is instantiated inside of a conditional check, the 
callback processing function will not be run, UNLESS the callback 
function is defined BEFORE the if "( $form->validate() )" check.

(The following examples are based on sample code from David Sklar's 
"Essential PHP Tools" book.)
For example, in the following case, the callback function will NOT be run:
---------------------------
<?php
/*
praise_book() function is defined AFTER  'if ($form->validate())'
The callback function will now NOT be run
This is a PHP or QF bug
*/

define ("IS_TRUE", true);

if (IS_TRUE)
{
// Load the HTML_QuickForm module
require 'HTML/QuickForm.php';

// Instantiate a new form
$form = new HTML_QuickForm('book');
// Add a text box
$form->addElement('text','title','Book Title:');
// Add a select box
$subjects = array('Math','Ice Fishing','Anatomy');
$form->addElement('select','subject','Subject(s): ',$subjects);
// Add a submit button
$form->addElement('submit','save','Save Book');

// Add a validation rule: title is required
$form->addRule('title','Please Enter a Book Title','required');

$praise_book_called = false;

// Call the processing function if the submitted form
// data is valid; otherwise, display the form
if ( $form->validate() )
{
    $f = $form->process('praise_book');
   
    if (! $f || ! $praise_book_called)
    {
        if ( ! $praise_book_called )
        {
        echo "<p> Error / PHP Bug: </p>";
        echo "<pre>";
        print_r($f);
        echo "</pre>";
        }
        else
        {
           echo "<p>Other error.</p>";
        }
    }
}
else
{
   
    $form->display();
}

// Define a function to process the form data
function praise_book($v)
{
    global $subjects, $praise_book_called;
    $praise_book_called = true;
    // Entity-encode any special characters in $v['title']
    $v['title'] = htmlentities($v['title']);
    print "<i>$v[title]</i> is a great book about ";
    print $subjects[$v['subject']] . '.';
   
    return true;
}

}
else
{
    exit("<p>Error: An unknown error has occured. Exiting.</p>");
}
?>
---------------------------
The following error will be outputted (via print_r() )

---------------------------
Error / PHP Bug:

HTML_QuickForm_Error Object
(
    [error_message_prefix] => QuickForm Error: 
    [mode] => 1
    [level] => 1024
    [code] => -7
    [message] => process callback does not exist
    [userinfo] => Callback function does not exist in QuickForm::process()
    [backtrace] => Array
        (
            [0] => Array
                (
                    [file] => c:\apachefriends\xampp\php\pear\HTML\QuickForm.php
                    [line] => 1818
                    [function] => PEAR_Error
                    [class] => HTML_QuickForm_Error
                    [type] => ->
                    [args] => Array
                        (
                            [0] => process callback does not exist
                            [1] => -7
                            [2] => 1
                            [3] => 1024
                            [4] => Callback function does not exist in QuickForm::process()
                        )

                )

            [SNIP ... more stuff in here. Please run the example to see the entire output.]
)

-------------------------
Now, in the following case, if praise_book is defined before 'if 
($form->validate())', it will work as expected. Also, if the conditional 
check "if (IS_TRUE)" is removed, it will work as expected.

What is going on here?
Is it something that I am missing, or is it a PHP or QuickForm bug?

Thanks in advance.

- Jay




More information about the talk mailing list