laravel表单验证

详细地址:https://segmentfault.com/a/1190000003718819


效果展示:

       



代码 实现:


后台:

use Validator;

 public function login()
    {
        if($input = Input::all()){
            //验证提交的数据
            $rules = [
                'user_name'=>'required|between:4,20',
                'user_pwd'=>'required|between:6,20',
                'code'=>'required|between:4,4'
            ];
            $message = [
                'user_name.required'=>'账号不能为空!',
                'user_name.between'=>'账号必须在6-20位之间!',
                'user_pwd.required'=>'密码不能为空!',
                'user_pwd.between'=>'密码必须在6-20位之间!',
                'code.required'=>'验证码不能为空!',
                'code.between'=>'验证码必须为4位!',
            ];
            $validator = Validator::make($input,$rules,$message);
            //表单验证
            if($validator->passes()){
            //验证验证码
            if($_SESSION['code']!=strtoupper($input['code'])){
                return back()->with('msg','验证码错误!');
            }
            //用户验证
            $user = DB::table('user')->select

('user_id','user_encrypt','user_pwd')->where('user_name',$input['user_name'])-

>first();
            if(!$user){
                return back()->with('msg','用户不存在!');
            }else{
                if(md5(md5($input['user_pwd']).$user->user_encrypt)!=$user-

>user_pwd){
                    return back()->with('msg','密码错误!');
                }
                session(['user_id'=>$user->user_id]);
                return redirect('index');
               }
            }else{
                return back()->withErrors($validator);
            }
        }else{
            return view('admin.login');
        }

    }

前台:

<div class="middle-box text-center loginscreen  animated fadeInDown">
        <div>
            <div>

                <h1 class="logo-name">h</h1>

            </div>
            <h3>欢迎使用 hAdmin</h3>
            
            <form class="m-t" role="form" action="{{URL('/login')}}" method="post">
               @if(count($errors)>0)
                            @if(is_object($errors))
                                @foreach($errors->all() as $error)
                                <p style="color:red">{{$error}}</p>
                                @endforeach
                            @else
                            <p style="color:red">{{$errors}}</p>
                        @endif
                    @endif
                    @if(session('msg'))
                        <p style="color:red">{{session('msg')}}</p>
                    @endif
                <div class="form-group">
                    <input type="email" class="form-control" placeholder="用户名" required="" name="email">
                <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" placeholder="密码" required="" name="pwd">
                </div>
                <button type="submit" class="btn btn-primary block full-width m-b">登 录</button>


                <p class="text-muted text-center"> <a href="login.html#"><small>忘记密码了?</small></a> | <a href="{{URL('/register')}}">注册一个新账号</a>
                </p>

            </form>
        </div>
    </div>

$validator->fails()  验证都符合规则返回false 不符合规则返回true




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值