首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Laravel 5提供内联错误反馈?

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

Stack Overflow用户
提问于 2019-07-09 12:06:42
回答 2查看 76关注 0票数 0

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

代码语言:javascript
复制
<!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“按钮来显示警告信息,这是我的控制器。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-09 14:57:25

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

代码语言:javascript
复制
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');
        }
    }
}

然后,在您的刀片中,您可以使用如下所述的正则错误:

代码语言:javascript
复制
<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
票数 2
EN

Stack Overflow用户

发布于 2019-07-09 12:55:53

哟会写

代码语言:javascript
复制
<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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56952235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档