后端自动验证与错误提示

 验证错误提示:控制器里调取模型中的方法

$this->error($cate->getError());

模型里CateModel.class.php:

<?php
    namespace Admin\Model;
    use Think\Model;
    class CateModel extends Model{
        protected $_validate = array(
             array('catename','require','不能为空',1,'regesx',3), //默认情况下用正则进行验证
          );
    }
?>

控制器里CateController.class.php:

public function add(){
            if(IS_POST){
                //处理提交
                $data = I('post.');
                $cate = D('cate');
                if ($cate->create($data)) {
                    if($cate->add()){
                        $this->success('添加成功',U('lst'),3);
                    }else{
                        $this->error('添加失败');
                    }
                }else{
                    $this->error($cate->getError());
                }
            }else{
                $this->display();
            }
        }

 此时后台便可以自定义验证提示

例如验证唯一性:

 array('catename','','该名称已经存在!',0,'unique',3), // 在新增的时候验证name字段是否唯一

在手册里都有

 

 

 

 

 

 

 

.

posted @ 2018-04-06 11:09  剑仙6  阅读(156)  评论(0编辑  收藏  举报
欢迎访问个人网站www.qingchun.在线