laravel-livewire-tables icon indicating copy to clipboard operation
laravel-livewire-tables copied to clipboard

[Bug]: NumberFilter doesn't validate a string value, and as a result never validates any number

Open khwadj opened this issue 1 year ago • 1 comments

What happened?

Using a NumberFilter prevents the value in the input from being treated since NumberFilter::validate always returns false.

public function validate(float|int|string|array $value): float|int|string|false
    {
        if (is_array($value)) {
            return false;
        } elseif (is_float($value)) {
            return (float) $value;
        } elseif (is_int($value)) {
            return (int) $value;
        }

        return false;
    }

This is a regression from v3.4.14 that still had the old definition as

public function validate(mixed $value): float|int|bool
    {
        return is_numeric($value) ? $value : false;
    }

I do understand and share the will to type everything.

However, in this case, a valid $value comes from a input type="number" (or input type="text", or nput without a type) and will always be a string.

workaround: use a TextFilter instead, which is not great.

How to reproduce the bug

No response

Package Version

3.4.22

PHP Version

None

Laravel Version

10.48

Alpine Version

No response

Theme

None

Notes

Proposed solution:

public function validate(float|int|string|array $value): float|int|string|false
    {
        if (is_float($value)) {
            return (float) $value;
        } elseif (is_int($value)) {
            return (int) $value;
        }

        return is_numeric($value) ? (float) $value : false;
    }

I'm removing the test against the array type, since is_numeric(array) is false. I'm leaving the first two tests against the float and int because you could add anything before reaching this point where you would cast the value.

Let me know if this is an acceptable solution

Error Message

No response

khwadj avatar Oct 16 '24 09:10 khwadj

Thanks for your other FR, please issue a PR against the "development" branch, and I'll get it merged in this weekend.

lrljoe avatar Oct 18 '24 02:10 lrljoe

same issue. duplicate with https://github.com/rappasoft/laravel-livewire-tables/issues/2030 ?

gantispam avatar Nov 05 '24 07:11 gantispam

The validation element should be fixed in the next release.

I'd appreciate it if you could give it a quick test against the "dev-development" branch (which is where the next release will come from).

lrljoe avatar Nov 06 '24 00:11 lrljoe

This is in 3.5.0 as far as I can tell, please let me know if issue persists!

lrljoe avatar Nov 06 '24 00:11 lrljoe