laravel / nova-issues

554 stars 34 forks source link

Select DependsOn: Set Value Not Emitting Change #6380

Open 3owlcristian opened 5 months ago

3owlcristian commented 5 months ago

Description: I'm using a feature called 'dependOn' to automatically fill in the city, county, and state fields based on the postal code entered. The state and county fields are select fields with predefined options.

The functionality works correctly for text fields, but I'm experiencing issues with the select fields. Initially, when the select field has no value and I change the postal code, the correct value is selected based on the new postal code. However, after this initial selection, the value in the select field does not change anymore, even though I can see in the request that the value is correctly set for the field.

This issue only occurs on the update page. On the create page, the functionality works without any issues. I am also attaching video of this issue. https://www.awesomescreenshot.com/video/27543474?key=e26c0283592351a50ea820d7a7f4b33b

$request_data = request()->all();
$postal_info = null;
if (@$request_data['zip']) {
    $zip = str_pad($request_data['zip'], 6, '0', STR_PAD_LEFT);
    $postal_info = PostalInfo::where('zip', $zip)->first();
}
return [
  Text::make(__('Zip'), 'zip'),
  Text::make(__('City'), 'city')->dependsOn(
      'zip',
      function (Select $field, NovaRequest $request, FormData $formData) use ($postal_info) {
          if (@$postal_info) {
              $field->setValue($postal_info->city);
          }
      }
  ),
  Select::make(__('State'), 'state')->options(config('states'))->searchable()->hideFromIndex()->hideFromDetail()->nullable()->displayUsingLabels()->dependsOn(
      ['zip'],
      function (Select $field, NovaRequest $request, FormData $formData) use ($postal_info) {
          if (@$postal_info) {
              $field->setValue($postal_info->state);
          }
      }
  ),

  Select::make(__('County'), 'county_id')->options($counties_arr)->hideFromDetail()->searchable()->dependsOn(
      'zip',
      function (Select $field, NovaRequest $request, FormData $formData) use ($postal_info) {
          if (@$formData['zip']) {
              if (@$postal_info) {
                  $field->setValue($postal_info->county_id);
              }

          }
      }
  ),
];
crynobone commented 5 months ago

Don't do the following.

$request_data = request()->all();
$postal_info = null;
if (@$request_data['zip']) {
    $zip = str_pad($request_data['zip'], 6, '0', STR_PAD_LEFT);
    $postal_info = PostalInfo::where('zip', $zip)->first();
}

Only depends on value available via FormData>

3owlcristian commented 5 months ago

@crynobone I got your point but. With that getting same issue on update page.

The issue is happening with select field only on the update page! On create page it works without any issue!

crynobone commented 5 months ago

Again, this is not a supported behaviour.

3owlcristian commented 5 months ago

@crynobone I converted the code that is mentioned in the doc. I am seeing as bug that when you set value on the select field on the edit page of resource it's not working properly.

return [
            Text::make(__('Email'), 'email'),
            PhoneNumber::make('Phone', 'phone')->format('###-###-####')->disableValidation()->nullable()
            ,
            PhoneNumber::make('Fax', 'fax')->format('###-###-####')->disableValidation()->nullable(),
            Text::make(__('Address Line 1'), 'address_1'),
            Text::make(__('Address Line 2'), 'address_2'),
            Text::make(__('Zip'), 'zip'),
            Text::make(__('City'), 'city')->dependsOn(
                'zip',
                function ($field, $request, $formData) {
                    if (@$formData['zip']) {
                        $zip = str_pad($formData['zip'], 6, '0', STR_PAD_LEFT);
                        $postal_info = PostalInfo::where('zip', $zip)->first();
                        if (@$postal_info) {
                            $field->setValue($postal_info->city);
                        }

                    }
                }
            ),
            Select::make(__('State'), 'state')->options(config('states'))->searchable()->hideFromIndex()->hideFromDetail()->nullable()->displayUsingLabels()->dependsOn(
                ['zip'],
                function (Select $field, NovaRequest $request, FormData $formData) {
                    if (@$formData['zip']) {
                        $zip = str_pad($formData['zip'], 6, '0', STR_PAD_LEFT);
                        $postal_info = PostalInfo::where('zip', $zip)->first();
                        if (@$postal_info) {
                            $field->setValue($postal_info->state);
                        }

                    }
                }
            ),

            Select::make(__('County'), 'county_id')->options($counties_arr)->searchable()->nullable()->hideFromDetail()->dependsOn(
                'zip',
                function (Select $field, NovaRequest $request, FormData $formData) {
                    if (@$formData['zip']) {
                        $zip = str_pad($formData['zip'], 6, '0', STR_PAD_LEFT);
                        $postal_info = PostalInfo::where('zip', $zip)->first();
                        if (@$postal_info) {
                            $field->setValue($postal_info->county_id);
                        }

                    }
                }
            ),
        ];
