-
-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: cast value to a decimal #1520
Fix: cast value to a decimal #1520
Conversation
Hello @wandesnet, thank you for submitting this PR! Can you add any tests? |
In this specific case, shouldn't the user be checking for null in the custom column? public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('amount')
->add('amount_formatted', fn (Order $model) => is_null($model->amount) ? 0 : formatCurrency($model->amount));
} In my experience, when dealing with currency amounts, humans don't say "your bill is null dollars". Displaying "$0.00" is often the same as displaying " ". Regarding the PR, I am not sure if we should remove the Alternatively, could we just provide an empty string as the default value in this->fields[$fieldName] = $closure ?? fn ($model) => e(strval(data_get($model, $fieldName, ''))); |
In this specific case, the error occurs regardless of whether it is a custom column.
In my opinion, you should not change the values to string, that is, you should always keep the original value/type saved in the table.
This way, the error will persist because it continues to return an empty string if the amount field is null, why not keep the original value/type saved in the table? Is there a reason for this? |
I see the point, as it is a cast. For me, it is more about not removing the So perhaps something like: if is_callable($closure) {
$this->fields[$fieldName] = $closure;
return;
}
$data = fn ($model) => data_get($model, $fieldName);
if (is_string($data) {
$data = e($data);
}
$this->fields[$fieldName] = $data (writing code on the go, from my head and untested) |
Got it, we can keep |
👍 L.G.T.M Thank you very much @wandesnet |
⚡ PowerGrid - Pull Request
Welcome and thank you for your interest in contributing to our project!. You must use this template to submit a Pull Request or it will not be accepted.
Motivation
Description
This Pull Request adds...
Related Issue(s): #_____.
Documentation
This PR requires Documentation update?
This Pr fixes a bug, let's understand the problem. Let's say we have an Orders table with the amount field of decimal type in which NULL VALUE is allowed.
In the Order model, define the cast, e.g.:
Component PowerGrid
When I access the page, a cast error appears.
Reason for the error, this transforms all values into strings, when the amount field is null in the table, the code below transforms the value of the amount field into an empty string, ""
this ends up resulting in an error because it is trying to convert an empty string to decimal
There are three ways to solve the problem.