Closed Kovah closed 1 year ago
Hello there! Thanks for opening your first issue on this repo!
Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.
Backpack communication channels:
backpack-for-laravel
tag;Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.
Thank you!
-- Justin Case The Backpack Robot
So, I have tried to understand why this happens and looked into the source. The resulting HTML for the field is built in the enum.blade.php with the important part being around line 33:
if($enumClassReflection) {
$options = array_map(function($item) use ($enumClassReflection) {
return $enumClassReflection->isBacked() ? [$item->getBackingValue() => $item->name] : $item->name;
},$enumClassReflection->getCases());
$options = is_multidimensional_array($options) ? array_merge(...$options) : array_combine($options, $options);
}
When I use my backed enum, the options array becomes a multidimensional array with the correct values. In the following line however, that multidimensional array is flattened into a simple array where the actual correct value (10,20,30 in my case) is discarded and the regular array keys (0,1,2) are used.
Of course I do not get the full picture here, but it looks like there is a mistake.
For reference: those are the option arrays for the two enum types before flattening.
SimplePriority::class
array:3 [
0 => "LOW"
1 => "MEDIUM"
2 => "HIGH"
]
Priority::class
array:3 [
0 => array:1 [
10 => "Low"
]
1 => array:1 [
20 => "Medium"
]
2 => array:1 [
30 => "High"
]
]
When I replace array_merge(...$options)
with array_replace(...$options)
, I get the correct resulting array with the enum values as the array keys:
array:3 [
10 => "Low"
20 => "Medium"
30 => "High"
]
Ouch! Thanks for the detailed report @Kovah . @pxpm could you take a look at this please? It should be faster for you to understand & fix 🙏
Already opened a pull request with a possible fix. Hopefully this fixes it.
Bug report
What I did
I want to add a backed enum with typed values to Crud. The model is already prepared with the corresponding casting for the field.
The enum:
The model with casting (irrelevant info was removed):
And finally, the field for the creation form in
setupCreateOperation()
:What I expected to happen
Crud saves the value of the enum to the database.
What happened
While saving a
0 is not a valid backing value for enum "App\Enums\Priority"
error popped up:It seems that Backpack already mistakes the real keys of the enum while creating the field. This is the HTML output for the priority field:
I would assume that something like this would be correct:
What I've already tried to fix it
I created a very simply enum like used in the Backpack documentation:
With this enum, I am able to save the entry to the database.
Is it a bug in the latest version of Backpack?
After I run
composer update backpack/crud
the bug is it still there. Just updated to 5.5.0.Backpack, Laravel, PHP, DB version
When I run
php artisan backpack:version
the output is: