如何使用 codeigniter 对 vehicle_number 和日期进行表单验证/数据限制

How form validation/ data limitation of a vehicle_number and date using codeigniter

我没有在此处包括表单输入等,因为我的代码可以正常工作。 我想要一个像这样的表单验证查询是 select 交货 table 如果 "vehicle 1" 在数据库中出现 5 次并且在同一交货日期。警告车辆 1 只需要在该日期交付 5 次的错误。"

数据库:
vehicle_name | delivery_date
车辆 1 2016 年 8 月 29 日
车辆 1 2016 年 8 月 29 日
车辆 1 2016 年 8 月 29 日
车辆 1 2016 年 8 月 29 日
车辆 1 2016/8/29

6号输入
输入:车辆 1
输入:2016 年 8 月 29 日

我的输入没有问题。我只想确认如果 "vehicle 1" 在 2016 年 8 月 29 日的 table 交货时存在 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
}