Roho
Roho

Reputation: 23

The given data invalid

I am building an API using laravel 5.5 and i am implementing a store method with validation. In a normal laravel application if any of the fields fail you get the "Field *** is required" but i am not getting that why ? i keep getting the " The given data was invalid" and this throws just this and doesn't tell you what field is wrong. What am i doing wrong.

    $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);
}

so if i don't enter anything into 'dob' field or leave or send the post request without entering anything i should be getting the bla bla field is required. but instead i am getting that below error log from postman.

"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)",

I would honestly appreciate any help. Thanks in Advance

Upvotes: 1

Views: 7893

Answers (2)

Nasik Ahd
Nasik Ahd

Reputation: 798

I have face the same issue with Dingo Api. And the issue came from FormRequest class. Refer this link.

may be your API also have a different FormRequest class to handle the request.

According to Dingo API.

use Dingo\Api\Http\FormRequest;

Instead of

use Illuminate\Foundation\Http\FormRequest;

Upvotes: 1

Niklesh Raut
Niklesh Raut

Reputation: 34914

Use , comma instead | here , pipe means new check not options

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

Check in docs : in:foo,bar,...

Upvotes: 3

Related Questions