2017-02-28 59 views
1

我有我的自定義請求,它擴展了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方法,因爲這看起來像普通的形式給出了這一點,但它不執行(所以不重寫 - 保護?)。

+0

我目前無法測試,但這可能只是一個錯字? 'private function prepareForValidation()',請嘗試將其更改爲public。 – Sheraz

+0

nope,沒有區別...不能覆蓋該方法:/ thx! –

+0

'prepareForValidation()'是否受保護並不重要,這是您應該重寫以執行這些類型的任務的方法。在這個方法中,調用'$ this-> replace()'來替換你的輸入和消毒變體。 –

回答

0

好的,我發現錯誤在哪裏。我做了前端請求和後端請求調用。由於我正在研究後端請求,所以前端請求不會覆蓋任何東西......所以這是我的錯,沒有錯誤,因爲浪費時間,但是非常感謝社區!

5

雖然我沒有嘗試,但它似乎應該工作,你可以覆蓋validationDataIlluminate\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; 
} 

,或者你可以重寫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; 
} 
+0

我嘗試了他們兩個,但它不接受覆蓋。我需要做些特別的事情來完成這項工作嗎?以某種方式提及特質或課程? –

+0

@ThomasVenturini我已更新我的答案,請檢查歷史記錄,如果您未成功,請更新您的問題讓我們知道您做了什麼 –

0

你可以修改的要求?

$request->merge(['field' => 'new value']); 
+0

我該在哪裏做?如上所述,問題是我找不到要覆蓋的方法。但thx :) –

0

嗯,我敢肯定,這可以在修改輸入幫助,它listFormRequest爲我工作。[laravel 5.4]

地方本

​​

。 (使用$all而不是$input,如果您按照以上使用的答案)。

這隻會改變輸入,即使在控制器中也是如此。您仍然需要找到將其插入到數據庫中的方法,或者執行其他操作以使用修改的輸入在刀片中使用它。

+0

這可能是Thomas Venturini向@Tim Jai提問的答案。 – RajG