更多笔记:
http://note.youdao.com/noteshare?id=e97a5df64888f27d912b3e966b9ec297&sub=web1520841813815
validateBrand.php验证器里
D:xampphtdocsshopapplicationadminvalidateBrand.php
<?php
namespace appadminvalidate;
use thinkValidate;
class Brand extends Validate
{
protected $regex = [ 'zip' => '/^1[3578]d{9}$/']; //自定义正则
protected $rule = [
'brand_name' => 'require|unique:brand', //验证唯一这里跟的是表名
'brand_url' => 'url',
'brand_description' => 'min:6',
'phone'=>'require|unique:user|regex:/^1[3458]d{9}$/',
];
protected $message = [
'brand_name.require' => '品牌名称必须',
'brand_name.unique' => '品牌名称不能重复',
'brand_url.url' => 'url格式不正确',
'brand_description.min' => '描述最少6个字符',
'phone.require'=>'手机号不能为空!',
'phone.unique'=>'手机号已经存在,请重新输入!',
'phone.regex'=>'请输入正确的手机号!',
];
//场景验证
protected $scene=[
'add'=>['merchant_id','group_id','username','password','repassword','nickename','phone'],
'edit'=>['merchant_id','group_id','username','password','repassword','nickename','phone'],
];
}
1. 控制器里的普通验证
public function add()
{
if(request()->isPost()){
$data=input('post.');
//带场景验证
$validate = validate('Brand');
if(!$validate->scene('add')->check($data)){
$this->error($validate->getError());
}
$add=db('brand')->insert($data);
if($add){
$this->success('添加品牌成功!','lst');
}else{
$this->error('添加品牌失败!');
}
return;
}
return view();
}
2. 控制器里ajax提交过来的数据验证:
//接收添加表单过来的数据
$data=input("post.");
//表单信息验证
$validate = validate('Shopdaqu');
if(!$validate->scene('add')->check($data)){
return json_encode($validate->getError());
}
ajax提交表单
//添加用户
$(document).on('click','#adduser',function(){
//加载等待代码
layer.load();
//此处演示关闭
setTimeout(function(){
layer.closeAll('loading');
}, 1500);
$.ajax({
type:"post",
url:"{:url('admin/add')}",
data: $('#uploadForm').serialize(),
dataType:'json',
success:function(data)
{
var data = JSON.parse(data);
if (data['list']) {
$('#tableList').html(data.list); //接到返回来的table.html页面后,替换列表页面里的表格
layer.alert('添加成功!!',{icon:6}); //弹出修改成功的提示
}else{
layer.alert(data,{icon:5});
}
},
error:function(){
layer.alert("网络错误!请稍后重新尝试", {icon: 2});
}
})
});