2016-08-03 107 views
1

後這是我的形式請求的代碼,我想驗證成功後添加新的變量,所以我可以在我的控制器訪問該變量:Laravel表單請求添加自定義變量驗證

class CouponRequest extends Request 
{ 
    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     return [ 
      'start_year' => 'required', 
      'start_month' => 'required', 
      'start_day' => 'required', 
      'start_time' => 'required', 
      'finish_year' => 'required', 
      'finish_month' => 'required', 
      'finish_day' => 'required', 
      'finish_time' => 'required', 
     ]; 
    } 

    public function afterValidation() 
    { 
     $this->start_date = Carbon::create($this->start_year, $this->start_month, $this->start_day); 
    } 
} 

驗證沒有打完錯誤,我可以在我的控制器調用此實例:

$request->start_date; 

我可以這樣做嗎?

回答

0

我在控制器中進行了驗證。該方法有一個「Request $ request」參數。我有一個我這樣做:

$input = $request->all(); 
$input['my_new_field] = 'the_data'; 
+0

是的,我知道,但我需要創建驗證在表格要求 –

+0

好的。然後你可以做一個dd($ request)來看看這個新字段是否存在。 –

2

你能做到這一點

public function afterValidation() 
{ 
    $this->request->add([ 
     'start_date' => Carbon::create($this->start_year, $this->start_month, $this->start_day) 
    ]); 
} 

public function validate() 
{ 
    parent::validate(); 

    $this->afterValidation(); 
} 

,然後訪問屬性,在控制器

$request->get('start_date');