使用场景:会员名、标题等唯一性校验
效果图

1.HTML部分
- 添加
<input id="c-title" data-rule="required;remote(get:archives/check_title_available)" class="form-control" name="row[title]" type="text" value="">
备注:
data-rule="required;remote(get:archives/check_title_available)"
这句话的含义:
校验规则
required 必填
remote(get:archives/check_title_available) 访问地址为archives/check_title_available,控制器名:archives,函数名:check_title_available
- 编辑
-
<input id="c-title" data-rule="required;remote(get:archives/check_title_available?row[id]={$row.id})" class="form-control" name="row[title]" type="text" value="{$row.title}">
备注:
data-rule="required;remote(get:archives/check_title_available?row[id]={$row.id})"
与上述所说的,添加后缀?row[id]={$row.id},此为get参数,id为不含当前编辑的文章。
2.控制器部分
public function check_title_available(){
$params = $this->request->get("row/a");
$title = $params['title'];
$id = isset($params['id']) ? (int)$params['id'] : '';
if($id){
$this->model->where('id', '<>', $id);
}
$count = $this->model->where('title', '=', $title)->count();
if ($count > 0)
{
$this->error('该标题已经占用');
}
$this->success();
}
具体的业务逻辑还需要各位大神自行修改
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)