我有我的自定義請求,它擴展了Backpack CrudController。laravel 5.4在請求驗證前修改數據
現在我想重寫ValidatesWhenResolvedTrait的prepareForValidation因爲它看起來像修改我輸入的數據在正確的地方,但我不能想出如何......
所以我的第一個問題是,能我重寫這個方法?它的保護......
protected function prepareForValidation()
我的第二個問題,如何修改我輸入的請求或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方法,因爲這看起來像普通的形式給出了這一點,但它不執行(所以不重寫 - 保護?)。
我目前無法測試,但這可能只是一個錯字? 'private function prepareForValidation()',請嘗試將其更改爲public。 – Sheraz
nope,沒有區別...不能覆蓋該方法:/ thx! –
'prepareForValidation()'是否受保護並不重要,這是您應該重寫以執行這些類型的任務的方法。在這個方法中,調用'$ this-> replace()'來替換你的輸入和消毒變體。 –