合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
现在我们来进一步使用表单提交数据完成模型的对象操作,主要内容包含: - - [表单提交](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147289#u8868u5355u63D0u4EA4) - [表单验证](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147289#u8868u5355u9A8Cu8BC1) - [错误提示](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147289#u9519u8BEFu63D0u793A) - [自定义验证规则](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147289#u81EAu5B9Au4E49u9A8Cu8BC1u89C4u5219) - [控制器验证](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147289#u63A7u5236u5668u9A8Cu8BC1) ## 表单提交 首先创建一个视图模板文件 `application/index/view/user/create.html`,内容如下: ``` <pre class="calibre18"> ``` <span class="hljs-regexp"><!doctype html></span><span class="hljs-regexp"><<span class="hljs-operator">html</span>></span><span class="hljs-regexp"><<span class="hljs-operator">head</span>></span><span class="hljs-regexp"><<span class="hljs-operator">meta</span> <span class="hljs-operator">charset</span>=<span class="hljs-string">"UTF-8"</span>></span><span class="hljs-regexp"><<span class="hljs-operator">title</span>></span>创建用户<span class="hljs-regexp"></<span class="hljs-operator">title</span>></span><span class="hljs-regexp"><<span class="hljs-operator">style</span>></span><span class="css"><span class="hljs-regexp">body</span> <span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">font-family</span>:<span class="hljs-string"><span class="hljs-operator">"Microsoft Yahei"</span>,<span class="hljs-operator">"Helvetica Neue"</span>,Helvetica,Arial,sans-serif</span></span>; <span class="hljs-operator"><span class="hljs-title1">font-size</span>:<span class="hljs-string"><span class="hljs-number">16px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"><span class="hljs-number">5px</span></span></span>; }</span><span class="hljs-regexp">.form</span><span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">15px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">font-size</span>:<span class="hljs-string"> <span class="hljs-number">16px</span></span></span>; }</span><span class="hljs-regexp">.form</span> <span class="hljs-regexp">.text</span> <span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">3px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">margin</span>:<span class="hljs-string"><span class="hljs-number">2px</span> <span class="hljs-number">10px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">width</span>:<span class="hljs-string"> <span class="hljs-number">240px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">height</span>:<span class="hljs-string"> <span class="hljs-number">24px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">line-height</span>:<span class="hljs-string"> <span class="hljs-number">28px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">border</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#D4D4D4</span></span></span>; }</span><span class="hljs-regexp">.form</span> <span class="hljs-regexp">.btn</span><span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">margin</span>:<span class="hljs-string"><span class="hljs-number">6px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">6px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">width</span>:<span class="hljs-string"> <span class="hljs-number">120px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">font-size</span>:<span class="hljs-string"> <span class="hljs-number">16px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">border</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#D4D4D4</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">cursor</span>:<span class="hljs-string"> pointer</span></span>; <span class="hljs-operator"><span class="hljs-title1">background</span>:<span class="hljs-string"><span class="hljs-title">#eee</span></span></span>; }</span><span class="hljs-regexp">a</span><span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">color</span>:<span class="hljs-string"> <span class="hljs-title">#868686</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">cursor</span>:<span class="hljs-string"> pointer</span></span>; }</span><span class="hljs-operator"><span class="hljs-title1">a</span>:<span class="hljs-string">hover{ text-decoration: underline</span></span>; } <span class="hljs-regexp">h2</span><span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">color</span>:<span class="hljs-string"> <span class="hljs-title">#4288ce</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">font-weight</span>:<span class="hljs-string"> <span class="hljs-number">400</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">6px</span> <span class="hljs-number">0</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">margin</span>:<span class="hljs-string"> <span class="hljs-number">6px</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">font-size</span>:<span class="hljs-string"> <span class="hljs-number">28px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">border-bottom</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#eee</span></span></span>; }</span><span class="hljs-regexp">div</span><span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">margin</span>:<span class="hljs-string"><span class="hljs-number">8px</span></span></span>; }</span><span class="hljs-regexp">.info</span><span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">12px</span> <span class="hljs-number">0</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">border-bottom</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#eee</span></span></span>; }</span><span class="hljs-regexp">.copyright</span><span class="hljs-operator">{ <span class="hljs-operator"><span class="hljs-title1">margin-top</span>:<span class="hljs-string"> <span class="hljs-number">24px</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">padding</span>:<span class="hljs-string"> <span class="hljs-number">12px</span> <span class="hljs-number">0</span></span></span>; <span class="hljs-operator"><span class="hljs-title1">border-top</span>:<span class="hljs-string"> <span class="hljs-number">1px</span> solid <span class="hljs-title">#eee</span></span></span>; }</span></span><span class="hljs-regexp"></<span class="hljs-operator">style</span>></span><span class="hljs-regexp"></<span class="hljs-operator">head</span>></span><span class="hljs-regexp"><<span class="hljs-operator">body</span>></span><span class="hljs-regexp"><<span class="hljs-operator">h2</span>></span>创建用户<span class="hljs-regexp"></<span class="hljs-operator">h2</span>></span><span class="hljs-regexp"><<span class="hljs-operator">FORM</span> <span class="hljs-operator">method</span>=<span class="hljs-string">"post"</span> <span class="hljs-operator">class</span>=<span class="hljs-string">"form"</span> <span class="hljs-operator">action</span>=<span class="hljs-string">"{:url('index/user/add')}"</span>></span> 昵 称:<span class="hljs-regexp"><<span class="hljs-operator">INPUT</span> <span class="hljs-operator">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-operator">class</span>=<span class="hljs-string">"text"</span> <span class="hljs-operator">name</span>=<span class="hljs-string">"nickname"</span>></span><span class="hljs-regexp"><<span class="hljs-operator">br</span>/></span> 邮 箱:<span class="hljs-regexp"><<span class="hljs-operator">INPUT</span> <span class="hljs-operator">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-operator">class</span>=<span class="hljs-string">"text"</span> <span class="hljs-operator">name</span>=<span class="hljs-string">"email"</span>></span><span class="hljs-regexp"><<span class="hljs-operator">br</span>/></span> 生 日:<span class="hljs-regexp"><<span class="hljs-operator">INPUT</span> <span class="hljs-operator">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-operator">class</span>=<span class="hljs-string">"text"</span> <span class="hljs-operator">name</span>=<span class="hljs-string">"birthday"</span>></span><span class="hljs-regexp"><<span class="hljs-operator">br</span>/></span><span class="hljs-regexp"><<span class="hljs-operator">input</span> <span class="hljs-operator">type</span>=<span class="hljs-string">"hidden"</span> <span class="hljs-operator">name</span>=<span class="hljs-string">"__token__"</span> <span class="hljs-operator">value</span>=<span class="hljs-string">"{$Request.token}"</span> /></span><span class="hljs-regexp"><<span class="hljs-operator">INPUT</span> <span class="hljs-operator">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-operator">class</span>=<span class="hljs-string">"btn"</span> <span class="hljs-operator">value</span>=<span class="hljs-string">" 提交 "</span>></span><span class="hljs-regexp"></<span class="hljs-operator">FORM</span>></span><span class="hljs-regexp"><<span class="hljs-operator">div</span> <span class="hljs-operator">class</span>=<span class="hljs-string">"copyright"</span>></span><span class="hljs-regexp"><<span class="hljs-operator">a</span> <span class="hljs-operator">title</span>=<span class="hljs-string">"官方网站"</span> <span class="hljs-operator">href</span>=<span class="hljs-string">"http://www.thinkphp.cn"</span>></span>ThinkPHP<span class="hljs-regexp"></<span class="hljs-operator">a</span>></span><span class="hljs-regexp"><<span class="hljs-operator">span</span>></span>V5<span class="hljs-regexp"></<span class="hljs-operator">span</span>></span><span class="hljs-regexp"><<span class="hljs-operator">span</span>></span>{ 十年磨一剑-为API开发设计的高性能框架 }<span class="hljs-regexp"></<span class="hljs-operator">span</span>></span><span class="hljs-regexp"></<span class="hljs-operator">div</span>></span><span class="hljs-regexp"></<span class="hljs-operator">body</span>></span><span class="hljs-regexp"></<span class="hljs-operator">html</span>></span> ``` ``` User控制器增加新的操作方法`create`如下: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 创建用户数据页面</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">create</span><span class="hljs-number">()</span></span>{ <span class="hljs-keyword">return</span> view(); } ``` ``` `view`方法是系统封装的助手函数用于快速渲染模板文件,这里没有传入模板文件,则按照系统默认的解析规则会自动渲染当前操作方法对应的模板文件,也就是默认视图目录(`application/index/view`)下面的`user/create.html`文件,所以如果改成下面的方式是相同的: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 创建用户数据页面</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">create</span><span class="hljs-number">()</span></span>{ <span class="hljs-keyword">return</span> view(<span class="hljs-string">'user/create'</span>); } ``` ``` 并且修改之前的`add`方法如下: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 新增用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$user</span> = <span class="hljs-keyword">new</span> UserModel; <span class="hljs-keyword">if</span> (<span class="hljs-regexp">$user</span>->allowField(<span class="hljs-keyword">true</span>)->save(input(<span class="hljs-string">'post.'</span>))) { <span class="hljs-keyword">return</span> <span class="hljs-string">'用户[ '</span> . <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">':'</span> . <span class="hljs-regexp">$user</span>->id . <span class="hljs-string">' ]新增成功'</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-regexp">$user</span>->getError(); } } ``` ``` > #### 注意 > > - - - - - - > > 这里使用`allowField(true)`是为了避免表单令牌验证的字段被写入数据表,如果你已经在模型里面定义了field属性的话,可以不需要。 我们访问URL地址: ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/create</span> ``` ``` 页面输出如图: ![](https://img.kancloud.cn/f8/e3/f8e34e6389cb4323a94410c2d84bd574_644x364.png) 输入用户信息后,点击提交按钮: ![](https://img.kancloud.cn/02/9a/029a16213a903f640c9e306aed5b3129_654x362.png) 页面显示结果为: ``` <pre class="calibre18"> ``` 用户<span class="hljs-operator">[ 流年:30 ]</span>新增成功 ``` ``` ## 表单验证 永远不要相信用户的数据,所以现在给表单提交添加数据验证。 我们添加一个`User`验证器,如下: ``` <pre class="calibre18"> ``` <?php namespace app\index\validate; use think\<span class="hljs-operator">Validate</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword"><span class="hljs-operator">extends</span></span> <span class="hljs-title">Validate</span></span>{ <span class="hljs-comment">// 验证规则</span><span class="hljs-keyword">protected</span> $rule = [ <span class="hljs-operator">'nicknam</span>e' => <span class="hljs-operator">'require</span>|min:<span class="hljs-number">5</span>|token', <span class="hljs-operator">'emai</span>l' => <span class="hljs-operator">'require</span>|email', <span class="hljs-operator">'birthda</span>y' => <span class="hljs-operator">'dateFormat</span>:<span class="hljs-operator">Y</span>-m-d', ]; } ``` ``` `User`验证器添加了三个属性的验证规则,分别表示: - 昵称必须,而且最小长度为5 - 邮箱必须,而且必须是合法的邮件地址 - 生日可选,如果填写的话必须为 `Y-m-d`格式的日期格式 对属性可以使用多个验证规则,除非使用了`require`开头的规则,否则所有的验证都是可选的(也就是说有值才验证),多个验证之间用`|`分割,并且按照先后顺序依次进行验证,一旦某个规则验证失败,后续的规则就不会再进行验证(除非设置批量验证方式则统一返回所有的错误信息)。 更多的内置规则可以参考完全开发手册的[内置规则](http://ihavenolimitations.xyz/manual/thinkphp5/129356)一节。 如果我们的验证规则里面使用了`|`,为了避免混淆则必须用数组方式定义验证规则,验证规则定义修改如下: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">validate</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Validate</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Validate</span></span>{ <span class="hljs-comment">// 验证规则</span><span class="hljs-keyword">protected</span> <span class="hljs-regexp">$rule</span> = [ <span class="hljs-string">'nickname'</span> => [<span class="hljs-string">'require'</span>, <span class="hljs-string">'min'</span>=><span class="hljs-number">5</span>, <span class="hljs-string">'token'</span>], <span class="hljs-string">'email'</span> => [<span class="hljs-string">'require'</span>, <span class="hljs-string">'email'</span>], <span class="hljs-string">'birthday'</span> => [<span class="hljs-string">'dateFormat'</span> => <span class="hljs-string">'Y|m|d'</span>], ]; }</span> ``` ``` 然后对控制器的`add`方法则稍加修改,在`save`方法之前添加一个`validate`方法即可: ``` <pre class="calibre18"> ``` <span class="hljs-comment">// 新增用户数据</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$user</span> = <span class="hljs-keyword">new</span> UserModel; <span class="hljs-keyword">if</span> (<span class="hljs-regexp">$user</span>->allowField(<span class="hljs-keyword">true</span>)->validate(<span class="hljs-keyword">true</span>)->save(input(<span class="hljs-string">'post.'</span>))) { <span class="hljs-keyword">return</span> <span class="hljs-string">'用户[ '</span> . <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">':'</span> . <span class="hljs-regexp">$user</span>->id . <span class="hljs-string">' ]新增成功'</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-regexp">$user</span>->getError(); } } ``` ``` 当我们没有输入任何表单数据就直接提交的话,页面会输出结果: ``` <pre class="calibre18"> ``` nickname不能为空 ``` ``` 当我们输入昵称为`wo`的时候点击提交 ![](https://img.kancloud.cn/31/fb/31fb0c11efa60349efb95cbcc5d713fe_640x366.png) 页面输出结果为: ``` <pre class="calibre18"> ``` nickname长度不能小于 <span class="hljs-number">5</span> ``` ``` 当输入一个错误的邮箱格式后提交 ![](https://img.kancloud.cn/74/07/7407dbf4585df5326c778c2c63bc7f52_672x363.png) 页面提示错误信息为: ``` <pre class="calibre18"> ``` email格式不符 ``` ``` 当输入一个 1990/08/09 的生日时候 ![](https://img.kancloud.cn/57/25/5725d53d0fb53af1dfef73fe1a61b0cb_674x362.png) 页面提示的错误信息是: ``` <pre class="calibre18"> ``` birthday必须使用日期格式:Y-m<span class="hljs-operator">-d</span> ``` ``` ## 错误提示 目前为止,提示的错误信息都是系统默认的,接下来我们来定义提示信息。 首先,如果只是希望修改属性名称的话,可以直接使用下面的验证规则定义方式: ``` <pre class="calibre18"> ``` <?php namespace app\index\validate; use think\<span class="hljs-operator">Validate</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword"><span class="hljs-operator">extends</span></span> <span class="hljs-title">Validate</span></span>{ <span class="hljs-comment">// 验证规则</span><span class="hljs-keyword">protected</span> $rule = [ <span class="hljs-operator">'nickname</span>|昵称' => <span class="hljs-operator">'require</span>|min:<span class="hljs-number">5</span>', <span class="hljs-operator">'email</span>|邮箱' => <span class="hljs-operator">'require</span>|email', <span class="hljs-operator">'birthday</span>|生日' => <span class="hljs-operator">'dateFormat</span>:<span class="hljs-operator">Y</span>-m-d', ]; } ``` ``` 现在我们提交一个错误的邮箱, ![](https://img.kancloud.cn/2d/67/2d67370c41637ecf2209ff0982e5df10_626x362.png) 提示的错误信息为: ``` <pre class="calibre18"> ``` 邮箱格式不符 ``` ``` 输入错误的生日格式的时候,提示的错误信息为: ``` <pre class="calibre18"> ``` 生日必须使用日期格式:Y-m<span class="hljs-operator">-d</span> ``` ``` 如果希望完整定义错误提示信息的话,可以使用: ``` <pre class="calibre18"> ``` <?php namespace app\index\validate; use think\<span class="hljs-operator">Validate</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword"><span class="hljs-operator">extends</span></span> <span class="hljs-title">Validate</span></span>{ <span class="hljs-comment">// 验证规则</span><span class="hljs-keyword">protected</span> $rule = [ [<span class="hljs-operator">'nicknam</span>e', <span class="hljs-operator">'require</span>|min:<span class="hljs-number">5</span>', '昵称必须|昵称不能短于<span class="hljs-number">5</span>个字符'], [<span class="hljs-operator">'emai</span>l', <span class="hljs-operator">'emai</span>l', '邮箱格式错误'], [<span class="hljs-operator">'birthda</span>y', <span class="hljs-operator">'dateFormat</span>:<span class="hljs-operator">Y</span>-m-d', '生日格式错误'], ]; } ``` ``` 现在我们提交一个错误的邮箱,提示的错误信息为: ``` <pre class="calibre18"> ``` 邮箱格式错误 ``` ``` 提交一个错误的生日格式后,提示的错误信息变成: ``` <pre class="calibre18"> ``` 生日格式错误 ``` ``` 系统提供了丰富的内置验证规则,具体可以参考完全开发手册。 ## 自定义验证规则 系统的验证规则可以满足大部分的验证场景,但有时候我们也需要自定义特殊的验证规则,例如我们需要验证邮箱必须为`thinkphp.cn`域名的话,可以在`User`验证器中添加验证规则如下: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">validate</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Validate</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Validate</span></span>{ <span class="hljs-comment">// 验证规则</span><span class="hljs-keyword">protected</span> <span class="hljs-regexp">$rule</span> = [ [<span class="hljs-string">'nickname'</span>, <span class="hljs-string">'require|min:5'</span>, <span class="hljs-string">'昵称必须|昵称不能短于5个字符'</span>], [<span class="hljs-string">'email'</span>, <span class="hljs-string">'checkMail:thinkphp.cn'</span>, <span class="hljs-string">'邮箱格式错误'</span>], [<span class="hljs-string">'birthday'</span>, <span class="hljs-string">'dateFormat:Y-m-d'</span>, <span class="hljs-string">'生日格式错误'</span>], ]; <span class="hljs-comment">// 验证邮箱格式 是否符合指定的域名</span><span class="hljs-keyword">protected</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkMail</span><span class="hljs-number">(<span class="hljs-regexp">$value</span>, <span class="hljs-regexp">$rule</span>)</span></span>{ <span class="hljs-keyword">return</span> <span class="hljs-number">1</span> === preg_match(<span class="hljs-string">'/^\w+([-+.]\w+)*@'</span> . <span class="hljs-regexp">$rule</span> . <span class="hljs-string">'$/'</span>, <span class="hljs-regexp">$value</span>); } }</span> ``` ``` 自定义验证规则也支持返回动态的错误信息,只需要在验证方法里面返回错误信息字符串即可,例如: ``` <pre class="calibre18"> ``` <span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">validate</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Validate</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Validate</span></span>{ <span class="hljs-comment">// 验证规则</span><span class="hljs-keyword">protected</span> <span class="hljs-regexp">$rule</span> = [ [<span class="hljs-string">'nickname'</span>, <span class="hljs-string">'require|min:5'</span>, <span class="hljs-string">'昵称必须|昵称不能短于5个字符'</span>], [<span class="hljs-string">'email'</span>, <span class="hljs-string">'checkMail:thinkphp.cn'</span>, <span class="hljs-string">'邮箱格式错误'</span>], [<span class="hljs-string">'birthday'</span>, <span class="hljs-string">'dateFormat:Y-m-d'</span>, <span class="hljs-string">'生日格式错误'</span>], ]; <span class="hljs-comment">// 验证邮箱格式 是否符合指定的域名</span><span class="hljs-keyword">protected</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkMail</span><span class="hljs-number">(<span class="hljs-regexp">$value</span>, <span class="hljs-regexp">$rule</span>)</span></span>{ <span class="hljs-regexp">$result</span> = preg_match(<span class="hljs-string">'/^\w+([-+.]\w+)*@'</span> . <span class="hljs-regexp">$rule</span> . <span class="hljs-string">'$/'</span>, <span class="hljs-regexp">$value</span>); <span class="hljs-keyword">if</span> (!<span class="hljs-regexp">$result</span>) { <span class="hljs-keyword">return</span> <span class="hljs-string">'邮箱只能是'</span> . <span class="hljs-regexp">$rule</span> . <span class="hljs-string">'域名'</span>; } <span class="hljs-keyword">else</span> { <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>; } } }</span> ``` ``` 如果输入了一个不是`thinkphp.cn`域名的邮箱地址,会提示如下错误信息: ``` <pre class="calibre18"> ``` 邮箱只能是<span class="hljs-regexp">thinkphp</span><span class="hljs-regexp">.cn</span>域名 ``` ``` ## 控制器验证 前面我们讲了在模型中使用验证器进行数据验证的方法,下面来讲下如何在控制器中进行数据验证。 验证器类的定义不变,现在修改下控制器类: ``` <pre class="calibre18"> ``` <span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">model</span>\<span class="hljs-title">User</span> <span class="hljs-title">as</span> <span class="hljs-title">UserModel</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Controller</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</span></span>{ <span class="hljs-comment">// 创建用户数据页面</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">create</span><span class="hljs-number">()</span></span>{ <span class="hljs-keyword">return</span> view(); } <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$data</span> = input(<span class="hljs-string">'post.'</span>); <span class="hljs-comment">// 数据验证</span><span class="hljs-regexp">$result</span> = <span class="hljs-regexp">$this</span>->validate(<span class="hljs-regexp">$data</span>,<span class="hljs-string">'User'</span>); <span class="hljs-keyword">if</span> (<span class="hljs-keyword">true</span> !== <span class="hljs-regexp">$result</span>) { <span class="hljs-keyword">return</span> <span class="hljs-regexp">$result</span>; } <span class="hljs-regexp">$user</span> = <span class="hljs-keyword">new</span> UserModel; <span class="hljs-comment">// 数据保存</span><span class="hljs-regexp">$user</span>->allowField(<span class="hljs-keyword">true</span>)->save(<span class="hljs-regexp">$data</span>); <span class="hljs-keyword">return</span> <span class="hljs-string">'用户[ '</span> . <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">':'</span> . <span class="hljs-regexp">$user</span>->id . <span class="hljs-string">' ]新增成功'</span>; } } ``` ``` 然后访问 ``` <pre class="calibre18"> ``` <span class="hljs-string">http:</span> <span class="hljs-comment">//tp5.com/user/create</span> ``` ``` 当输入一个错误的邮箱格式后提交 ![](https://img.kancloud.cn/74/07/7407dbf4585df5326c778c2c63bc7f52_672x363.png) 页面提示错误信息为: ``` <pre class="calibre18"> ``` email格式不符 ``` ``` 如果有一些个别的验证没有在验证器里面定义,也可以使用静态方法单独处理,例如下面对birthday字段单独验证是否是一个有效的日期格式: ``` <pre class="calibre42"> ``` <span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">model</span>\<span class="hljs-title">User</span> <span class="hljs-title">as</span> <span class="hljs-title">UserModel</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Controller</span>; <span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Validate</span>; <span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</span></span>{ <span class="hljs-comment">// 创建用户数据页面</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">create</span><span class="hljs-number">()</span></span>{ <span class="hljs-keyword">return</span> view(); } <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span><span class="hljs-number">()</span></span>{ <span class="hljs-regexp">$data</span> = input(<span class="hljs-string">'post.'</span>); <span class="hljs-comment">// 验证birthday是否有效的日期</span><span class="hljs-regexp">$check</span> = Validate::is(<span class="hljs-regexp">$data</span>[<span class="hljs-string">'birthday'</span>],<span class="hljs-string">'date'</span>); <span class="hljs-keyword">if</span> (<span class="hljs-keyword">false</span> === <span class="hljs-regexp">$check</span>) { <span class="hljs-keyword">return</span> <span class="hljs-string">'birthday日期格式非法'</span>; } <span class="hljs-regexp">$user</span> = <span class="hljs-keyword">new</span> UserModel; <span class="hljs-comment">// 数据保存</span><span class="hljs-regexp">$user</span>->save(<span class="hljs-regexp">$data</span>); <span class="hljs-keyword">return</span> <span class="hljs-string">'用户[ '</span> . <span class="hljs-regexp">$user</span>->nickname . <span class="hljs-string">':'</span> . <span class="hljs-regexp">$user</span>->id . <span class="hljs-string">' ]新增成功'</span>; } } ``` ```