0

我尝试使用CodeIgniter框架的项目使用引导验证,但是当我插入当前值“ID”时,数据库中已存在值时验证不起作用。价值已经存在使用codeigniter bootstrapvalidation如果值已存在

ITS解决

这是我的控制器

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; 
    } 
} 

,这我进行验证

$(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' 
           }       
         } 
       }, 
01 javascipt的

回答

1

正如我注意到,你是不是通过使用data通过后的任何值, 那么试试这个,

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

+0

感谢@Niranjanñ拉朱,我尝试,但它不是工作。控制器和模型有什么问题? –

+0

检查控制台,是传递给控制器​​的'id'?在控制器中回显身份,看看你到达那里。 –

+0

也可以不用'return $ query-> result();'试一下,'return $ query-> num_rows();' –

相关问题