2015-09-27 106 views
0

這裏是我的驗證請求:規則驗證的Fileds [Laravel 5]

<?php 

namespace App\Http\Requests; 

use App\Http\Requests\Request; 
use Illuminate\Support\Facades\Auth; 

class UpdateCommentRequest extends Request { 

    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() { 
     return true; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() { 
     $user = Auth::user()->id; 
     return [ 
      'comment' => 'required|between:15,600', 
      'projectID' => "required|exists:project_group,project_id,user_id,$user|numeric", 
      'order' => "required|numeric", 
      'level' => "required|numeric" 
     ]; 
    } 

} 

而且在我的模型我有這樣的:

public function apiUpdateComment(UpdateCommentRequest $request){ 

    $comment = Comment::find(Input::get("order")); 
    $comment->text = Input::get('comment'); 
    if($comment->save()){ 
     return 'success'; 
    } 

} 

這的Fileds我需要驗證agins規則陣列:

array(
     'comment' => Input::get('comment'), 
     'projectID' => Input::get('projectID'), 
     'order' => Input::get("order"), 
     'level' => Input::get("level"), 
    ); 

我需要檢查所有規則是否正常,然後更新評論......任何人都可以提供幫助嗎?

+0

我不明白這個問題。如果你傳遞了一個Request對象,那麼只有在規則()被傳遞時纔會通過請求。所以''apiUpdateComment'只會在UpdateCommentRequest-> rules()返回true時運行。 – dotty

回答

2
public function apiUpdateComment(UpdateCommentRequest $request){ 
    $comment = Comment::find($request->get("order")); 
    $comment->text = $request->get('comment'); 
    if($comment->save()){ 
     return 'success'; 
    } 
} 

代碼背後的邏輯: POST請求是發送服務器和路由文件與$request內的所有變量並將其發送的所述apiUpdateComment。但是在函數的代碼執行之前,驗證程序會檢查您的UpdateCommentRequest中的規則。如果測試失敗,它將返回錯誤。如果它通過與id的評論將被更新。

+0

這段代碼驗證所有的ajax文件或只是這個:'$ request-> get(「order」)','$ request-> get('comment')'?我需要驗證所有的文件,如果驗證通過更新:'$ comment-> text' –

+0

驗證發生在'UpdateCommentRequest'類中。 'comment'只是正在更新的字段 – mimo