Skip to content

Exists validation not works properly with BelongsTo relationship and custom ownerKey #5014

@feeh27

Description

@feeh27
  • Laravel Version: 9.38.0
  • Nova Version: 4.17.1
  • PHP Version: 8.1.1
  • Database Driver & Version: MariaDB 10.5
  • Operating System and Version: Windows 11
  • Browser type and version: Google Chrome 106.0.5249.119
  • Reproduction Repository: https://github.com/###/###

Description:

When I try create a new register (Asset), the following message is returned: "The Entity field is required.".

Debugging I noticed that Laravel Nova sends the id for validation instead of the code field, which would be correct.

Detailed steps to reproduce the issue on a fresh Nova installation:

  • Create a new Asset using Laravel Nova interface
  • Configure models and resources as below:

I have a two models, entity and assets:
DER

In my assets Model I've entity method with BelongsTo relationship.

public function entity(): BelongsTo
{
    return $this->belongsTo(
        Entity::class, // Eloquent model
        Asset::ENTITY_CODE, // Foreing key
        Entity::CODE, // Owner key
    );
}

In Entity Resource (Laravel Nova):

public function fields(NovaRequest $request): array
{
    return [
        ID::make()->sortable(),
        Text::make('Code')->hideWhenUpdating()->sortable(),
        Text::make('Name')->sortable(),
        Text::make('Description')->sortable(),
        BelongsTo::make('Entity Code', null, Entity::class)
                ->dontReorderAssociatables()
                ->withoutTrashed()
                ->sortable()
                ->hideWhenUpdating()
                ->creationRules('exists:entities,code'),
    ];
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions