Home » HTML » HTML_QuickForm » Bug #4435
Custom callback rules not evaluated correctly when value = 0
Details
| Submitted | 2005-05-25 20:48 UTC |
|---|---|
| From | ptermaten at teris dot nl |
| Status | Bogus |
| Package | HTML_QuickForm |
| PHP Version | 4.3.10 |
| OS | Linux |
| Roadmaps | (Not assigned) |
Comments
[2005-05-25 20:48 UTC] ptermaten at teris dot nl
Description:
------------
A 0 (zero) value is not considered to be correct when using a callback function. The same rexexp in a regex-type rule does work correctly.
Quickform version: 3.2.4pl1
Reproduce code:
---------------
call back function:
function test($val)
{
if (!preg_match('/^[0-9]+$/', $val)) {
return false;
}
return $val;
}
============
$form->registerRule('test', 'callback', 'test', 'Validate');
$form->addRule('fielda', $this->getMessage(1), 'test');
$form->addRule('fieldb', $this->getMessage(1), 'regex', '/^[0-9]+$/');
Expected result:
----------------
Entering a 0 in Field a triggers the error message.
Entering a 0 in Field b is accepted.
[2005-05-28 21:29 UTC] ptermaten at teris dot nl
See the code below. Obviously, validate() evaluates the value 0 that the test method returns as a 'false'.
==============================
<?php
/**
* Bug4435 test
*/
require_once 'HTML/QuickForm.php';
class Validate
{
function test($val)
{
if (!preg_match('/^[0-9]+$/', $val)) {
return false;
}
return $val;
}
}
$form =& new HTML_QuickForm('custom');
$form->addElement('header', null, 'Bug4435 test');
$form->registerRule('test', 'callback', 'test', 'Validate');
$form->addElement('text', 'field_a', 'The custom rule:');
$form->addElement('text', 'field_b', 'The regexp rule:');
$form->addRule('field_a', 'Enter number from 0 to 9', 'test');
$form->addRule('field_b', 'Enter number from 0 to 9', 'regex', '/^[0-9]+$/');
$form->addElement('submit', null, 'Send');
$form->validate();
$form->display();
?>
[2005-06-18 21:48 UTC] ptermaten at teris dot nl
Yes, I know what is causing it. The question is: should Quickform test for whether it gets a boolean or other type, using a indentical (===) operator?