我有自定义请求,它扩展了Backpack CrudController。
现在我想覆盖ValidatesWhenResolvedTrait的prepareForValidation,因为它看起来是修改我的传入数据的正确位置,但我无法弄清楚如何......
所以我的第一个问题是,我可以覆盖此方法吗?它受到保护......
protected function prepareForValidation()
我的第二个问题,如何修改Request或FormRreuqest对象的输入?
这是我的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');
}
}
正如您所见,我首先尝试覆盖getValidatorInstance方法,因为这看起来像是常见的方法,但它没有被执行(所以没有被覆盖 - 受保护?)。
答案 0 :(得分:13)
虽然我没有尝试,但似乎它应该可行,你可以覆盖Illuminate\Foundation\Http\FormRequest
类的 Camera
,如。
validationData
或者您可以覆盖Illuminate\Http\Concerns\InteractsWithInput
特质
/**
* 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;
}
方法
all
答案 1 :(得分:1)
你可以修改请求吗?
$request->merge(['field' => 'new value']);
答案 2 :(得分:0)
嗯,我确信,这可以帮助修改输入,它对我有用。[laravel 5.4]
放置此
$input['url'] = $url;
$this->replace($input);
dd($input);
listFormRequest中的。 (如果您按照上面使用的答案,请使用$all
代替$input
。
这只会改变输入,即使在控制器中也是如此。您仍然需要找到一种方法将其插入到DB中,或者执行其他操作以使用修改后的输入在刀片中使用它。
答案 3 :(得分:-1)
好的,我发现了错误的位置。我确实拆分了前端请求和后端请求调用。由于我正在处理后端请求,前端请求没有覆盖任何内容......所以这是我的坏,没有错误,为了浪费时间而烦恼,但是非常感谢社区!