Home > php教程 > PHP源码 > body text

ThinkPHP 使用 callback 自动验证字段唯一

PHP中文网
Release: 2016-05-25 16:59:12
Original
1363 people have browsed it

跳至

public $_validate = array(
        array('name', 'chkUniName', '英文代码已经存在.', self::EXISTS_VALIDATE, 'callback'),
 放在 CommonModel 中.
 //检查唯一
    public function chkUni($field){
        if(empty($field)){
            return false;
        }
        $map = array();
        $pk = $this->getPk();
        //如果有主键传入, 说明是编辑, 加入排除自己的条件.
        if(!empty($_REQUEST[$pk])){
           $map[$pk] = array('neq', intval($_REQUEST[$pk]));
        }
        $map[$field] = array('eq', trim($_REQUEST[$field]));
        if(null === $this->where($map)->find()){
            return true;
        }else{
            return false;
        }
    }
    //检查 name 是否存在
    protected function chkUniName(){
        return $this->chkUni('name');
    }
Copy after login

                   

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!