1. 验证
对于验证用户输入的数据 ,主要有两种方式 ,手动验证或在控制器中使用 validate() 方法
2. 使用 ValidatesRequests 的 validate() 方法
首先定义一个控制器 ,在控制器中使用 validate() 方法来验证用户输入的数据
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class RecipesController extends Controller
{
public function create()
{
return view('recipes.create');
}
public function store(Request $request)
{
// 使用 validate() 验证 $request 传入的数据
$this->validate($request, [
// 这里是验证的字段和验证规则 ,验证规则可以使用 "管道" 语法或 "数组" 语法 ,这里用的是 "管道" 语法
'title' => 'required|unique:recipse|max:125',
'body' => 'required'
], [
'title.required' => 'title 被你吃了?'
]);
// 如果 validaet() 方法的验证没有通过 ,则会抛出 ValidationException 异常
// 如果请求不是 Ajax 请求 ,ValidationException 异常会重定向到上一页 ,包括用户输入和验证错误信息 ,在视图中可以通过 $errors 访问这些信息
// 如果请求是 Ajax 请求 ,ValidationException 异常会创建一个包含验证错误的 JSON 响应
}
}
然后定义用来发起请求的视图文件
<form action="/recipse" method="post">
<?= csrf_field() ?>
<p>Title :<input type="text" name="title" /></p>
<p>Body :<input type="text" name="body" /></p>
<button type="submit">submit</button>
</form>
<!-- Laravel 任何通过路由显示的视图都可以使用 $errors 变量 ,放心使用不需要 isset() 判断 -->
@if ($errors->any())
<ul id="errors">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
<!-- 如果 $errors 有内容 ,$errors->all() 内容大概是下面这样 -->
<!-- Array ( [0] => The title field is required. [1] => The body field is required. ) -->
上面的例子有些人可能会有一些疑惑 ,验证错误信息是如何传递到页面的 ,可以参考 Laravel 的 "消息包" 和重定向的 withErrors() 方法
3. 验证规则
3.1 嵌套属性验证和数组语法
验证规则有两种语法 ,"管道" 语法和 "数组" 语法 ,上面用的是 "管道" 语法
验证规则还可以验证表单提交的嵌套属性 ,比如这样的 <input type="text" name="user[name]" />
$this->validate($request, [
'user.name' => array('required'),
'user.email' => array('required', 'email')
]);
3.2 验证规则 :字段是否必须存在
| 验证规则 | 说明 | 示例 |
|---|---|---|
| required | 验证字段必须存在 ,且不能为空 | - |
| present | 验证字段必须存在 ,但可以为空 | - |
| required_if:fieldName,equalsToThisValue | 如果 fieldName 的值是 equalsToThisValue ,则验证字段必须存在 | - |
| required_unless:fieldName,equalsToThisValue | 如果 fieldName 的值是 equalsToThisValue ,则验证字段可以不存在 | - |
3.3 验证规则 :字段只能是特定字符
| 验证规则 | 说明 | 示例 |
|---|---|---|
| alpha | 验证字段只能是字母 | - |
| alpha_dash | 验证字段只能是字母 、数字 、破折号 、下划线 | - |
| alpha_num | 验证字段只能是字母和数字 | - |
| numeric | 只能是数字 | - |
| integer | 只能是整数 | - |
3.4 验证规则 :字段只能是特定的模式
| 验证规则 | 说明 | 示例 |
|---|---|---|
| ip | 只能是 ip 地址 | - |
| ipv4 | 只能是 ipv4 地址 | - |
| ipv6 | 只能是 ipv6 地址 | - |
| 只能是 email 地址 | - | |
| active_url | 只能是合法的 url ,基于 PHP 的 checkdnsrr() 函数 | - |
3.5 验证规则 :日期
| 验证规则 | 说明 | 示例 |
|---|---|---|
| date | 可以通过 strtotime() 验证的日期字符串 | - |
| after:date | 验证字段必须是 date 之后的日期 | - |
| after_or_equal:date | 验证字段不能是 date 之前的日期 | - |
| before:date | 验证字段必须是 date 之前的日期 | - |
| before_or_equal:date | 验证字段不能是 date之后的日期 | - |
3.6 验证规则 :数字
| 验证规则 | 说明 | 示例 |
|---|---|---|
| between:min,max | 验证字段必须在 min 和 max 之间 | - |
| min:num | 验证字段不能小于 num | - |
| max:num | 验证字段不能大于 num | - |
| size:num | 对整数进行等职测试 ,对字符串进行长度测试 、对数组则为 count() 值 ,文件大小则以 KB 为单位 | - |
3.7 验证规则 :图像
| 验证规则 | 说明 | 示例 |
|---|---|---|
| dimensions:min_width=XXX | 对图像尺寸进行验证 ,也可以用 and 或 or 组合 max_width 、min_height 、max_height 、width 、height 、ratio | - |
3.8 验证规则 :数据库
| 验证规则 | 说明 | 示例 |
|---|---|---|
| exists:tableName,column | 验证字段必须是 tableName 表中 column 列已存在的值 | - |
| unique:tableName,column | 验证字段必须是 tableName 表中 column 列不存在的值 | - |
4. 手动验证
有时候出于某些原因 ,上面介绍的工作的流程可能并不适合 ,可以手动创建 Validator 实例来进行验证
// 验证规则
$rules = array(
'title' => 'required|unique:recipse|max:125',
'body' => 'required'
);
// 验证规则匹配的提示信息 ,默认的提示信息都是英文的
$message = array(
'title.required' => 'title 被你吃了?'
);
// 创建一个 Validator 实例 ,将用户输入作为第一个参数 ,验证规则作为第二个参数 ,第三个参数可以自定义提示消息
$validator = Validater::make($request->all(), $rules, $message);
// Validator 有一个 fails() 方法 ,用来进行验证 ,验证失败就会返回 true
if ($validator->fails()) {
return recircet('recipes/create')
// 可以将 $validator 传递给 withErrors() ,以便在重定向的视图中通过 $errors 变量访问验证失败的错误信息
->withErrors($validator)
->withInput();
}
// 验证通过 ,继续