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

[4.2][Feature] Custom save request stripping #3987

Merged
merged 3 commits into from
Dec 6, 2021

Conversation

tabacitu
Copy link
Member

@tabacitu tabacitu commented Dec 4, 2021

Fixes #3792
Depends on #3986 (that one needs to be merged into 4.2 first)

WHY

BEFORE - What was wrong? What was happening before this PR?

As reported in #3792 , if a dev wanted to strip the request in a different way than what saveAllInputsExcept allowed, it was difficult, they'd probably have to override the store() and update methods.

We were basically providing three options for saveAllInputsExcept:

  • false or null => save ONLY inputs that have fields onpage
  • ['one_item', 'another_item'] => save all input EXCEPT the ones in this array

This was very limiting.

AFTER - What is happening after this PR?

Instead of defining an array, you can define a closure where you do whatever you want to the $request and return an array of inputs you want saved.

HOW

How did you achieve that, in technical terms?

I've

  • renamed the config from saveAllInputsExcept to strippedRequest
  • changed it to allow null, true or closure

Is it a breaking change or non-breaking change?

Fo shizzle my nizzle.

How can we test the before & after?

Can't really test the "before", as it was working, but had limited capacity.

To test the "after" you can:

  • define smth like this in your config/backpack/operations/create.php:
    /**
     * Before saving the entry, how would you like the request to be stripped?
     * - false - fall back to Backpack's default (ONLY save inputs that have fields)
     * - closure - process your own request (example removes all inputs that begin with underscode)
     *
     * @param  \Illuminate\Http\Request  $request
     * @return  array
     */
    'strippedRequest' => (function ($request) {
        return $request->except('_token', '_method', '_http_referrer', '_current_tab', '_save_action');
    }),
  • do a dd($crud->getStrippedRequest()) in CreateOperation.php::store(), right before the call to $this->crud->create() (line 77).

This is ready to test & review. But before merging it:

Copy link
Contributor

@pxpm pxpm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing to say here.

Flawless job @tabacitu !

Base automatically changed from move-operation-configs-to-separate-files to 4.2 December 6, 2021 10:56
@tabacitu tabacitu mentioned this pull request Dec 6, 2021
tabacitu added a commit to Laravel-Backpack/docs that referenced this pull request Dec 6, 2021
tabacitu added a commit to Laravel-Backpack/docs that referenced this pull request Dec 6, 2021
@tabacitu tabacitu merged commit 05e7167 into 4.2 Dec 6, 2021
@tabacitu tabacitu deleted the custom-save-request-stripping branch December 6, 2021 11:18
@tabacitu tabacitu mentioned this pull request Feb 4, 2022
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants