2

i'm building an API, i'm validating the input fields in my StoreLesson.php

    public function rules()
{
    return [
        'title' => 'required',
        'body' => 'required',
    ];
}

i'm using postman to test the API, things are working fine but when i send POST request with empty fields, in postman console in webview i'm getting redirected to welcom.blade.php

//LessonController.php
public function store(StoreLessons $request) 
{
    Lesson::create($request->all());

    return response()->json($validator->errors(), 422);  //i'm not getting any json with errors

    //Lesson::create(input::all());

    return $this->respondCreated('Lesson created successfully');
}

i want to display (return) the validator error as json

thank You

0

2 Answers 2

1

use the validator like this:

$validator = Validator::make($data, $rules);
if ($validator->fails())
    return response()->json($validator);
Sign up to request clarification or add additional context in comments.

9 Comments

i got this error now Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0 Warning: Cannot modify header information - headers already sent in Unknown on line 0 Redirecting to http://localhost:8000.
i think the same but i'm not getting any error response from validator tried many methods but keeps redirecting to home page, is there any way i can achieve this
check if the vidiation fails, if fails var_dump the $validator before return it.
i did the var_dump($validator); but still the same redirection, this is my api frocerie.in/Laravelapi/public/api/v1/lessons
I not get you idea, what's the relation between the api and the validator response in store
|
0
Create StoreLessons Request inside app/Http/Requests.And call this request in LessonController.php you did.

----------
     class StoreLessons 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()
                {
                     return [
                'title' => 'required',
                'body' => 'required',
            ];
                }


                /**
                 * @param array $errors
                 *
                 * @return JsonResponse
                 */
                public function response(array $errors)
                {
                    return response($errors);
                }

                /**
                 * @param Validator $validator
                 *
                 * @return mixed
                 */
                protected function formatErrors(Validator $validator)
                {
                    return $validator->errors()->all();
                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.