0

I'm working with Laravel and this is my RegisterController Class based on API:

class RegisterController extends AuthController
{
    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string',
            'email' => 'required|email|unique:users',
            'password' => 'required|string|min:6|max:13|same:password_confirmation',
        ]);

        $errors = [];
        if ($validator->fails()) {
            foreach($validator->errors()->all() as $err){
                array_push($errors, $err);
            }

            return view('auth.index', compact('errors'));
        }else{
            $user = User::create([
                'name' => $request->name,
                'email' => $request->email,
                'password' => bcrypt($request->password),
            ]);

            Auth::login($user);

            $token = $user->createToken('API Token')->accessToken;

            return response()->json(['token' => $token], 201);
        }
    }
}

It works fine but as you see I tried showing validation errors by returning the view and compacted the errors array to it:

return view('auth.index', compact('errors'));

This is wrong, however, I don't know how to capture the response json for the error showing.

So if you know how to do this, please let me know.

Thanks in advance.

5
  • do you want json validation response ? if not then share blade file Commented Dec 18, 2023 at 8:19
  • since this is api, you might be using ajax./axios or anything equivalent. did you try to console.log the response? Check the contents of response Commented Dec 18, 2023 at 8:22
  • 2
    First of all, why are you foreaching the errors and putting them in an other array? And you are returning a view, not a json response. It is strange that you return a json on success and a view on error. Commented Dec 18, 2023 at 8:24
  • @JohnLobo I want to use this for sending api requests and also use that as action of my form in blade, Commented Dec 18, 2023 at 8:44
  • Directly use ->validate instead of make... read the documentation... Commented Dec 18, 2023 at 9:41

1 Answer 1

3

Automatic Redirection

If you would like to create a validator instance manually but still take advantage of the automatic redirection offered by the HTTP request's validate method, you may call the validate method on an existing validator instance. If validation fails, the user will automatically be redirected or, in the case of an XHR request, a JSON response will be returned:

Validator::make($request->all(), [
            'name' => 'required|string',
            'email' => 'required|email|unique:users',
            'password' => 'required|string|min:6|max:13|same:password_confirmation',
        ])->validate();

or

$request->validate([
            'name' => 'required|string',
            'email' => 'required|email|unique:users',
            'password' => 'required|string|min:6|max:13|same:password_confirmation',
        ])

So if the request contains header content-type:application/json then it automatically converts to JSON.In Blade you can do the following

@error('name')
    <div class="alert alert-danger">{{ $message }}</div>
@enderror

or

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
Sign up to request clarification or add additional context in comments.

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.