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

select2_json_from_api behaviour when form is reloaded #5557

Closed
miquelangeld opened this issue Jul 10, 2024 · 5 comments
Closed

select2_json_from_api behaviour when form is reloaded #5557

miquelangeld opened this issue Jul 10, 2024 · 5 comments
Assignees
Labels
Possible Bug A bug that was reported but not confirmed yet.

Comments

@miquelangeld
Copy link

miquelangeld commented Jul 10, 2024

Hi, I'm using select2_json_from_api, here is my code

$this->crud->addField([
            'label' => 'Booking', // Table column heading
            'type' => 'select2_json_from_api',
            'name' => 'booking', 
            'entity' => false, 
            'attribute' => 'id', 
            'attributes_to_store'       => ['id', 'booking'],
            'model' => "App\Models\PayByLink",
            'data_source' => url('/admin/bookings/ajax-bookings-avalon-options'),
            'placeholder' =>'Select booking',
        ]);

In the same page I have another select2_json_from_api field but with include_all_form_fields = true

$this->crud->addField([
            'label' => 'Pax Name', // Table column heading
            'type' => 'select2_json_from_api',
            'minimum_input_length'    => 0,
            'name' => 'name', // the column that contains the ID of that connected entity
            'attribute'   => "name", // foreign key attribute that is shown to user
            'include_all_form_fields' => true,
            'data_source' => url('/admin/bookings/ajax-bookings-avalon-pax-options'),
        ]);
``
When I click on the second field ('name') this is what I get in my controller from the first field: 
	{"id":"MURR230044149-1","booking":"MURR230044149-1 Localizador: MUR0002615"}


But i f for some reason I reload (for example if  I forgot to fill any  mandatory field and then click  again in name i receive this, note that now I get extra square brakets on the first field booking.

	[{"id":"MURR230044149-1","booking":"MURR230044149-1 Localizador: MUR0002615"}]


### PHP VERSION:
8.2.17

### PHP EXTENSIONS:
Core, date, libxml, openssl, pcre, zlib, filter, hash, json, pcntl, random, Reflection, SPL, session, standard, sodium, mysqlnd, PDO, xml, bcmath, calendar, ctype, curl, dom, mbstring, FFI, fileinfo, ftp, gd, gettext, iconv, imap, intl, ldap, exif, mysqli, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql, Phar, posix, readline, shmop, SimpleXML, soap, sockets, sqlite3, sqlsrv, sysvmsg, sysvsem, sysvshm, tokenizer, xmlreader, xmlwriter, xsl, zip, pdo_sqlsrv, Zend OPcache

### LARAVEL VERSION:
10.48.14.0

### BACKPACK PACKAGE VERSIONS:
backpack/backupmanager: v5.0.3
backpack/basset: 1.3.4
backpack/calendar-operation: 1.0.5
backpack/crud: 6.7.17
backpack/devtools: 3.1.5
backpack/editable-columns: 3.0.9
backpack/generators: v4.0.5
backpack/logmanager: v5.0.2
backpack/permissionmanager: 7.2.1
backpack/pro: 2.2.4
backpack/settings: 3.1.1
backpack/theme-coreuiv2: 1.2.4
backpack/theme-coreuiv4: 1.1.1
backpack/theme-tabler: 1.2.10
@karandatwani92
Copy link
Contributor

Hello @miquelangeld

Can you rephrase? I'm unable to get the problem.

Can you also share the CRUD code so we can know more about the issue?

@miquelangeld
Copy link
Author

Hi @karandatwani92 summing up using a select2_json_from_api field, when you select a value and save, the stored value is something like this: {"id":"MURR230044149-1","booking":"MURR230044149-1 Localizador: MUR0002615"}

But if the page reloads after you have selected an option (for example by saving the form without having filled in a required field causing a reload), and then save the form, then the same select2_json_from_api store in DB

[{"id":"MURR230044149-1","booking":"MURR230044149-1 Localizador: MUR0002615"}] adding this square brackets

$this->crud->addField([
'label' => 'Pax Name', // Table column heading
'type' => 'select2_json_from_api',
'minimum_input_length' => 0,
'name' => 'name', // the column that contains the ID of that connected entity
'attribute' => "name", // foreign key attribute that is shown to user
'include_all_form_fields' => true,
'data_source' => url('/admin/bookings/ajax-bookings-avalon-pax-options'),
]);

I hope I have explained myself better this time :)

@miquelangeld miquelangeld changed the title select2_json_from_api behaviour when page is reloaded select2_json_from_api behaviour when form is reloaded Jul 16, 2024
@pxpm
Copy link
Contributor

pxpm commented Jul 22, 2024

ping @karandatwani92

@pxpm pxpm added Possible Bug A bug that was reported but not confirmed yet. and removed triage labels Jul 22, 2024
@pxpm
Copy link
Contributor

pxpm commented Dec 4, 2024

Hey @miquelangeld sorry for the time it took to solve this issue. It seems it felt through the cracks 😢

I've now released backpack/pro 2.2.29 with the fix for the inconsistent behavior. Thank you very much for the time spent raising and explaining how to reproduce the issue 🙏

Let us know if you experience some other issues and we hopefully can fix them faster 😃

Cheers

@adriallongarriu
Copy link
Contributor

Hey @miquelangeld sorry for the time it took to solve this issue. It seems it felt through the cracks 😢

I've now released backpack/pro 2.2.29 with the fix for the inconsistent behavior. Thank you very much for the time spent raising and explaining how to reproduce the issue 🙏

Let us know if you experience some other issues and we hopefully can fix them faster 😃

Cheers

@pxpm there is and error with this change. I was facing the same issue i debuged and found the select is empty(on edit or saving the form without having filled in a required field) because is calling a undeclared variable.

Actual code:

@if ($value)
    @if (is_object($value))
        <option value="{{ json_encode($value) }}" selected>
            {{ $item->{$field['attribute']} ?? '' }}
        </option>
    @else
        @foreach ($value as $item)
            @php
                $item = is_string($item) ? json_decode($item) : (object) $item;
            @endphp
            <option value="{{ json_encode($item) }}" selected>
                {{ $item->{$field['attribute']} ?? '' }}
            </option>
        @endforeach
    @endif
@endif

The variable $item is declared inisde the loop not before. For now i override the file.
I change the $item to use $value:

@if (is_object($value))
    <option value="{{ json_encode($value) }}" selected>
        {{ $value->{$field['attribute']} ?? '' }}
    </option>
@else
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Possible Bug A bug that was reported but not confirmed yet.
Projects
Status: Done
Development

No branches or pull requests

4 participants