Laravel-Backpack / CRUD

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

[Bug] Cannot specify pivot field as optional #5696

Closed cod3rshotout closed 2 weeks ago

cod3rshotout commented 2 weeks ago

Bug report

What I did

I have created a repeatable field using the following:

CRUD::field([
  'name' => 'attachments',
  'label' => 'Attachments',
  'type' => 'relationship',
  'subfields' => [],
  'pivotSelect' => [
    'label' =>  'Select attachment','
    'allows_null' => true,
    'inline_create' => [
      'entity' => 'attachment',
      'modal_route' => route('attachment-inline-create')
    ],
  ],
  'new_item_label' => 'Add new item',
  'init_rows' => 1,
  'min_rows' => 1,
]);

What I expected to happen

The pivot field shouldn't be required.

What happened

When I submit the form I get: pivot field is required.

What I've already tried to fix it

I have specified "allows_null" to true, so I guess it's not working.

Is it a bug in the latest version of Backpack? Yes.

After I run composer update backpack/crud the bug... is it still there? Yes.

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

PHP VERSION:

8.2.4

PHP EXTENSIONS:

Core, date, libxml, openssl, pcre, sqlite3, zlib, ctype, curl, dom, fileinfo, filter, ftp, hash, iconv, json, mbstring, SPL, session, PDO, pdo_sqlite, standard, posix, random, readline, Reflection, Phar, SimpleXML, tokenizer, xml, xmlreader, xmlwriter, mysqlnd, bcmath, exif, gd, gmp, imagick, intl, memcached, pcntl, pdo_mysql, pdo_pgsql, redis, soap, sodium, zip

LARAVEL VERSION:

11.28.0.0

BACKPACK PACKAGE VERSIONS:

backpack/basset: 1.3.6 backpack/crud: 6.7.41 backpack/filemanager: 3.0.8 backpack/generators: v4.0.7 backpack/logmanager: v5.0.2 backpack/medialibrary-uploaders: 1.2.1 backpack/newscrud: 5.1.0 backpack/pagemanager: 3.3.2 backpack/permissionmanager: 7.2.1 backpack/pro: 2.2.21 backpack/revise-operation: 2.0.0 backpack/settings: 3.1.1 backpack/theme-tabler: 1.2.13

pxpm commented 2 weeks ago

Hey @cod3rshotout thanks for the questions.

I don't think there is any bug here, pivot field is a mandatory field to create the relation, otherwise how would the models relate if the connecting key was not set in the database ?

I will be closing this, but if you feel I am wrong, please continue the conversation and we can re-evaluate. 👍

Cheers

cod3rshotout commented 2 weeks ago

Hey @cod3rshotout thanks for the questions.

I don't think there is any bug here, pivot field is a mandatory field to create the relation, otherwise how would the models relate if the connecting key was not set in the database ?

I will be closing this, but if you feel I am wrong, please continue the conversation and we can re-evaluate. 👍

Cheers

Hi, I don't think you're wrong but I need a little help please. I need to create the repeatable structure you see in this image:

How can I do it without it being a mandatory field?

pxpm commented 2 weeks ago

Hey @cod3rshotout

You can overwrite all the "backpack defaults" for pivot field, so setting 'pivotSelect' => ['validationRules' => []] in your field definition should work.

It's possible you will enter in errors while attempting to save the relation, for that you can overwrite the store() function in your CrudController: https://backpackforlaravel.com/docs/6.x/crud-operation-create#override-the-store-method and adjust as needed.

Like I told you, it's not recommended, but we will not step in front of your desire to do the stuff the way you want. You wish, you have it 😄

Cheers

cod3rshotout commented 2 weeks ago

Hey @cod3rshotout

You can overwrite all the "backpack defaults" for pivot field, so setting 'pivotSelect' => ['validationRules' => []] in your field definition should work.

It's possible you will enter in errors while attempting to save the relation, for that you can overwrite the store() function in your CrudController: https://backpackforlaravel.com/docs/6.x/crud-operation-create#override-the-store-method and adjust as needed.

Like I told you, it's not recommended, but we will not step in front of your desire to do the stuff the way you want. You wish, you have it 😄

Cheers

maybe I'm missing something, but... I added the following as you said and the field is still shown as mandatory:

'pivotSelect' => [
  'label' =>  __('backend/page.attachments.select_attachment'),
  'validationRules' => [],
  'inline_create' => [
    'entity' => 'attachment',
    'modal_route' => route('attachment-inline-create') . '?intranet=' . $intranet
  ],
],
pxpm commented 2 weeks ago

Ahh sorry, you also need to remove it from required fields.

After the field definition do:

$requiredFields = CRUD::getOperationSetting('requiredFields');
CRUD::setOperationSetting('requiredFields', array_diff($requiredFields, ['attachments[attachments]']);

Cheers

cod3rshotout commented 2 weeks ago

Ahh sorry, you also need to remove it from required fields.

After the field definition do:

$requiredFields = CRUD::getOperationSetting('requiredFields');
CRUD::setOperationSetting('requiredFields', array_diff($requiredFields, ['attachments[attachments]']);

Cheers

Thank you! Have a good day :)