给定数据无效

时间:2018-05-24 16:57:14

标签: laravel laravel-5.5 laravel-validation

我正在使用laravel 5.5构建API,我正在实现一个带验证的存储方法。在正常的laravel应用程序中,如果任何字段失败,您将获得“字段***是必需的”,但我不明白为什么?我一直得到“给定数据无效”,这就抛出了这个并没有告诉你哪个字段是错误的。我做错了什么。

    $profile = new Profile();
        $profile->dob = $request['dob'];
        $profile->first_name = $request['first_name'];
        $profile->last_name = $request['last_name'];
        $profile->email = $request['email'];
        $profile->id_type = $request['id_type'];
        $profile->id_number = $request['id_number'];
        $profile->address = $request['address'];
        $profile->gender = $request['gender'];
        $profile->user_id = $user->id;
        $profile->save();
  public function validateRequest (Request $request){
    $rules = [
        'dob'=>'required',
        'first_name'=>'required',
        'last_name'=>'required',
        'email'=>'required',
        'id_type'=>'required',
        'id_number'=>'required',
        'address'=>'required',
        'gender'=>'required|in:female|male|none',
    ];
    return  $this->validate($request,$rules);
}

所以,如果我没有在'dob'字段中输入任何内容或离开或发送帖子请求而不输入任何内容我应该得到bla bla字段是必需的。但我会从邮递员那里得到错误日志。

"message": "The given data was invalid.",
    "status_code": 500,
    "debug": {
        "line": 306,
        "file": "/Users/robertjoseph/Documents/Development/Aglett/Uncompleted/WAPI/vendor/laravel/framework/src/Illuminate/Validation/Validator.php",
        "class": "Illuminate\Validation\ValidationException",
        "trace": [
            "#0 /Users/robertjoseph/Documents/Development/Aglett/Uncompleted/WAPI/vendor/laravel/framework/src/Illuminate/Foundation/Validation/ValidatesRequests.php(46): Illuminate\Validation\Validator->validate()",
"#1/Users/robertjoseph/Documents/Development/Aglett/Uncompleted/WAPI/app/Http/Controllers/ProfileController.php(76): App\Http\Controllers\Controller->validate(Object(Dingo\Api\Http\Request), Array)",
"#2 /Users/robertjoseph/Documents/Development/Aglett/Uncompleted/WAPI/app/Http/Controllers/ProfileController.php(44): App\Http\Controllers\ProfileController->validateRequest(Object(Dingo\Api\Http\Request))",
            "#3 [internal function]: App\Http\Controllers\ProfileController->store(Object(Dingo\Api\Http\Request))",
            "#4 /Users/robertjoseph/Documents/Development/Aglett/Uncompleted/WAPI/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)",
            "#5 /Users/robertjoseph/Documents/Development/Aglett/Uncompleted/WAPI/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('store', Array)",

我真的很感激任何帮助。在此先感谢

2 个答案:

答案 0 :(得分:1)

在此处使用,逗号代替|,竖线表示新检查不是选项

   'gender'=>'required|in:female,male,none',

签入文档:in:foo,bar,...

答案 1 :(得分:1)

我遇到Dingo Api的相同问题。问题出在 FormRequest 类。 请参阅this链接。

可能是您的API也具有另一个 FormRequest 类来处理请求。

根据Dingo API。

  

使用Dingo \ Api \ Http \ FormRequest;

代替

  

使用Illuminate \ Foundation \ Http \ FormRequest;