laravel 5.4 在请求验证之前修改数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42506302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel 5.4 modify data before validation in request
提问by Thomas Venturini
I have my custom Request, which extends the Backpack CrudController.
我有我的自定义请求,它扩展了 Backpack CrudController。
Now I would like to override the prepareForValidation of the ValidatesWhenResolvedTrait since it looks like the right place to modify my incoming data, but I can't figure out how ...
现在我想覆盖 ValidatesWhenResolvedTrait 的 prepareForValidation ,因为它看起来是修改传入数据的正确位置,但我不知道如何......
So my first question is, can I override this method? Its protected ...
所以我的第一个问题是,我可以覆盖这个方法吗?其保护...
protected function prepareForValidation()
And my second question, how can I modify my input on the Request or FormRreuqest objects?
我的第二个问题是,如何修改我对 Request 或 FormRreuqest 对象的输入?
Here is my RequestClass
这是我的 RequestClass
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Config;
class DonationsRequest extends \Backpack\CRUD\app\Http\Requests\CrudRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
// only allow updates if the user is logged in
return \Auth::check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:255',
'email' => 'required|email',
'dob' => 'required|date',
'newsletter' => 'required|boolean',
'country' => 'sometimes|required|in:'.implode(',', Config::get('validation.countries')),
'street' => 'sometimes|required|string|max:255',
'zip' => 'sometimes|required|string|between:4,5',
'city' => 'sometimes|required|string|between:4,255',
'amount' => 'required|numeric|between:1,'.Config::get('donations.max'),
'type' => 'required|in:oo,monthly',
'provider' => 'sometimes|string|nullable',
'product_id' => 'sometimes|exists:products,id|nullable',
'campaign_id' => 'required|exists:campaigns,id',
'status' => 'sometimes|required|in:pending,canceled,success,error',
'profile' => 'sometimes|string|regex:/^profile[0-9]+$/|nullable',
];
}
/**
* Get the validation attributes that apply to the request.
*
* @return array
*/
public function attributes()
{
return [
//
];
}
/**
* Get the validation messages that apply to the request.
*
* @return array
*/
public function messages()
{
return [
//
];
}
private function prepareForValidation()
{
dd('getValidatorInstance custom');
$this->sanitizeInput();
return parent::getValidatorInstance();
}
private function sanitizeInput()
{
dd('sanitizeInput custom');
$data = $this->all();
dd($data);
// overwrite the newsletter field value to match boolean validation
$data['newsletter'] = ($data['newsletter'] == 'true' || $data['newsletter'] == '1' || $data['newsletter'] == true) ? true : false;
return $data;
}
private function validate() {
dd('validate');
}
}
As you can see, I first tried to override the getValidatorInstance method, since this looked like the common aproach to this, but it is not executed (so not overridden - protected?).
正如您所看到的,我首先尝试覆盖 getValidatorInstance 方法,因为这看起来像是对此的常见方法,但它没有被执行(所以没有被覆盖 - 受保护?)。
采纳答案by Thomas Venturini
Ok I found out where the error was. I did split the Frontend Request and the Backend Request Call. Since I was working on the Backend Request the Frontend Request was not overwriting anything ... so it was my bad, no bug there, sry for the waste of time, but a big thanks to the community!
好的,我找到了错误所在。我确实拆分了前端请求和后端请求调用。因为我正在处理后端请求,所以前端请求没有覆盖任何东西......所以这是我的坏事,没有错误,抱歉浪费时间,但非常感谢社区!
回答by ARIF MAHMUD RANA
Although I didn't tried but it seems it should work you can override validationData
from Illuminate\Foundation\Http\FormRequest
class like.
虽然我没有尝试过,但它似乎应该可以工作,您可以validationData
从Illuminate\Foundation\Http\FormRequest
类中覆盖。
/**
* Get data to be validated from the request.
*
* @return array
*/
protected function validationData()
{
$all = parent::validationData();
//e.g you have a field which may be json string or array
if (is_string($playerIDs = array_get($all, 'player_id')))
$playerIDs = json_decode($playerIDs, true);
$all['player_id'] = $playerIDs
return $all;
}
or you can override all
method in Illuminate\Http\Concerns\InteractsWithInput
trait
或者你可以覆盖特征中的all
方法Illuminate\Http\Concerns\InteractsWithInput
/**
* Get all of the input and files for the request.
*
* @return array
*/
public function all()
{
$all = parent::all();
//then do your operation
if (is_string($playerIDs = array_get($all, 'player_id')))
$playerIDs = json_decode($playerIDs, true);
$all['player_id'] = $playerIDs
return $all;
}
回答by Tim Jai
Could you modify the request?
你能修改请求吗?
$request->merge(['field' => 'new value']);
回答by RajG
Well I am sure,this can help in modifying The input, it worked for me.[laravel 5.4]
嗯,我确定,这可以帮助修改输入,它对我有用。[laravel 5.4]
place this
把这个
$input['url'] = $url;
$this->replace($input);
dd($input);
in listFormRequest. (use $all
instead of $input
, if you follow above used answer).
在 listFormRequest 中。(如果您遵循上述使用的答案,请使用$all
代替$input
)。
This only changes input,which is available even in controller. You still need to find a way to insert it into DB, or do something else to use modified input for using it in blade.
这只会改变输入,即使在控制器中也可以使用。您仍然需要找到一种方法将它插入到数据库中,或者做一些其他的事情来使用修改后的输入在刀片中使用它。