一、优化展示:让验证码输入框和验证码图片同行显示
<!--继续编辑 \resources\views\Admin\Login\index.blade.php --> <style> .code{display: flex;}/*flex弹性布局,可以让其包含的input和img同行显示*/ .code img{border:1px solid #d5d5d5;margin-left:10px;border-radius:3px!important;cursor:pointer;} </style>
<div class="loginbox-textbox code"> <input class="form-control" placeholder="验证码" name="code" type="text"> <img src="{{captcha_src(‘mini‘)}}" alt="" onclick="this.src=‘{{captcha_src(‘mini‘)}}?‘+Math.random()"> </div> <!--注:上面行内JS代码 onclick="this.src=‘{{captcha_src(‘mini‘)}}?‘+Math.random()" 表示点击验证码图片后会随机切换成另一张不同的验证码图,不停点击可以不停的随机切换 -->
效果如下图:
二、校验功能
1,编辑 \app\Http\Requests\LoginRequest.php
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class LoginRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ "username" => ‘required‘, "password" => "required", "code" => "required|captcha" ]; } public function messages() { return [ "username.required" => "用户名不能为空", "password.required" =>"密码不能为空", "code.required" =>"验证码不能为空", "code.captcha" =>"验证码错误" ]; } }
主要是改了下图红框所示
最后效果图,提示:验证码不能为空
END
原文:https://www.cnblogs.com/pensive/p/12885038.html