Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select DependsOn: Set Value Not Emitting Change #6380

Closed
3owlcristian opened this issue May 10, 2024 · 20 comments
Closed

Select DependsOn: Set Value Not Emitting Change #6380

3owlcristian opened this issue May 10, 2024 · 20 comments
Assignees
Labels
bug Verified bug by the Nova team next
Milestone

Comments

@3owlcristian
Copy link

3owlcristian commented May 10, 2024

  • Laravel Version:10.48.10
  • Nova Version: 4.33.3
  • PHP Version: 8.1.28

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
Copy link
Member

crynobone commented May 10, 2024

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>

@crynobone crynobone closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2024
@3owlcristian
Copy link
Author

@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
Copy link
Member

Again, this is not a supported behaviour.

@3owlcristian
Copy link
Author

@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 crynobone added the needs more info More information is required label May 10, 2024
@crynobone
Copy link
Member

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)

@crynobone crynobone reopened this May 10, 2024
@3owlcristian
Copy link
Author

@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 crynobone added this to the 5.x milestone May 15, 2024
@crynobone crynobone added bug Verified bug by the Nova team and removed needs more info More information is required labels May 15, 2024
@crynobone
Copy link
Member

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

@3owlcristian
Copy link
Author

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

@crynobone crynobone self-assigned this May 28, 2024
@ecreeth
Copy link

ecreeth commented May 28, 2024

@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
Copy link
Member

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
Copy link

ecreeth commented May 28, 2024

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
Copy link

salvisb commented Jul 16, 2024

@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
Copy link

+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.

@crynobone crynobone added the next label Jul 23, 2024
@sylouuu
Copy link

sylouuu commented Aug 13, 2024

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

This comment has been minimized.

@crynobone
Copy link
Member

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

@salvisb
Copy link

salvisb commented Oct 4, 2024

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

@salvisb
Copy link

salvisb commented Dec 11, 2024

This issue keeps biting. Still patiently waiting for the fix.

@crynobone
Copy link
Member

Released with Laravel Nova 5.0.0

Feel free to open up a new issue if you're still experiencing this problem on the latest version.

Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Verified bug by the Nova team next
Projects
None yet
Development

No branches or pull requests

7 participants