0

I am trying to create a user authentication using vue and laravel, after register button click, I am sending the form data to the create user controller where I have coded in this manner

public function store(Request $request)
{
    //

    $validator = Validator::make($request->all(), [
        'firstname'  =>  'required',
        'lastname'   =>  'required',
        'email'      =>  'required|email|unique:users',
        'password'   =>  'required|min:8',
        'phoneno'    =>  'required'
    ]);

    if ($validator->fails()){
        return response($validator->getMessageBag()->jsonSerialize(), Response::HTTP_UNPROCESSABLE_ENTITY); 
    }

    $firstname = $request->firstname;
    $lastname = $request->lastname;
    $email = $request->email;
    $password = $request->password;
    $phoneno = $request->phoneno;
    return response($request, Response::HTTP_OK);
}

and from my frontend, i am makking an ajax request using axios, i am sending the form data as:

    let formdata = this.user;
    axios.post('/api/user', {
        data: formdata
    }).then((data) => {
        console.log(data);
    })
    .catch((error) => {
        console.log(error);
    });

But in cases where there are errors, i dont get the json i passed from the $validator->fails() check i only get to see: image from registering with errors

Is there a way i can get the json i passed on error?

5
  • Are you positive you are meeting the validation requirements? Have you tried looking at what might be in the request? dd($request->all()); Commented Dec 12, 2018 at 17:25
  • positive, the form data gets to the controller, my concern isnt about the data getting to the controller but rather about getting the errors as json back to the view... if i made the data pass all validation checks, i wont get any problems Commented Dec 12, 2018 at 17:29
  • @AhmedNourJamalEl-Din the data gets sent to the controller, i just want to be able to get the errors in the message bag when validation fails Commented Dec 12, 2018 at 17:30
  • Have you entered "Accept : application/json" in the header? Commented Dec 12, 2018 at 19:04
  • @rezabaghiee The answer solved this.. Commented Dec 12, 2018 at 19:13

1 Answer 1

1

something like this:

return response()->json($validator->errors(), Response::HTTP_UNPROCESSABLE_ENTITY)

EDIT:

error.response.data has the error messages

error.response.status has the error code

Sign up to request clarification or add additional context in comments.

7 Comments

same output the 422 response can be seen from the console but not the data
check the image from the question, that is the output from console.log(error) but i actually want the data i sent from the controller.
please add this console.log('errorType', typeof error); and post the message
output is object
actually you can see the available attributes of error object in the console, I think you need these two attributes: error.response.data error.response.status, log them and post what u got please
|

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.