我尝试使用 CodeIgniter 框架对项目使用 Bootstrap 验证,但是当我插入当前值“ID”时,当数据库中已存在值时,验证不起作用。
ITS SOLVED
这是我的 Controller
public function check_user()
{
$id= $this->input->post('id');
$result= $this->mcrud->check_user_exist($id);
if($result)
{
echo "false";
}
else{
echo "true";
}
}
这是我的模型
public function check_user_exist($id)
{
$this->db->where('id',$id);
$query= $this->db->get('tbdetail');
if($query->num_rows()>0)
{
return $query->result();
}
else
{
return false;
}
}
这是我的用于验证的 javascipt
$(document).ready(function() {
$('#defaultForm').bootstrapValidator({
message: 'This value is not valid',
icon: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
id: {
row: '.col-md-3',
message: 'ID Tidak Valid',
validators: {
notEmpty: {
message: 'ID tidak boleh kosong'
},
remote: {
url: '<?php echo base_url(); ?>index.php/instrument/detailalat/check_user',
type:'POST',
message: 'ID sudah ada'
}
}
},
最佳答案
正如我注意到的,您没有使用 data
通过 post 传递任何值,
所以试试这个,
id: {
row: '.col-md-3',
message: 'ID Tidak Valid',
validators: {
notEmpty: {
message: 'ID tidak boleh kosong'
},
remote: {
url: '<?php echo base_url(); ?>index.php/instrument/detailalat/check_user',
type:'POST',
message: 'ID sudah ada',
data: function(validator) {
return {
id: $('[name="name of the id in your form"]').val(),
};
},
}
}
},
有关更多信息,请参阅here
关于javascript - 如果值已经存在,则使用 codeigniter 中的 Bootstrap 验证值已经存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33359969/