simplesquid / nova-enum-field

An enum field and filters for Laravel Nova.
MIT License
52 stars 26 forks source link

Field declaring field is required even with selected value #31

Closed neildcuk closed 3 years ago

neildcuk commented 3 years ago

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!

neildcuk commented 3 years ago

Interestingly, I just managed to resolve the unrelated errors on a required BelongsTo relation field by adding that field to the resource so it can be filled in before saving.

The issue with the enum field has oddly vanished, and it will now submit and store the value correctly on creation of a new entity.

I believe this is resolved but leaving this here in case you want to look into why Nova would highlight your field as required when another, unrelated field referencing a constrained relationship is missing from the resource.

mdpoulter commented 3 years ago

Interesting! Glad you got it working though, @neilwhitespace 😄 I will try check it out this weekend and see if I can replicate the issue.

mdpoulter commented 3 years ago

I haven't been able to make headway with this, so I'm closing for now. If anyone else runs into this issue, I will look into it again.

neildcuk commented 3 years ago

Not a problem—as I've said, we worked around it by providing complete validation anyway, so definitely an edge case!