Comments (8)
Looks like a duplicate of #17.
from nova-enum-field.
Thanks Matthew. I took a look at #17 and #172. I'm not understanding the relevance of #172 to this issue.
The purpose of #172 seems to be to support instantiation of enums from null
, which is not what I'm talking about here.
We're (myself and the author of #17) just talking about supporting the option to pass no value for the enum field when submitting the form, so we can keep a nullable/optional enum attribute null
if the user didn't wish to provide a value.
from nova-enum-field.
What would you want to store in the database in that instance?
from nova-enum-field.
If the enum attribute's column is nullable in the table (the only scenario I'm aware of where we'd use nullable()
in the Nova resource) I'm not understanding why we have to pass a value to store at all.
It would be just like any other nullable field. They didn't provide a value, so we don't set it on store or update. It just stays null
from nova-enum-field.
The problem with that approach is that when it's then read from the database (as null
), it would throw an error (BenSampo/laravel-enum#172).
I think the better approach would be to create an explicit "null" or "none" value:
final class ReasonType extends Enum
{
const None = 0;
const ReasonOne = 1;
const ReasonTwo = 2;
}
from nova-enum-field.
I think the better approach would be to create an explicit "null" or "none" value:
I respectfully disagree. If this is the solution, then why not just get rid of null
in every RDMS and use this "0 is the new null
" pattern on every column and data type we previously had as nullable?
Of course not. Clearly null
is incredibly useful, and I'd certainly like to be able to leverage it here.
The problem with that approach is that when it's then read from the database (as null), it would throw an error (BenSampo/laravel-enum#172).
In Tinker at least, I'm not getting an exception when accessing a null
enum attribute or saving a model with a null
enum attribute - and that's without any safety checks!
Psy Shell v0.10.8 (PHP 8.0.9 — cli) by Justin Hileman
>>> $ps = App\Models\PassStatus::first();
=> App\Models\PassStatus {#4674
id: 1,
pass_id: 1,
status: 1,
reason: null,
setter_type: "App\Models\User",
setter_id: 1,
created_at: "2021-09-25 21:06:00",
updated_at: "2021-09-25 21:06:00",
}
>>> $ps->reason
=> null
>>> $ps->reason->value
<warning>PHP Warning: Attempt to read property "value" on null in Psy Shell code on line 1</warning>
=> null
>>> $ps->save()
=> true
So the worst I got a warning. If this were production code, I'd have implemented safety checks. in whatever view, controller, etc.
Now I go into TablePlus and change the reason
value for that record to 1
. Then continue in the same Tinker session:
>>> $ps->refresh()
=> App\Models\PassStatus {#4674
id: 1,
pass_id: 1,
status: 1,
reason: 1,
setter_type: "App\Models\User",
setter_id: 1,
note: null,
created_at: "2021-09-25 21:06:00",
updated_at: "2021-09-25 21:06:00",
}
>>> $ps->reason
=> App\Enums\PassStatus\ReasonType {#4741
+value: 1,
+key: "Chargeback",
+description: "Chargeback",
}
>>>
And to confirm, I do have the cast in the model:
use App\Enums\PassStatus\StatusType;
use App\Enums\PassStatus\ReasonType;
// ...
protected $casts = [
'status' => StatusType::class,
'reason' => ReasonType::class,
];
IMO, if any attribute's corresponding table column - regardless of type - is nullable()
in the migration, the responsibility lies on the developer to handle safety checks and validation elsewhere throughout the application.
And at least looking at the CLI example above, there's nothing preventing saving a null
value to an enum attribute, or accessing a null
enum-casted value.
In one of my current projects, we're using enums in probably a dozen models. Some are nullable, and we've had no issues. Then again, we have safety checks on any nullable model attribute, which I think is just to be expected as a best practice.
By the way, I do want to say I'm very grateful for this package, it's been incredibly useful for me lately, and saved time in multiple projects. I appreciate it!
from nova-enum-field.
That's interesting actually. It looks like support was added for casting null
values (BenSampo/laravel-enum#152) - I missed that!
Feel free to PR a nullable()
method with tests, otherwise I should have time to do it myself in the coming days.
from nova-enum-field.
I totally missed #152! It's been a busy week, but I could try to give it a shot if you're not able to carve out time in the next few days.
from nova-enum-field.
Related Issues (20)
- Laravel 7 support
- Nullable field HOT 3
- Convert to GitHub Actions
- Laravel 8 Support Please HOT 3
- Can we apply the enum resolution on the index? HOT 10
- Screenshot link results in 404
- Flagged enum HOT 1
- Field declaring field is required even with selected value HOT 4
- Default filter value HOT 1
- Enum column name "_backoffice" works fine, but "BACKOFFICE" shows empty value HOT 7
- Is there any way to translate enumeration in field HOT 1
- EnumBooleanFilter and EnumFilter not working properly HOT 6
- Support 4 version of bensampo/laravel-enum HOT 3
- Support 5 version of bensampo/laravel-enum HOT 2
- Nova V4 support HOT 8
- Filter shows selected enum option even when all enum options are unchecked HOT 4
- Nova Enum does not persist value on edit form HOT 1
- Not working with Laravel Nova 4.32.12 (Feb 14)
- Add Filter HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nova-enum-field.