1. 程式人生 > >laraval新增驗證碼驗證

laraval新增驗證碼驗證

安裝:composer require "mews/captcha:~2.0"

執行:php artisan vendor:publish 生成配置檔案config/captcha


前端
 <img class="thumbnail captcha" src="{{ captcha_src('flat') }}" onclick="this.src='/captcha/flat?'+Math.random()" title="點選圖片重新獲取驗證碼">
後端Auth/registerController.php
protected function validator(array $data)
    {
        $rules = [
            'vricode'=>'required|string|min:6|max:6|captcha',
            'smscode'=>'required|string|min:6|max:6',
            'phone' => 'regex:/^1[34578][0-9]{9}$/',
            'password' => 'required|string|min:6|confirmed',
        ];
        $messages = [
            'vricode.captcha'=>'請輸入正確的驗證碼',
            'vricode'=>':attribute的長度不正確',
            'smscode'=>':attribute的長度不正確',
            'phone' => ':attribute格式不正確',
            'password' => ':attribute不一致',
        ];
        $attributes = [
            'vricode'=>'驗證碼',
            'smscode'=>'手機驗證碼',
            'phone' => '手機號碼',
            'password' => '密碼',
        ];
        return Validator::make($data, $rules, $messages, $attributes);
    }