如何使用Laravel 5提供内联错误反馈

时间:2019-07-09 12:06:42

标签: php validation laravel-5

我正在使用Laravel 5构建表单,并且我希望错误消息出现在身份验证页面的字段旁边。 这是我的控制器

<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="/css/bootstrap.css" rel="stylesheet">
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/bootstrap-theme.css" rel="stylesheet">
    <link href="/css/all.css" rel="stylesheet">
    <link href="/css/all.min.css" rel="stylesheet">
    <link href="/css/Style.css" rel="stylesheet">

    <script src="/js/jquery-2.2.4.min.js"></script>
    <script src="/js/bootstrap.js"></script>
    <script src="/js/all.js"></script>
    <script src="/js/all.min.js"></script>


    <title>LOGIN</title>
    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
</head>
<body>
<form id="form1" action="/loginme" method="post">
    <div >
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <div class="login_form">
                <div class="inputWithIcon">
                    <i class="fas fa-user-tie" ></i>
                    <input type="text" placeholder="Username" class="form-control" name="username" />
                </div>
                <div class="inputWithIcon">
                    <i class="fas fa-unlock-alt" ></i>
                    <input type="password" placeholder="Password" class="form-control" name="password" />
                </div>
                <br />
<label class="invalid-feedback" id="login_error"></label>
                <input type="submit" id="submitBtn" class="btn btn-secondary  btn-block login-submit-btn"  value="Login"></input>
            </div>
    </div>
</form>
</body>
</html>

我想通过单击按钮“ submitBtn”显示警报消息 这是我的控制器

enter image description here

2 个答案:

答案 0 :(得分:2)

您需要在控制器中使用验证。

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use DB;

class LoginController extends Controller {

    public function login(Request $request){

        $this->validate($request, [
            'username' => 'required',
            'password' => 'required'
        ]);

        $username = $request->input('username');
        $password = $request->input('password');
        $checklogin = DB::table('users')->where(['Username' => $username, 'Password' => $password])->get();
        if(count($checklogin)){
            echo "login successful";
        }else{
            return view('login');
        }
    }
}

然后在刀片中使用如下所示的错误提示:

<input type="text" placeholder="Username" class="form-control" name="username" />
@if ($errors->has('username'))
  <span class="error-text text-danger">{{ $errors->first('username') }}</span>
@endif
<input type="password" placeholder="Password" class="form-control" name="password" />
@if ($errors->has('password'))
  <span class="error-text text-danger">{{ $errors->first('password') }}</span>
@endif

答案 1 :(得分:0)

可以写

<input type="text" placeholder="Username" class="form-control" name="username" />
@if ($errors->has('username')) <span class="error-text text-danger">{{ $errors->first('username') }}</span> @endif

<input type="password" placeholder="Password" class="form-control" name="password" />
@if ($errors->has('password')) <span class="error-text text-danger">{{ $errors->first('password') }}</span> @endif