laravel 5.4 在请求验证之前修改数据
laravel 5.4 modify data before validation in request
我有我的自定义请求,它扩展了 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 方法,因为这看起来像是对此的常见方法,但它没有被执行(所以没有被重写 - 受保护?).
虽然我没试过,但它似乎应该可以工作你可以覆盖 validationData
from Illuminate\Foundation\Http\FormRequest
class like.
/**
* 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
方法 Illuminate\Http\Concerns\InteractsWithInput
trait
/**
* 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;
}
你能修改请求吗?
$request->merge(['field' => 'new value']);
好的,我找到错误的地方了。我确实拆分了前端请求和后端请求调用。因为我在处理后端请求,前端请求没有覆盖任何东西……所以这是我的错,那里没有错误,很抱歉浪费时间,但非常感谢社区!
嗯,我确定,这有助于修改输入,它对我有用。[laravel 5.4]
放置这个
$input['url'] = $url;
$this->replace($input);
dd($input);
在 listFormRequest 中。 (如果您遵循上面使用的答案,请使用 $all
而不是 $input
。
这仅更改输入,即使在控制器中也可用。您仍然需要找到一种方法将其插入数据库,或者做其他事情以使用修改后的输入以在 blade.
中使用它
我有我的自定义请求,它扩展了 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 方法,因为这看起来像是对此的常见方法,但它没有被执行(所以没有被重写 - 受保护?).
虽然我没试过,但它似乎应该可以工作你可以覆盖 validationData
from Illuminate\Foundation\Http\FormRequest
class like.
/**
* 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
方法 Illuminate\Http\Concerns\InteractsWithInput
trait
/**
* 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;
}
你能修改请求吗?
$request->merge(['field' => 'new value']);
好的,我找到错误的地方了。我确实拆分了前端请求和后端请求调用。因为我在处理后端请求,前端请求没有覆盖任何东西……所以这是我的错,那里没有错误,很抱歉浪费时间,但非常感谢社区!
嗯,我确定,这有助于修改输入,它对我有用。[laravel 5.4]
放置这个
$input['url'] = $url;
$this->replace($input);
dd($input);
在 listFormRequest 中。 (如果您遵循上面使用的答案,请使用 $all
而不是 $input
。
这仅更改输入,即使在控制器中也可用。您仍然需要找到一种方法将其插入数据库,或者做其他事情以使用修改后的输入以在 blade.
中使用它