Search code examples
javascriptphptwitter-bootstrapcodeigniterbootstrapvalidator

value already exist using bootstrapvalidation on codeigniter if value already exist


I Tried using bootstrap validation for a project with CodeIgniter framework , but when I insert the current value ' ID ' validation does not work when there is value already exist in the database.

ITS SOLVED

this my controllers

public function check_user()
{
    $id= $this->input->post('id');
    $result= $this->mcrud->check_user_exist($id);
    if($result)
    {
        echo "false";

    }
    else{

        echo "true";
    }
}

this my models

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

and this my javascipt for validation

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

Solution

  • As i notice, you are not passing any value through post using data, So try this,

        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(),
                              };
                         },
                   }                           
             }
        },
    

    For more information see here