crynobone commented 5 months ago

Unable to reproduce the issue, please provide full reproducing repository based on fresh installation as suggested in the bug report template (or you can refer to https://github.com/nova-issues for example)

3owlcristian commented 5 months ago

@crynobone please use this repo: https://github.com/3owlcristian/nova-dependson-issue

I have added some seeders also for testing postal codes.

Issue happing on Contac Information resource update page!

crynobone commented 5 months ago

Confirmed this is a bug but going to reserve this for a major release to avoid breaking existing application.

3owlcristian commented 5 months ago

@crynobone thank you for your response! Hope v5 will released soon!

ecreeth commented 5 months ago

@crynobone Why is this considered a breaking change if that is the expected behavior of the field?

Confirmed this is a bug but going to reserve this for a major release to avoid breaking existing application.

crynobone commented 5 months ago

Changing behaviour on existing application is a breaking change. Would you be happy if a paid product out of nowhere cause your application to break to fix an expectation by someone else?

ecreeth commented 5 months ago

Changing behaviour on existing application is a breaking change. Would you be happy if a paid product out of nowhere cause your application to break to fix an expectation by someone else?

I understand breaking changes. Just imagine that I have a production application running without knowing that a certain method isn't working as expected. When I was implementing a part of my application, I noticed it immediately. What would happen in the scenario that I didn't? The customer would had noticed it.

I would be happy if the change is mentioned in the release notes.

salvisb commented 3 months ago

@crynobone what about other paying customers who expect a feature to just work as it should? I am also curious as to why fixing a feature that is not working would break someone's application.

When might this be fixed? Thanks!

vitaliyb commented 3 months ago

+1

We use ->setValue() method both in create and update form. On create form it works fine, but on update - not. As long as you fix it only for update forms - it should not be considered a breaking change. As for now it's simply not working.

sylouuu commented 2 months ago

Hello here,

It seems the value is still updated when submitting the form and the issue is only on the UI feedback.

Hope that helps!

Bests

ozanhazer commented 1 month ago

I'm not sure if it's the same issue, but I'm having a similar one with the help text in a custom dependent form field:

OrderContactField::make('Contact person')
    ->help('REQUIRED FOR SALES')
    ->dependsOn('tenant', function ($field, $request, $formData) {
        if ($formData->tenant) {
            $field->help('Some information');
        } else {
            $field->help('Other information');
        }
    });

It updates to "Some information" when a tenant is selected but the consequent updates are not reflected to the UI. The requests have the correct helpText in the responses.

I don't think it's caused by the custom field. FormField.vue:

<template>
  <DefaultField
    :field="field"
    :errors="errors"
    :show-help-text="showHelpText"
    :full-width-content="fullWidthContent"
  >
    <template #field>
    ...
    </template>
  </DefaultField>
</template>

<script>
import { DependentFormField, HandlesValidationErrors } from "laravel-nova"

export default {
  mixins: [DependentFormField, HandlesValidationErrors],
  ...
crynobone commented 1 month ago

@ozanhazer submit a proper bug report with reproducing code/repository.

salvisb commented 3 weeks ago

Hi @crynobone. Still looking forward to this fix. When could it be released? Thanks!