1.前后端不分离 (form表单提交)

控制器定义验证规则

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator; class LoginController
{
/**
* @param Request $request
* 登录
*/
public function login(Request $request)
{
$post['name'] = $request->input('name');
$post['password'] = $request->input('password'); $messages = [
'name.required' => '请输入账号',
'name.max' => '账号错误',
'password.required' => '请输入密码',
'password.max' => '密码错误',
]; Validator::make($post, [
'name' => 'required|max:20',
'password' => 'required|max:30',
],$messages)->validate(); }
}

前端

@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror

2.前后端分离

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator; class LoginController
{
/**
* @param Request $request
* 登录
*/
public function login(Request $request)
{
$post['name'] = $request->input('name');
$post['password'] = $request->input('password'); $messages = [
'name.required' => '请输入账号',
'name.max' => '账号错误',
'password.required' => '请输入密码',
'password.max' => '密码错误',
]; $validator = Validator::make($post, [
'name' => 'required|max:20',
'password' => 'required|max:30',
],$messages); if ($validator->fails()){
//返回错误信息
//{
// "name": [
// "账号错误"
// ],
// "password": [
// "密码错误"
// ]
//}
var_dump($validator->errors()->messages());
} }
}

laravel 验证器使用的更多相关文章

  1. laravel验证器例子

    直接贴测试代码 Route::get('/', function() { $name = "rico"; $validateData = array('name1' => $ ...

  2. 关于脱离laravel框架使用Illuminate/Validation验证器

    1.关于Illuminate/Validation验证器 Validation 类用于验证数据以及获取错误消息. github地址:github.com/illuminate/validation 文 ...

  3. 9、 Struts2验证(声明式验证、自定义验证器)

    1. 什么是Struts2 验证器 一个健壮的 web 应用程序必须确保用户输入是合法.有效的. Struts2 的输入验证 基于 XWork Validation Framework 的声明式验证: ...

  4. linux上使用google身份验证器(简版)

    系统:centos6.6 下载google身份验证包google-authenticator-master(其实只是一个.zip文件,在windwos下解压,然后传进linux) #cd /data/ ...

  5. vue-validator(vue验证器)

    官方文档:http://vuejs.github.io/vue-validator/zh-cn/index.html github项目地址:https://github.com/vuejs/vue-v ...

  6. 原生JS 表单提交验证器

    转载:http://www.cnblogs.com/sicd/p/4613628.html 一.前言 最近在开发一个新项目,需要做登陆等一系列的表单提交页面.在经过“缜密”的讨论后,我们决定 不用外部 ...

  7. yii框架中验证器声明一组内置验证器可以使用短名称引用

    1.内置验证器的短名称分别有: boolean: yii\validators\BooleanValidator captcha: yii\captcha\CaptchaValidator compa ...

  8. 通过Google身份验证器加强Linux帐户安全

    下载Google的身份验证模块: # wget https://google-authenticator.googlecode.com/files/libpam-google-authenticato ...

  9. 谷歌身份验证器加强Linux帐户安全

    下载 Google的身份验证模块 # wget https://google-authenticator.googlecode.com/files/libpam-google-authenticato ...

随机推荐

  1. 第三十六章 Linux常用性能检测的指令

    作为一个Linux运维人员,介绍下常用的性能检测指令! 一.uptime 命令返回的信息: 19:08:17              //系统当前时间 up 127 days,  3:00     ...

  2. frp 内网穿透远程桌面(Windows 10)配置

    一.服务端配置 服务端需要公网环境,一般用一台云服务器就行了,我选择的是 Linux 服务器,Windows 服务器也是可以的. 下载 frp: wget https://github.com/fat ...

  3. Linux命令的内部命令执行

    一个命令可能既是内部命令也是外部命令 因为内部命令优先级高,先执行内部命令 [04:21:44 root@C8[ ~]#type -a echo echo is a shell builtin ech ...

  4. Vue基础(2)

    fetch与axios请求数据 fetch基本语法: fetch(url,{parmas}).then(res=> res.json()  //返回promise对象 ).then(data=& ...

  5. redis限频

    做法 使用redis的lua脚本功能来限频 在redis中定时刷新系统时间来作为一个全局的时钟 限频脚本: /** * 获取令牌的lua脚本 */ public final static String ...

  6. Oracle一些常用操作语句

    --创建oracle登录用户 create user CHECKDATAUSER   identified by "bsoft"   default tablespace PBPG ...

  7. Windows Server 2019 在桌面图标

    直接按Win(键盘上的微软徽标键)+R,输入: rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0 回车 rundll32.exe shell32. ...

  8. 撸个反向代理,激活JRebel~

    持续原创输出,点击上方蓝字关注我 目录 前言 本地反向代理 服务器反向代理[个人推荐] IDEA安装JRebel并激活 服务器安装JRebel并激活 总结 前言 热部署相信大家都听说过,比如Sprin ...

  9. OpenCascade拓扑对象之:Face的方向、参数域和曲面间的关系

    @font-face { font-family: "Times New Roman" } @font-face { font-family: "宋体" } @ ...

  10. 如何在Debian上安装和使用PHP Composer

    1.条件 shell使用sudo权限访问正在运行的debian系统. 必须安装和配置5.3或更高版本的PHP. 2.在Debian上安装Composer 可以通过运行以下命令从getcomposer. ...