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

[Bug] [PRO] When clearing the option in select2_json_from_api field, set the value to null #5731

Closed
susanu opened this issue Dec 13, 2024 · 2 comments
Labels

Comments

@susanu
Copy link
Contributor

susanu commented Dec 13, 2024

File: backpack/pro/resources/views/fields/select2_json_from_api.blade.php | Lines 177->178
From:

let options = [...element.selectedOptions].map(option => JSON.parse(option.value));
let value = JSON.stringify(multiple ? options : options[0]);

To:

let value = null;
if(element.selectedOptions.length > 0) {
	let options = [...element.selectedOptions].map(option => JSON.parse(option.value));
	value = JSON.stringify(multiple ? options : options[0]);
}
@susanu susanu added the triage label Dec 13, 2024
@susanu susanu changed the title [Bug] [PRO] When clearing the option in select2_json_from_api, set the value to null [Bug] [PRO] When clearing the option in select2_json_from_api field, set the value to null Dec 13, 2024
Copy link

Thank you for your question about the select2_json_from_api field in Backpack. Based on the knowledge sources provided, I don't have specific information about the exact code change you're proposing. However, I can provide some relevant context about clearing values in select2 fields in Backpack.

The issue of clearing values in select2 fields has been addressed in several Backpack updates. For example, in PR #3301, a fix was implemented to clear ajax select options when clearing the value. This PR aimed to solve the problem of previous fetched options still being available to select while the field fetches new options from the server.

Additionally, in PR #2426, a fix was implemented to allow select2_from_ajax to send an empty value when cleared. This PR addressed the issue of the field not being present in the request if cleared in newer versions of select2.

While these PRs don't directly address the select2_json_from_api field, they suggest that Backpack has been working on improving the behavior of select2 fields when clearing values.

Your proposed code change seems to aim at setting the value to null when no options are selected. This approach aligns with the general direction of the fixes implemented in Backpack, which is to properly handle cleared or null values in select2 fields.

However, without more specific information about the select2_json_from_api field in the PRO version, I can't confirm if this exact change is necessary or if it might have unintended consequences.

If you're experiencing a specific issue with the select2_json_from_api field not properly handling cleared values, I would recommend reporting it as a bug to the Backpack team, providing details about your specific use case and the version of Backpack PRO you're using.

ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️

Relevant sources:

Github Issue (closed): Clean dependant select contents on change. Now only sets value to null.
Github Issue (closed): Clear values from select2_from_ajax
fixing select2 from array
Github Issue (closed): [4.0][BUG] The select2_from_ajax field does not save a null value when the column is nullable and the old value is removed
[4.0][BUGFIX] select2_from_ajax send empty value for clear
Github Issue (closed): [Bug] Can not save NULL value for relation field
clear ajax select options when clearing the value
field dependant functionality fixes
Github Issue (closed): [Bug] Select2 filter UI - previous selected value still visible after clearing
Github Issue (closed): [Bug] select2_multiple field send value null
Github Issue (closed): select2 multiple add all / remove all features
Github Issue (closed): [3.6][Bug] select2_from_array with allows_multiple "null" saves incorrectly
Github Issue (closed): select2_json_from_api wrong attribute to store behaviour

@pxpm
Copy link
Contributor

pxpm commented Jan 3, 2025

Thanks @susanu

I've merged the fix in backpack/pro and should release a new version later today 🙏

Cheers

@pxpm pxpm closed this as completed Jan 3, 2025
@github-project-automation github-project-automation bot moved this to Done in This week Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

No branches or pull requests

2 participants