[Bug]: NumberFilter doesn't validate a string value, and as a result never validates any number
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
Thanks for your other FR, please issue a PR against the "development" branch, and I'll get it merged in this weekend.
same issue. duplicate with https://github.com/rappasoft/laravel-livewire-tables/issues/2030 ?
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).
This is in 3.5.0 as far as I can tell, please let me know if issue persists!