码迷,mamicode.com
首页 > 其他好文 > 详细

后端自动验证与错误提示

时间:2018-04-06 12:25:01      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:col   cte   class   cat   pre   mod   log   func   AC   

技术分享图片

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

$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字段是否唯一

在手册里都有

 

 

 

 

 

 

 

.

后端自动验证与错误提示

标签:col   cte   class   cat   pre   mod   log   func   AC   

原文地址:https://www.cnblogs.com/jianxian/p/8727168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!