simplesquid / nova-enum-field Goto Github PK
View Code? Open in Web Editor NEWAn enum field and filters for Laravel Nova.
License: MIT License
An enum field and filters for Laravel Nova.
License: MIT License
Given model
Models/Inventory.php
protected $casts = [
'type' => InventoryType::class,
];
Given enum
Enums/InventoryType.php
final class InventoryType extends Enum
{
const UNAVAILABLE = 0;
const RENTAL = 1;
const FOR_SALE = 2;
const AVAILABLE = 3;
const SOLD = 4;
}
Given Nova Resource
Nova/Inventory.php
Enum::make('Type')->attach(InventoryType::class),
In Nova, when setting value for type, it saves correctly, and on the index & detail view it displays correctly, but when on the edit form, it fails to pre-select the already chosen value, leading to upon saving, it saving a NULL
value, causing a validation error, unless ->nullable()
is added, but then overrides the previously selected value with NULL
Hi,
currently composer fails when using Laravel 7
Laravel v9.41.0
Nova v4.19.3
Nova Enum Field v3.0.1
Issue:
The Filter dropdown shows that an enum filter is selected even when filters have been unchecked.
Steps to reproduce:
Expected results:
Currently on the index listing page it just outputs the number, is it possible to show the resolved value there?
Happy to PR this if you'd like. Would be good to save having to duplicate them manually :)
Example:
Enum::make('Reason', 'reason')->attach(ReasonType::class)->sortable()->nullable(),
On forms, this field is still required, and the form won't submit without a value.
Hello!
I'm making a dashboard for French administrator, but for the relevance of the code, I obviously always use English.
The problem is that I don't see how to translate this, I haven't found any method to customize this on the field
My enum is declared like that :
<?php
namespace App\Enum;
use BenSampo\Enum\Enum;
final class StateType extends Enum
{
const InStock = "in_stock";
const OutStock = "out_stock";
const DataNotAvailable = "data_not_available";
}
I would like to keep enum in English. Only the state in the select should be translated if possible.
Hi!
Is there any way to set default value for EnumFilter
?
By default, you need to define default()
method inside your filter class, but I don't see any method to do it here.
Optional hack is to do something like:
(new EnumFilter('foo', BarEnum::class))->withMeta([
'currentValue' => BarEnum::Baz
]),
I can submit PR if desired.
Thank you for this package.
You're assuming that the Model uses Attribute Casting by default which is okay but please mention it in the documentation since you're relying on it. Otherwise, "Trying to get property 'value' of non-object" error would be thrown.
Thank you again.
Hi there โ bit of an odd one. We're having trouble changing the enum values in existing records, and submitting the form on a new record.
There's about 5 or 6 enums in the project but one Model only has one so that'll be my example.
Enum:
namespace App\Enums;
use BenSampo\Enum\Enum;
/**
* @method static static None()
* @method static static Employed()
* @method static static SelfEmployed()
*/
final class Employment extends Enum
{
const None = 0;
const Employed = 1;
const SelfEmployed = 2;
}
Relevant bits of the Model:
use App\Enums\Employment;
class Contact extends Model implements Auditable
{
//...
protected $casts = [
'employment' => Employment::class
];
// ...
protected $fillable = [
// ...
'employment',
];
}
Resource:
use SimpleSquid\Nova\Fields\Enum\Enum;
use App\Enums\Employment;
class Contact extends Resource
{
public function fields(Request $request)
{
Enum::make('Employment', 'employment')
->attach(Employment::class)
->hideFromIndex(),
}
}
The select appears on the resource correctly and it shows all of the possible enum values. I've checked it also has value
attributes which match the property values too.
It seems that whatever I put in the value of the select, when I hit submit, it states the field is required.
When I had other errors appear due to a missing relation on an unrelated field, I did notice that Nova was attempting to insert the chosen value into the DB as well, so not sure what might be happening here.
I've also checked your code here:
$this->fillUsing(
function (NovaRequest $request, $model, $attribute, $requestAttribute) {
if ($request->exists($requestAttribute)) {
$model->{$attribute} = $request[$requestAttribute];
}
}
);
And dumping $request[$requestAttribute]
comes back with the value as selected.
Any ideas? Any suggestions would be gratefully received!
With laravel 9 release, laravel enum was also updated to 5, https://github.com/BenSampo/laravel-enum/blob/master/UPGRADE.md
Friendly reminder that Laravel Nova V4 released today. Upgrade guide.
It would be great if I could make the Enum field nullable. Even if I add ->nullable()
to my field configuration, the input can't be null, because of a hard-coded ->rules('required', ...
in the Enum field class (line 25).
The screenshot that was requested in #1 does not exist anymore.
https://github.com/simplesquid/nova-enum-field/raw/master/docs/screenshot.png
The link must be now:
https://raw.githubusercontent.com/simplesquid/nova-enum-field/main/docs/screenshot.png
Happy for someone to work on this. It could either be just converting the Travis CI tests to use GitHub actions, or it could include the StyleCI checks too.
Hi,
You should add your package to novapackages.com and add a screenshot!
Keep up the good work and thanks for sharing :)
/Tim
Looking at the code it seems that the request value is cast to an int. Since the enum package support strings/etc, we shouldn't cast (and let eloquent handle it)
For me, both EnumBooleanFilter
and EnumFilter
are buggy.
Example from one of my resources:
namespace App\Nova;
// ...
use SimpleSquid\Nova\Fields\Enum\Enum;
use SimpleSquid\Nova\Fields\Enum\EnumFilter;
use App\Enums\PassStatus\StatusType;
use App\Enums\PassStatus\ReasonType;
// ...
public function filters(Request $request)
{
return [
new EnumFilter('status', StatusType::class),
new EnumFilter('reason', ReasonType::class),
];
}
The output is this. Both dropdowns contain the enum values for StatusType
.
If I switch the order, so ReasonType
comes first, then both of the dropdowns are "Reason".
And even though the filters are identical, filtering doesn't really work properly for either of them.
I haven't seen any closed issues for this, and people seem to be using the feature. Maybe this is a new behavior in recent Nova versions?
New version of bensampo/laravel-enum was released yesterday.
Changelog: https://github.com/BenSampo/laravel-enum/blob/master/CHANGELOG.md#400---2021-11-09
After update sth is wrong with validation, cannot save any field with enum type.
There is no support for flagged enum. Since flagged enum's main advantage is multiple choice value in one field it would be nice to have multiple choice select in nova.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.