PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » HTML » HTML_QuickForm » Bug #3865

Float/Integer values are not handled correctly when passed to HTML_QuickForm_Ru

Details

Submitted2005-03-17 22:04 UTC
Fromjbeall at heraldic dot us
Assignedavb
StatusClosed
PackageHTML_QuickForm
PHP Version5.0.3
OSLinux 2.4
Roadmaps(Not assigned)

Comments

[2005-03-17 22:04 UTC] jbeall at heraldic dot us

Description:
------------
If you apply a filter function that causes a field to have have value (integer)0, and the field is required, it will fail the validation check.

Reproduce code:
---------------
$form->addElement('text','test','Put a 0 here');
$form->addElement('submit','submit','submit');
$form->addRule('test','Required','required');
$form->applyFilter('test','abs');

$form->validate();

$form->display();

Expected result:
----------------
It should validate

Actual result:
--------------
Validation fails, it says "Required" even if you put 0.00 in the text box.

[2005-03-17 22:08 UTC] jbeall at heraldic dot us

The solution that has worked for me is to simply replace the line in HTML/QuickForm/Rule/Required.php that reads:

if ($value == '') {

with

if (strlen($value) < 1) {

This causes value to be cast to a string. Other possible solutions include:

if ("$value" == '') {
if ($value == '' && !is_numeric($value)) {
if ($value == ''&& !(is_int($value) || is_float($value)) {

[2005-03-17 22:10 UTC] jbeall at heraldic dot us

Of course the problem is that in the expression if($value == ''), the empty string '' is being cast to an int or float, and the cast results in 0 or 0.0, and if $value is the int 0 or float 0.0, respectively, the expression evaluates to bool(true).

[2005-03-17 22:32 UTC] bmansion at mamasam dot com

That's because you change the value type of the submitted value before it is being validated. So the code should be:

$form->addElement('text','test','Put a 0 here');
$form->addElement('submit','submit','submit');
$form->addRule('test','Required','required');

$form->validate();
$form->applyFilter('test','abs');

Anyway, would the following do :
if ((string)$value == '') {

Please test and report.
Thanks.

[2005-03-17 22:38 UTC] jbeall at heraldic dot us

Yes, I understand. Applying the filter after validation is the workaround I have been using, and it works fine.

I thought that it would be considered acceptable to change the type of the value prior to validation. I thought that was one of the reasons for filters, to put constraints on the submitted values? I didn't know it had to be after the form was validated.

At any rate, yes, (string)$value would work as well.

It is fine to apply the filters after validation, I just did not know that was the only "correct" way to do things.