Doraemon 24 Doraemon 24 - 3 months ago 15
Javascript Question

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

i dont include here the form input Etc. because my code is working.
i want a form validation like this query is to select delivery table if "vehicle 1" appears on the database 5 times and on the same delivery date. alert an error that Vehicle 1 need to have a delivery 5 times only on that date."

DATABASE:

vehicle_name | delivery_date

vehicle 1 8/29/2016

vehicle 1 8/29/2016

vehicle 1 8/29/2016

vehicle 1 8/29/2016

vehicle 1 8/29/2016


on the 6th input

input: vehicle 1

input: 8/29/2016


there is no problem in my inputs. i just want a validation that you cannot add any delivery anymore if "vehicle 1" exist 5 times on delivery table on 8/29/2016. HOW WILL I CODE THAT?"

Controller

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


Model

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


on my script

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

}
});


}

Answer

In Model

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

In Callback function

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