-
Notifications
You must be signed in to change notification settings - Fork 917
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 #3789
Make requests modifiable #3789
Conversation
The inspection completed: No new issues |
I think we should do this NOW, in 4.2. I especially like the fact that this adds the ability to modify the request in two different but important moments: A) Before the validation/**
* Modify the input values
*
* @return void
*/
protected function prepareForValidation() {
// get the input
$input = array_map('trim', $this->all());
// check newsletter
if (!isset($input['newsletter'])) {
$input['newsletter'] = false;
}
// replace old input with new input
$this->replace($input);
} B) After the validation /**
* Get data to be validated from the request.
*
* @return array
*/
public function validationData() {
return array_merge(
$this->all(),
[
'number' => preg_replace("/[^0-9]/", "", $this->number)
]
);
} In addition to that:
@pxpm you should take a look at this too, I think this PR is much bigger than it seems. Small change, but with HUGE positive implications. |
@tabacitu I carefully read your and @promatik explanations on this. I think we are opening Fillable fields means only that the field can be mass assigned in the Model, not that the developer wants that field to be processed in that particular form. Allowing all the
public function setupCreateOperation() {
$this->setOperationSetting('shadow_fields', ['user_id']);
} This would allow the developer to then set the Apart from that the Let me know, |
Hey @pxpm
Sorry... I don't understand. How does this PR do that? From what I can tell it does one thing - run You CAN bypass that, by telling Backpack to not do Please explain @pxpm - something's off here. That being said, I did think of a way this could be a breaking change, though. What if the dev has already changed his request, in the controller, in public function store() {
// change the request()
return $this->traitStore();
} Wouldn't this PR make Backpack ignore their changes? And prevent people from doing that? What do you think @promatik ? |
@tabacitu my concern is about That's why I proposed the "shadow fields" that are fields that developer don't want exposed in the form page, but want backpack requests to allow them to go through the saving process. I get that it's not a great idea, but is more to mark a standpoint than a solution for the problem itself. Let me know if I didn't make myself clear. |
OOooohhhhhh. NOW I get it! Thank you @pxpm . I agree with you 100% - I did NOT notice we're using |
Regarding my concern about the breaking change this entail, I've tested this and it's true. It IS a breaking change for the people who have changed the request in the Controller - in For those people, what we'll need to say is: // if you've done anything like this in your CrudController to manipulate the request
//
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { store as traitStore; }
public function store()
{
$this->crud->setOperationSetting('saveAllInputsExcept', ['save_action']);
$this->crud->getRequest()->request->add(['updated_by' => backpack_user()->id]);
return $this->traitStore();
}
// that will no longer work, because Backpack no longer saves the Illuminate\Http\Request
// but instead saves the FormRequest you've defined for validation;
// you can do THIS instead, to achieve the same thing from the CrudController
public function store()
{
$this->crud->set('strippedRequest', function($request) {
CRUD::getRequest()->request->add([
'updated_by' => backpack_user()->id,
]);
return CRUD::getRequest()->except(['_save_action', '_token', '_http_referrer']);
});
return $this->traitStore();
}
// but what we recommend you do is move that logic to the FormRequest that holds your
// validation too, that way everything that concerns the Request is in one place
// for example in your ProductCrudRequest you could do:
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]);
}
// then make sure inside rules() you have ALL the inputs you want saved, including updated_by;
// for those you want saved but can be null you can use the `nullable` validation rule; Do you guys think this is too much of a breaking change? Or worth it? |
Please reply in #3988 - that PR there is the same as this one, but without the fillable thing, and pointing to the refactored branch that has |
Related with #3783.
Backpack doesn't allow to modify the request on the Request class, because after the validation, the request is discarded.
This PR sends the request to the
getStrippedSaveRequest
function so it doesn't get discarded.Since developers may want to modify the request, in order to add "static" fields like the author of a post, with
author_id
field, this field in not in the crud field list, so$this->getAllFieldNames()
will not return it andgetStrippedSaveRequest()
will strip off that field.To overcome this, I made
getStrippedSaveRequest()
not strip crud fields AND model fillable. This way, ifauthor_id
is fillable, even if it's not in the crud fields, it