2015-09-27 79 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