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] decodeJsonCastedAttributes sets field values to null #2880

Closed
NicklasT opened this issue May 28, 2020 · 2 comments
Closed

[Bug] decodeJsonCastedAttributes sets field values to null #2880

NicklasT opened this issue May 28, 2020 · 2 comments
Assignees
Labels

Comments

@NicklasT
Copy link

Bug report

What I did

I am trying to use spatie/laravel-tags together with Backpack.

What I expected to happen

Being able to create a simple crud for creating/updating tags.

What happened

Field values set to null by backpack before saving.

What I've already tried to fix it

Here are my fields from my TagCrudController:

    public function setupCreateOperation()
    {
        // ADD FIELDS
        $this->crud->addField([
            'name' => 'name',
            'label' => 'Name',
            'type' => 'text',
        ]);
        $this->crud->addField([
            'name' => 'slug',
            'label' => 'Slug',
            'type' => 'text',
        ]);
        $this->crud->addField([
            'name' => 'type',
            'label' => 'Type',
            'type' => 'text',
        ]);

When trying to create or update an entry, the name and slug is suddenly null.

I have tracked this down to the following;

In /vendor/backpack/crud/src/app/Library/CrudPanel/Traits/Create.php

    public function create($data)
    {
// dd($data) here and everything looks fine, all fields have the correct values
// array:3 [▼
//  "name" => "Sports"
//  "slug" => "sports"
//  "type" => "vlog"
//]
        $data = $this->decodeJsonCastedAttributes($data);
// dd($data) here and the 'name' and 'slug' are now both null

Going further to the decodeJsonCastedAttributes function;

In /vendor/backpack/crud/src/app/Library/CrudPanel/Traits/Fields.php

    public function decodeJsonCastedAttributes($data)
    {
....
                if (in_array($fieldCasting, $jsonCastables) && isset($data[$field['name']]) && ! empty($data[$field['name']]) && ! is$
                    try {
// dd(json_decode($data[$field['name']]) gives null, but an exception is not thrown, so the field now is set to null.
                        $data[$field['name']] = json_decode($data[$field['name']]);
                    } catch (\Exception $e) {
                        $data[$field['name']] = [];
                    }
                }

It seems this function is trying to make sure the fields are not json encoded twice, but in this case they are just set to null by this function.

$casted_attributes given back from the tag model are the following:

array:3 [▼
  "id" => "int"
  "name" => "array"
  "slug" => "array"
]

I don't quite understand what is happening here, or if this is how it is intended to work. If so, what can I do to fix the issue?

Thank you again for all your hard work.

Backpack, Laravel, PHP, DB version

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

PHP VERSION:

PHP 7.2.24-0ubuntu0.18.04.6 (cli) (built: May 26 2020 13:09:11) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.24-0ubuntu0.18.04.6, Copyright (c) 1999-2018, by Zend Technologies
with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans

LARAVEL VERSION:

v7.11.0@f4563bd2e0875c59a1f7967abdbe5cef7f240117

BACKPACK VERSION:

4.1.5@e2c166b065f6b405747db5a5f4d36a2a88f8dc22

@welcome
Copy link

welcome bot commented May 28, 2020

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication channels:

  • Bug Reports, Feature Requests - Github Issues (here);
  • Quick help (How do I do X) - Gitter Chatroom;
  • Long questions (I have done X and Y and it won't do Z wtf) - Stackoverflow, using the backpack-for-laravel tag;
  • Showing off something you've made, asking for opinion on Backpack/Laravel matters - Reddit;

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

--
Justin Case
The Backpack Robot

@NicklasT NicklasT changed the title [Bug] [Bug] decodeJsonCastedAttributes sets field values to null May 28, 2020
@pxpm
Copy link
Contributor

pxpm commented May 28, 2020

Hello @NicklasT

There is already an issue raised here about that package: #2725

Not the same error, but I think you will endup in the same road.

From my understanding that package comes with spatie/translatable by default, one we also use to provide translatable cruds.

My guess is that Backpack picks it up and those fields are treated as translatable, stored in json and not plain text as you are expecting when creating the crud.

One work around would be to create a translatable crud for them, a regular one but with translations (as provided by default in that package). backpack docs

And here is when I think you will hit the barrier of the issue I referenced later, but I think there is a work around provided by @tabacitu

If you come up with a solution we can incorporate in backpack, or even provide some help to someone with same issue, please let us know in the comments.

Wish you the best,
Pedro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants