0

How can i append the $errors->all() array? Like a custom error message... Searched on the internet but i did not find anything.

In my view:

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

My controller:

<?php

namespace App\Http\Controllers\Admin;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function login(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email|exists:users,email',
            'password' => 'required',
        ]);

        if ($user = Auth::attempt([
            'email' => $request->email,
            'password' => $request->password
        ])) {
            /** @var $user User */
            if ($user->isAdmin()) {
                return redirect()
                    ->route('admin.dashboard');
            }
        }

        // wrong email or password

        return redirect('/admin/login');
    }
}

4 Answers 4

1

First use Validator facade for validation and then

  $inputs = $request->all();
  $rules = array(
      'email' => 'required|email|exists:users,email',
      'password' => 'required',
  );
  $messages = array();
  $validator = Validator::make($inputs,$rules,$messages);
  $validator->after(function($validator) {
    //do some stuff
    $validator->errors()->add('error', 'error messgae');
  })
Sign up to request clarification or add additional context in comments.

1 Comment

...->after is overwriting old rules, its not appending it's overwriting rules to that field. Not worked for me. Thanks.
0

custom error message :

 return redirect('/admin/login')->withErrors([
   'error_key' => 'error_message'
]);

and if you want flush old inputs :

return redirect('/admin/login')->withInput()->withErrors([
     'error_key' => 'error_message'
]);

Comments

0

You should write this

<?php
namespace App\Http\Controllers\Admin;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function login(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email|exists:users,email',
            'password' => 'required',
        ]);

        if ($user = Auth::attempt([
            'email' => $request->email,
            'password' => $request->password
        ])) {
            /** @var $user User */
            if ($user->isAdmin()) {
                return redirect()
                    ->route('admin.dashboard');
            }else {
              return redirect('/admin/login')->>with('errors', 'Sorry!!! something went wrong. please try again.');
          }
        }else {
            return redirect('/admin/login')->>with('errors', 'Sorry!!! something went wrong. please try again.');
        }

        // wrong email or password

        return redirect('/admin/login');
    }
}

Comments

0

You can add to errors while using Validator Facade for validation:

$validator = Validator::make(
    [
        'email' => $request->email,
        'password' => $request->password
    ],
    [
        'email' => 'required|email|exists:users,email',
        'password' => 'required',
    ]
);
$validator->errors()->add('custom-error', 'my custom error');

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.