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();' –

相關問題