2016-08-03 80 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');