Home > php教程 > php手册 > body text

使用 callback 自动验证字段唯一

WBOY
Release: 2016-06-07 11:41:55
Original
1272 people have browsed it

使用 callback 使add 和 update 使用一个自动验证条件

ThinkPHP 3.x 之后的版本已经支持更新时自动验证唯一.
所以此代码失效, 感谢微博 @小杰_Dino 告知.

=====2014-05-12 之前的内容====
让检查唯一合并为一个验证条件.
如果你有更好的, 欢迎一起讨论.public $_validate = array(<br>         array('name', 'chkUniName', '英文代码已经存在.', self::EXISTS_VALIDATE, 'callback'),<br> <br> 放在 CommonModel 中.<br> //检查唯一<br>     public function chkUni($field){<br>         if(empty($field)){<br>             return false;<br>         }<br>         $map = array();<br>         $pk = $this->getPk();<br>         //如果有主键传入, 说明是编辑, 加入排除自己的条件.<br>         if(!empty($_REQUEST[$pk])){<br>            $map[$pk] = array('neq', intval($_REQUEST[$pk]));<br>         }<br>         $map[$field] = array('eq', trim($_REQUEST[$field]));<br>         if(null === $this->where($map)->find()){<br>             return true;<br>         }else{<br>             return false;<br>         }<br>     }<br> <br>     //检查 name 是否存在<br>     protected function chkUniName(){<br>         return $this->chkUni('name');<br>     }

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!