【问题标题】:value already exist using bootstrapvalidation on codeigniter if value already exist如果值已经存在,则在 codeigniter 中使用引导验证值已经存在
【发布时间】:2015-10-27 04:27:59
【问题描述】:

我尝试对带有 CodeIgniter 框架的项目使用引导验证,但是当我插入当前值时,当数据库中已经存在值时,验证不起作用。

解决了

这是我的控制器

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

【问题讨论】:

    标签: javascript php twitter-bootstrap codeigniter bootstrapvalidator


    【解决方案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

    【讨论】:

    • 感谢@Niranjan N Raju,我试过了,但没用。控制器和模型有什么问题??
    • 检查控制台,id 是否传递给控制器​​?在控制器中回显 id,看看你得到了什么。
    • 也可以用return $query-&gt;result();代替return $query-&gt;num_rows();
    • 你检查了吗??
    • 这个值是要交给控制器的吗??
    猜你喜欢
    • 1970-01-01
    • 2019-03-11
    • 2015-05-07
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 2017-12-23
    • 1970-01-01
    相关资源
    最近更新 更多