Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.04k stars 885 forks source link

select2_json_from_api behaviour when form is reloaded #5557

Open miquelangeld opened 1 month ago

miquelangeld commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 :)

pxpm commented 1 month ago

ping @karandatwani92