项目场景:展会数据分配,当选择分配的展会后,需要请求后台校验这条数据是否已经存在,避免重复分配。
html
js
// 自定义校验:参展商是否可以分配
$.validator.config({
rules: {
unique: function (element) {
return $.ajax({
url: 'zlf/actor/check',
type: 'POST',
data: {exhid: $("#c-exhid").val(), comid: $("#comid").val(), value: element.value},
dataType: 'json'
});
}
}
});
后端
public function check(){
$exhid = $this->request->request("exhid");
$comid = $this->request->request("comid");
$result = $this->zlfdb()->table('Actor')
->field('exhid,comid')
->where(['exhid'=>$exhid,'comid'=>$comid])
->find();
if ($result) {
//失败
$this->error("该展商已在该展会",'',$result);
} else {
//成功
$this->success();
}
}