Closed 0chak closed 1 year ago
Remove formatStateUsing method
Without formatStateUsing, the number gets displayed wrong: I have 1000.22 in the database but I see 100.022 in the field instead of 1.000,22
so far the only workaround I found is to use a Cast on the model:
namespace App\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
class MoneyCast implements CastsAttributes
{
public function get(Model $model, string $key, mixed $value, array $attributes): mixed
{
return floatval($value);
}
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
{
if (str_contains($value, ',') && str_contains($value, '.')) {
return floatval(str_replace(',', '.', str_replace('.', '',$value)));
}
return floatval($value);
}
}
in model:
protected $casts = [
'security_deposit' => MoneyCast::class,
];
and remove ->numeric()
from the field
but this way it allows me to input negative values because it ignores ->minValue(0)
so I have to make also a custom validation rule
basically its a mess
What happened?
Hi,
I need to show my currency in euro-style format ( 1.000,00 ): If I type some number, it gets formatted right, validated right and saved right, but if just I load the edit page, don't touch the field and hit save, it gives me the error "Field x must be a number"
How to reproduce the bug
security_deposit is a
decimal(10,2)
column in the dbno casts are used
enter some value, save it, reload the page, the field should be populated (till here ok), don't edit the field, hit save again, wrong validation appears
Package Version
1.1.0
PHP Version
8.2.2
Laravel Version
10.24.0
Which operating systems does with happen with?
No response
Notes
No response