标签:
静态方式:在模型类里面通过$_validate属性定义验证规则。
InfoModel.class.php
<?php namespace Home\Model; use Think\Model; class InfoModel extends Model { protected $_validate=array( //array(验证的字段,验证规则,错误提示,[验证条件,附加规则,验证时间]) //非空验证 regex array("Code","require","代号不能为空",0,"regex",3), //验证字段是否唯一 unique array("Name","","姓名不能重复",0,"unique",3), //验证两次输入是否一致 confirm array("Queren","Sex","两次输入不一致",0,"confirm",3), //验证输入的内容是否与正则的定义的值一致 equal array("Queren","123","输入的内容不正确",0,"equal",3), //范围验证 between array("Queren","18,30","输入年龄要在18~30岁之间",0,"between",3), //邮箱 email array("Queren","email","邮箱格式输入有误",0,"regex",3), ); }
function test() { $model=D("Info"); $model->create(); if(!$model->create()) { echo $model->getError(); } else { $bs=$model->add(); //跳转页面 //1.成功后跳转:success("提示的话","跳转的操作方法",等待的时长) //2.失败后跳转:error("提示的话","默认跳回上一个页面") if($bs) { $this->success("添加成功","test"); } else { $this->error("添加失败"); } } }
动态方式:使用模型类的validate方法动态创建自动验证规则。
function test() { $model=D("Info"); $model->create(); $rules=array( array("Code","require","代号不能为空",0,"regex",3), ); if(!$model->validate($rules)->create()) { echo $model->getError(); } else { $bs=$model->add(); //跳转页面 //1.成功后跳转:success("提示的话","跳转的操作方法",等待的时长) //2.失败后跳转:error("提示的话","默认跳回上一个页面") if($bs) { $this->success("添加成功","test"); } else { $this->error("添加失败"); } } }
ajax
test.html
<load href="__PUBLIC__/Js/jquery-1.11.2.min.js" />
<body> <input id="code" type="text" name="code" /> <input id="btn" type="button" value="查看姓名" /> </body> </html> <script type="text/javascript"> $(document).ready(function(e) { $("#btn").click(function(){ var code=$("#code").val(); $.ajax({ url:"ChuLi", data:{code:code}, type:"POST", dataType:"TEXT", success: function(data){ alert(data); } }) }); });
</script>
function ChuLi() { $code=$_POST["code"]; $model=D("Info"); $aname=$model->find($code); $name=$aname["name"]; //返回到ajax,$this->ajaxReturn(返回的值,返回的值的类型); $this->ajaxReturn($name,"eval"); }
test.html
<load href="__PUBLIC__/Js/jquery-1.11.2.min.js" /> <body> <form action="__ACTION__" method="post"> <div>代号:<input id="dh" type="text" name="Code" /></div> <span id=‘dhyz‘></span> <div>姓名:<input type="text" name="Name" /></div> <div>性别:<input type="text" name="Sex" /></div> <div>确认:<input type="text" name="Queren" /></div> <div>民族:<input type="text" name="Nation" /></div> <div>生日:<input type="text" name="Birthday" /></div> <div><input type="submit" value="提交" /></div> </form> </body> <script type="text/javascript"> $(document).ready(function(e) { $("#dh").blur(function(){ var code=$(this).val(); $.ajax({ url:"YanZheng", data:{Code:code}, type:"POST", dataType:"TEXT", success: function(data){ if(data.trim()=="OK") { $("#dhyz").html("验证通过"); $("#dhyz").css("color","green"); } else { $("#dhyz").html(data); $("#dhyz").css("color","red"); } } }) }) }); </script>
function YanZheng() { $model=D("Info"); $str=""; $rules=array( array("Code","require","代号不能为空",0,"regex",3), ); if(!$model->validate($rules)->create()) { $str=$model->getError(); } else { $str="OK"; } $this->ajaxReturn($str,"eval"); }
标签:
原文地址:http://www.cnblogs.com/yy01/p/5726450.html