Closed emcarson closed 1 year ago
If you wish to reset the selected salesperson
on country
changes you can do it by adding $field->setValue(null);
.
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(),
Thank you.
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.
Description:
I have two BelongsTo fields, Country and Salesperson. Salesperson dependsOn Country:
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).
3) Change the Country but leave Salesperson blank:
4) Submit the form
Validation fails ("This Salesperson may not be associated with this resource.") even though nothing is selected and the field itself is nullable.