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

Make requests modifiable v2 #3988

Merged
merged 2 commits into from
Dec 6, 2021
Merged

Make requests modifiable v2 #3988

merged 2 commits into from
Dec 6, 2021

Conversation

tabacitu
Copy link
Member

@tabacitu tabacitu commented Dec 4, 2021

WHY

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

As reported in #3789, the FormRequest wasn't passed to the saving. So if you edited the request there, tough luck.

AFTER - What is happening after this PR?

The FormRequest gets passed to the saving.

HOW

Is it a breaking change or non-breaking change?

BREAKING

How can we test the before & after?

Do this in your FormRequest:

    protected function prepareForValidation()
    {
        // add something to the request
        $this->request->add(['updated_by' => backpack_user()->id]);
    }

BEFORE this PR you'll notice it's ignored, it doesn't reach the saving part (if you dd() before it gets saved).
AFTER this PR it does reach it.

Of course, then it will get stripped by getStrippedSaveRequest(), but that's a different problem, but you can do something like this then (and add updated_by to your validation rules as nullable):

    protected function prepareForValidation()
    {
        // tell Backpack to save the validated inputs (instead of all inputs that have fields)
        \CRUD::set('create.strippedRequest', function ($request) {
            return $request->validated();
        });

        // add something to the request
        $this->request->add(['updated_by' => backpack_user()->id]);
    }

@tabacitu
Copy link
Member Author

tabacitu commented Dec 4, 2021

Todo:

@tabacitu tabacitu requested review from promatik and pxpm December 4, 2021 11:30
Copy link
Contributor

@promatik promatik left a comment

Choose a reason for hiding this comment

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

I approve the breaking change!
This makes it possible to edit the Request, I think it's a good think to have.

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.

I like this.

I think it makes this bit of functionality way way more easy to understand and work with.

No more store() and update() overrides to add a simple value in the request. 👍

Base automatically changed from custom-save-request-stripping to 4.2 December 6, 2021 11:18
@tabacitu tabacitu merged commit db8e28e into 4.2 Dec 6, 2021
@tabacitu tabacitu deleted the make-requests-modifiable-v2 branch December 6, 2021 13:39
@tabacitu tabacitu mentioned this pull request Dec 6, 2021
@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.

3 participants