laravel / nova-issues

556 stars 34 forks source link

BelongsTo field with dependsOn retains old selected value when changing parent #5674

Closed emcarson closed 1 year ago

emcarson commented 1 year ago

Description:

I have two BelongsTo fields, Country and Salesperson. Salesperson dependsOn Country:

BelongsTo::make('Country', 'country', Country::class),

BelongsTo::make('Salesperson', 'salesperson', Admin::class)
    ->dependsOn(['country'], function (BelongsTo $field, NovaRequest $request, FormData $formData) {
        $field->relatableQueryUsing(function (NovaRequest $request, Builder $query) use ($formData) {
            $query
                ->whereHas('countries', function (Builder $query) use ($formData) {
                    $query->where('countries.id', $formData['country']);
                });
        });
    })
    ->nullable(),

Whenever I change the Country option, Salesperson should refresh with a list of records for the selected country. That works flawlessly.

Problem is, if I select a Country followed by a Salesperson and then I change the Country again, the Salesperson field appears blank but retains the last Salesperson ID and validation fails ("This Salesperson may not be associated with this resource.").

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

1) Select Create resource

2) Select a record from the first dropdown (Country), then select a record from the second dropdown (Salesperson).

image

3) Change the Country but leave Salesperson blank:

image

4) Submit the form

image

Validation fails ("This Salesperson may not be associated with this resource.") even though nothing is selected and the field itself is nullable.

crynobone commented 1 year ago

If you wish to reset the selected salesperson on country changes you can do it by adding $field->setValue(null);.

emcarson commented 1 year ago

Is this the expected behaviour? That the belongsTo field retains the old value even though the options changed and said value isn't selectable anymore?

We're having lot's of problems, not just with validation but also with setting the fields readonly due to the old value being selected (in a three dropdown scenario).

I asked by partner to test the scenario out with his own project (different models/tables, resources, different Laravel version) and he's getting the exact same result.

I tried reseting the value as instructed in every possible way (before, after and inside relatableQueryUsing()) but the result remains the same:

BelongsTo::make('Salesperson', 'salesperson', Admin::class)
    ->dependsOn(['country'], function (BelongsTo $field, NovaRequest $request, FormData $formData) {
        $field->setValue(null);
        $field->relatableQueryUsing(function (NovaRequest $request, Builder $query) use ($formData) {
            $query
                ->whereHas('countries', function (Builder $query) use ($formData) {
                    $query->where('countries.id', $formData['country']);
                });
        });
    })
    ->nullable(),

image

Thank you.

crynobone commented 1 year ago

Is this the expected behaviour? That the belongsTo field retains the old value even though the options changed and said value isn't selectable anymore?

Yes.