【问题标题】:Request Validation with Laravel 5.3使用 Laravel 5.3 进行请求验证
【发布时间】:2017-01-24 19:40:36
【问题描述】:

我正在尝试使用 laravel 内置请求方法验证我的 api 调用。 我已使用 --resource 使其成为 REST。

OneTimePasswordController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Models\OneTimePassword as Model;
use App\Http\Requests\OneTimePasswordReq;


class OneTimePasswordController extends Controller
{
    /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
    public function index(UserController $user)
    {
        //
        $otps = Model::get();

        return response()->json($otps);
    }

    /**
    * Show the form for creating a new resource.
    *
    * @return \Illuminate\Http\Response
    */
    public function create()
    {
        //
    }

    /**
    * Store a newly created resource in storage.
    *
    * @param  \Illuminate\Http\Request  $request
    * @return \Illuminate\Http\Response
    */
    public function store(OneTimePasswordReq $request)
    {

        $insert = Model::create($request->all());
        return response()->json($insert);
    }

    /**
    * Display the specified resource.
    *
    * @param  int  $id
    * @return \Illuminate\Http\Response
    */
    public function show($id)
    {
        //
    }

    /**
    * Show the form for editing the specified resource.
    *
    * @param  int  $id
    * @return \Illuminate\Http\Response
    */
    public function edit($id)
    {
        //
    }

    /**
    * Update the specified resource in storage.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  int  $id
    * @return \Illuminate\Http\Response
    */
    public function update(Request $request, $id)
    {
        //
    }

    /**
    * Remove the specified resource from storage.
    *
    * @param  int  $id
    * @return \Illuminate\Http\Response
    */
    public function destroy($id)
    {
        //
        $delete = Model::destroy($id);
        return response()->json($delete);
    }
}

OneTimePasswordReq

    <?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;

class OneTimePasswordReq extends FormRequest
{
    /**
    * 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()
    {
        return [
        'mobile' => 'required',
        'code' => 'required',
        ];
    }

    protected function formatErrors(Validator $validator)
    {
        return $validator->errors()->all();
    }

    /**
     * Set custom messages for validator errors.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'mobile.required'=>"Mobile field is required"
        ];
    }
}

如果我通过我的参数和值,它会被插入。

现在,如果我通过删除移动字段的发布请求,我预计会出现验证错误。 但是调用的是 index 方法,该方法从 url 获取所有数据。

我的理解是请求被拒绝,因为缺少参数,问题是为什么它更改为索引方法以及我如何得到错误?

注意:我知道$validator-&gt;fails() 的概念,我不想把它放到我的控制器中,因为laravel 提供了这个。

【问题讨论】:

  • 您确定是“POST”请求而不是“GET”请求吗?
  • 你的表单的动作设置为store()函数?
  • 你能用你的OneTimePasswordControllerOneTimePasswordReq classes 更新这个问题吗
  • 我更新了完整的文件。@MattMcDonald。
  • 您实现逻辑的方式看起来一切正常 - 我最好的猜测是您的应用程序中的其他地方在发现验证错误时正在执行重定向。

标签: php validation laravel-5.3


【解决方案1】:

当我使用邮递员对其进行测试时,它会重定向到相同的路线并选择 get。

如果使用 javascript 代码调用,则会显示错误。

要与邮递员核对,您需要添加标题

X-Requested-With: XMLHttpRequest

感谢所有支持者。

【讨论】:

  • 哇!谢谢,我不知道你是怎么解决的,但你救了我!
猜你喜欢
  • 1970-01-01
  • 2017-01-19
  • 2017-03-18
  • 2017-06-02
  • 2017-06-13
  • 2017-04-10
  • 1970-01-01
  • 2017-11-21
  • 2020-01-13
相关资源
最近更新 更多