来自ajax请求的验证文件[Laravel 5]

时间:2015-09-27 16:07:43

标签: ajax laravel 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';
    }

 }

这个文件我需要验证agins规则数组:

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

我需要检查所有规则是否正常,然后更新评论......任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:2)

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

代码背后的逻辑: 发送请求发送服务器,路由文件将apiUpdateComment发送给$request内的所有变量。但是在执行函数代码之前,验证程序会检查UpdateCommentRequest中的规则。如果测试失败,它将返回错误。如果它通过了注释,则id将被更新。

相关问题