附加自定义错误的错误

时间:2017-01-28 11:44:41

标签: laravel laravel-validation

如何附加$errors->all()数组?像自定义错误消息...在互联网上搜索但我没有找到任何东西。

在我看来:

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

我的控制器:

<?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');
    }
}

3 个答案:

答案 0 :(得分:1)

首先使用Validator facade进行验证然后

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

答案 1 :(得分:0)

自定义错误消息:

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

如果你想要刷新旧输入:

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

答案 2 :(得分:0)

你应该写这个

<?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');
    }
}