laravel / nova-issues

556 stars 34 forks source link

BelongsTo field resets after changing value when depends on other field #5872

Closed kusab85 closed 1 year ago

kusab85 commented 1 year ago

Description:

Value of BelongsTo field resets to its initial value when dependsOn other field.

/**
 * App/Nova/Post.php 
 */

    public function fields(NovaRequest $request)
    {
        return [
            // [...]
            BelongsTo::make('User')
                ->dependsOn(
                    ['created_at'],
                    function (BelongsTo $field, NovaRequest $request, FormData $formData) {
                        $field->help(
                            '$formData = '.var_export($formData->getAttributes(), true)."</br>\n".
                            '$field->value = '.var_export($field->value, true)
                        );
                    }
                ),
           // [...]
        ];
    }
  1. Initial state of form (creating new post) obraz

  2. Selecting a user obraz

  3. Setting creation date obraz

  4. Form state after creation date setting obraz

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

Check reproduction repository.

crynobone commented 1 year ago

You shouldn't use $field->value as we need to determine whether it is undefined or null from dependsOn. You should be able to get the current value from $formData.

kusab85 commented 1 year ago

So, if I stop reading $field->value on server side the form field will stop resetting on client side? That's quite inobvious behavior ;-)

crynobone commented 1 year ago

https://github.com/laravel/nova-dusk-suite/blob/10.4/tests/Browser/DependentBelongsToFieldTest.php#L112-L155

This this in Nova repository hasn't failed yet, which should have if what you claimed did happened.

kusab85 commented 1 year ago

So @crynobone, please tell me if I'm misreading the test_it_reset_the_field_value test entirely.

The expected behavior of dependent BelongsTo field is to reset to its original value (null on creating) as soon as field on which it depends on changes its value?