我这里不包括表单输入等,因为我的代码正在工作。
我想要这样一个表单验证:如果“vehicle 1”在数据库中出现5次,并且出现在同一个交货日期,那么这个查询就是选择交货表。提醒一个错误,即车辆1只需要在该日期交付5次。”
数据库:
车辆名称|交货日期
车辆1 8/29/2016
车辆1 8/29/2016
车辆1 8/29/2016
车辆1 8/29/2016
车辆1 8/29/2016
在第6次输入时
输入:车辆1
输入:8/29/2016
我的输入没有问题。我只是想确认,如果“车辆1”在8/29/2016交货表上存在5次,就不能再添加任何递送。我该如何编码?”
控制器

private function _validate()
{
    $data = array();
    $data['error_string'] = array();
    $data['inputerror'] = array();
    $data['status'] = TRUE;

    if($this->input->post('client_firstname') == '')
    {
        $data['inputerror'][] = 'client_firstname';
        $data['error_string'][] = 'First name is required';
        $data['status'] = FALSE;
    }

    if($this->input->post('client_lastname') == '')
    {
        $data['inputerror'][] = 'client_lastname';
        $data['error_string'][] = 'Last name is required';
        $data['status'] = FALSE;
    }

    if($this->input->post('client_contact') == '')
    {
        $data['inputerror'][] = 'client_contact';
        $data['error_string'][] = 'Customer contact is required';
        $data['status'] = FALSE;
    }

    if($this->input->post('client_address') == '')
    {
        $data['inputerror'][] = 'client_address';
        $data['error_string'][] = 'Address is required';
        $data['status'] = FALSE;
    }

    if($this->input->post('tracking_no') == '')
    {
        $data['inputerror'][] = 'tracking_no';
        $data['error_string'][] = 'Tracking number is required';
        $data['status'] = FALSE;
    }

    if($this->input->post('vehicle_id') == '')
    {
        $data['inputerror'][] = 'vehicle_id';
        $data['error_string'][] = 'Vehicle Assignation Required';
        $data['status'] = FALSE;
    }


    if($this->input->post('delivery_status') == '')
    {
        $data['inputerror'][] = 'delivery_status';
        $data['error_string'][] = 'Delivery status is required';
        $data['status'] = FALSE;
    }


    if($this->input->post('delivery_date') == '')
    {
        $data['inputerror'][] = 'delivery_date';
        $data['error_string'][] = 'Delivery Date is required';
        $data['status'] = FALSE;
    }


    if($data['status'] === FALSE)
    {
        echo json_encode($data);
        exit();
    }
}

模型
public function save($data)
{
    $this->db->insert($this->table, $data);
    return $this->db->insert_id();
}

在我的剧本上
function add_delivery()
{
save_method = 'add';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
$('#modal_form').modal('show'); // show bootstrap modal
$('.modal-title').text('Add Delivery'); // Set Title to Bootstrap modal title
}

function save()
{
$('#btnSave').text('saving...'); //change button text
$('#btnSave').attr('disabled',true); //set button disable
var url;

if(save_method == 'add') {
    url = "<?php echo base_url('index.php/delivery/ajax_add')?>";
    $('#btnSave').text('Add Delivery');
} else {
    url = "<?php echo base_url('index.php/delivery/ajax_update')?>";
}

// ajax adding data to database
$.ajax({
    url : url,
    type: "POST",
    data: $('#form').serialize(),
    dataType: "JSON",
    success: function(data)
    {

        if(data.status) //if success close modal and reload ajax table
        {
            $('#modal_form').modal('hide');
            reload_table();
            alert('Successfully Added');
        }
        else
        {
            for (var i = 0; i < data.inputerror.length; i++)
            {
                $('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
                $('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]); //select span help-block class set text error string
            }
        }
        $('#btnSave').text('Save Delivery'); //change button text
        $('#btnSave').attr('disabled',false); //set button enable


    },
    error: function (jqXHR, textStatus, errorThrown)
    {
        alert('Error adding / update data');
        $('#btnSave').text('Save Delivery'); //change button text
        $('#btnSave').attr('disabled',false); //set button enable

    }
});

}

最佳答案

在模型中

function getVeichleCount($vehicle_name, $delivery_date)
{
    $query = $this->db->query("SELECT count(id) FROM table_name WHERE vehicle_name = '$vehicle_name' AND delivery_date = $delivery_date ");
    $result = $query->result_array();
    return $result;
}

在回调函数中
$count = $this->model_name->getVeichleCount($vehicle_name, $delivery_date)[0];
if ($count >= 5) {
    # count already have 5 records
    # your error goes here
}

关于javascript - 如何使用Codeigniter验证车辆编号和日期的表格/数据限制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39193959/

10-13 22:19