GithubHelp home page GithubHelp logo

simplesquid / nova-enum-field Goto Github PK

View Code? Open in Web Editor NEW
50.0 4.0 21.0 289 KB

An enum field and filters for Laravel Nova.

License: MIT License

PHP 100.00%
laravel laravel-nova-field nova enum laravel-enum enum-field laravel-nova

nova-enum-field's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nova-enum-field's Issues

Nova Enum does not persist value on edit form

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

2024-02-08 19 41 02

Filter shows selected enum option even when all enum options are unchecked

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:

  • Check off 1 enum filter option
  • See "Selected Filter Counter" change to "1" and background color change from "white" to "green"
  • Uncheck the enum filter option
  • See "Selected Filter Counter" stay at "1" and background color stays "green"

Expected results:

  • See "Selected Filter Counter" show no counter and background color change back to "white"

Screenshot 2022-12-12 at 10 16 20 PM

Add Filter

Happy to PR this if you'd like. Would be good to save having to duplicate them manually :)

nullable() not respected by Enum::make

  • Laravel v8.61.0
  • Laravel Nova v3.29.0
  • simplesquid/nova-enum-field v2.3.1
  • bensampo/laravel-enum v3.4.2

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.

Is there any way to translate enumeration in field

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

image

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.

Default filter value

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.

Attribute Casting Assumption

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.

Field declaring field is required even with selected value

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!

Nullable field

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).

Convert to GitHub Actions

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.

Add screenshot

Hi,

You should add your package to novapackages.com and add a screenshot!

Keep up the good work and thanks for sharing :)

/Tim

Support non int values

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)

EnumBooleanFilter and EnumFilter not working properly

  • Laravel v8.61.0
  • Laravel Nova v3.29.0
  • simplesquid/nova-enum-field v2.3.1
  • bensampo/laravel-enum v3.4.2

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?

Flagged enum

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.