Welcome Guest, Not a member yet? Register   Sign In
Consult: How to compare time field using form validation
#1

[eluser]terrycyleung[/eluser]
Hi All,
I would like to consult how to check the end time cannot early than start time, I've modified the validation code as follow but seem not work, please help, thank you so much.

$this->form_validation->set_rules('start_time', 'Start Time', 'required |less_than[end_time]');


Thanks.
#2

[eluser]Coding Pattern[/eluser]
You should use


$this->form_validation->set_rules(‘start_time’, ‘Start Time’, ‘required');
$this->form_validation->set_rules(‘start_time’, ‘Start Time’, ‘'callback_time_check[end_time]');

In your controller file:

function time_check($start_time, $end_time)
{
return (strtotime($start_time)<strtotime($end_time))?TRUE:FALSE;
}


Look here:
http://ellislab.com/codeigniter/user-gui...ationrules
and
http://ellislab.com/codeigniter/user-gui...#callbacks

Hope this will work...
#3

[eluser]terrycyleung[/eluser]
Thank you so much for your reply, but it seems have some error: it show that "Unable to access an error message corresponding to your field name." after I added this function, could anyone can help me, thank you.

My controller file code:

public function edit($year = NULL,$month = NULL,$day=NULL,$time=NULL)
{
if ($year == NULL)
{$year = date("Y");}
if ($month == NULL)
{$month = date("n");}
if ($day == NULL)
{$day = date("j");}

$app_dt = date("j-n-Y",gmmktime(0, 0, 0, $month, $day, $year));
$data['appointment_date']= $app_dt;

$data['appointment_time'] = $time;

$this->load->helper('form');
$this->load->library('form_validation');

$this->form_validation->set_rules('start_time', 'Start Time', 'required');
$this->form_validation->set_rules('end_time', 'End Time', 'required');
$this->form_validation->set_rules('appointment_date', 'Date', 'required');
$this->form_validation->set_rules('start_time', 'Start Time', 'callback_time_check');





if ($this->form_validation->run() === FALSE)
{
$data['patients'] = $this->patient_model->get_patient();
$data['appointment'] = $this->appointment_model->get_appointment_at($app_dt,$time);
if (isset($data['appointment']['patient_id']))
{
$patient_id = $data['appointment']['patient_id'];
$data['patient'] = $this->patient_model->get_patient_detail($patient_id);
}
$this->load->view('templates/header');
$this->load->view('templates/menu');
$this->load->view('appointments/edit',$data);
$this->load->view('templates/footer');
}
else
{
$this->appointment_model->add_appointment();
$year = date("Y",strtotime($this->input->post('appointment_date')));
$month = date("m",strtotime($this->input->post('appointment_date')));
$day = date("d",strtotime($this->input->post('appointment_date')));
$this->index($year,$month,$day);
}
}
.....


.....
function time_check($start_time, $end_time)
{
return (strtotime($start_time)<strtotime($end_time))?TRUE:FALSE;
}


}
?&gt;
#4

[eluser]TheFuzzy0ne[/eluser]
You can add an error message to ./application/language/english/form_validation_lang.php
Code:
$lang['time_check'] = '%s cannot occur after %s';

Or you can define it from within the validation method in your controller:
Code:
function time_check($start_time, $end_time='')
{
    // Does the start time occur after the end time?
    if (strtotime($start_time) >= strtotime($end_time))
    {
        $this->form_validation->set_message('time_check', '%s cannot occur after %s');
        return FALSE;
    }
    
    // All is well.
    return TRUE;
}

However, you might want to add a little more to your method to make sure you're actually getting a proper time string. With the current setup, all of the following would be valid times (borrowed from [url="http://php.net/manual/en/function.strtotime.php"]php.net[/url]), and strtotime works with date strings, not just time strings.

Code:
now
10 September 2000
+1 day
+1 week
+1 week 2 days 4 hours 2 seconds
next Thursday
last Monday

Or better yet, add another rule/method to check that the time is in the correct format.
#5

[eluser]terrycyleung[/eluser]
Thanks for your both reply, it always show the error "Start Time cannot occur after" after I add a validation method into my controller file, could anyone can help me? thank you so much.

Code:

class Appointment extends CI_Controller {
function __construct()
{
parent::__construct();

$this->load->model('appointment_model');
$this->load->model('patient_model');
$this->load->model('settings_model');
$this->load->helper('url');

$prefs = array (
'show_next_prev' => TRUE,
'next_prev_url' => base_url() . 'index.php/appointment/index',
);
$this->load->library('calendar', $prefs);

}

public function index($year = NULL,$month = NULL,$day=NULL)
{
if ($year == NULL)
{$year = date("Y");}
if ($month == NULL)
{$month = date("n");}
if ($day == NULL)
{$day = date("j");}
$data['year']=$year;
$data['month']=$month;
$data['day']=$day;
$appointment_date = date("Y-n-j",gmmktime(0, 0, 0, $month, $day, $year));
$data['appointments']= $this->appointment_model->get_appointments($appointment_date);
$data['start_time']=$this->settings_model->get_clinic_start_time();
$data['end_time']=$this->settings_model->get_clinic_end_time();
$this->load->view('templates/header');
$this->load->view('templates/menu');
$this->load->view('appointments/browse',$data);
$this->load->view('templates/footer');
}

function time_check($start_time, $end_time)
{
// Does the start time occur after the end time?
if (strtotime($start_time) >= strtotime($end_time))
{
$this->form_validation->set_message('time_check', '%s cannot occur after %s');
return FALSE;
}else{
// All is well.
return TRUE;
}
} // End time_check
#6

[eluser]terrycyleung[/eluser]
public function edit($year = NULL,$month = NULL,$day=NULL,$time=NULL)
{
if ($year == NULL)
{$year = date("Y");}
if ($month == NULL)
{$month = date("n");}
if ($day == NULL)
{$day = date("j");}

$app_dt = date("j-n-Y",gmmktime(0, 0, 0, $month, $day, $year));
$data['appointment_date']= $app_dt;

$data['appointment_time'] = $time;

$this->load->helper('form');
$this->load->library('form_validation');

$this->form_validation->set_rules('start_time', 'Start Time', 'required');
$this->form_validation->set_rules('end_time', 'End Time', 'required');
$this->form_validation->set_rules('appointment_date', 'Date', 'required');
$this->form_validation->set_rules('start_time', 'Start Time', 'callback_time_check');



if ($this->form_validation->run() === FALSE)
{
$data['patients'] = $this->patient_model->get_patient();
$data['appointment'] = $this->appointment_model->get_appointment_at($app_dt,$time);
if (isset($data['appointment']['patient_id']))
{
$patient_id = $data['appointment']['patient_id'];
$data['patient'] = $this->patient_model->get_patient_detail($patient_id);
}
$this->load->view('templates/header');
$this->load->view('templates/menu');
$this->load->view('appointments/edit',$data);
$this->load->view('templates/footer');
}
else
{
$this->appointment_model->add_appointment();
$year = date("Y",strtotime($this->input->post('appointment_date')));
$month = date("m",strtotime($this->input->post('appointment_date')));
$day = date("d",strtotime($this->input->post('appointment_date')));
$this->index($year,$month,$day);
}
}
public function add($patient_id)
{
$this->load->helper('form');
$this->load->library('form_validation');

//$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('start_time', 'Start Time', 'required');
$this->form_validation->set_rules('end_time', 'End Time', 'required');
$this->form_validation->set_rules('appointment_date', 'Date', 'required');
$this->form_validation->set_rules('patient_id', 'Patient', 'required');
$this->form_validation->set_rules('start_time', 'Start Time', 'callback_time_check');




if ($this->form_validation->run() === FALSE)
{
$data['start_time']=$this->settings_model->get_clinic_start_time();
$data['end_time']=$this->settings_model->get_clinic_end_time();
$data['patient'] = $this->patient_model->get_patient_detail($patient_id);
$this->load->view('templates/header');
$this->load->view('templates/menu');
$this->load->view('appointments/add',$data);
$this->load->view('templates/footer');
}
else{
$this->appointment_model->add_patient_appointment();
/*$year = date("Y",strtotime($this->input->post('appointment_date')));
$month = date("m",strtotime($this->input->post('appointment_date')));
$day = date("d",strtotime($this->input->post('appointment_date')));*/
$this->index();
}
}
public function delete($id,$appointment_date){
$year = date("Y",strtotime($appointment_date));
$month = date("m",strtotime($appointment_date));
$day = date("d",strtotime($appointment_date));
$this->appointment_model->delete_appointment($id);
$this->index($year,$month,$day);
}





}
#7

[eluser]terrycyleung[/eluser]
where should I add this "time_check" method? is it any problem in this method? thank you so much.
#8

[eluser]Aken[/eluser]
Use code tags, please.
#9

[eluser]TheFuzzy0ne[/eluser]
You haven't passed the expected parameter to the validation method via your rule, so the method gets an empty string.

Code:
$this->form_validation->set_rules(‘end_time’, ‘End Time’, ‘required|time_check['.$this->input->post('start_time.']’);

But as I mentioned before, you should also check to make sure that the string being passed is in the format you are expecting it to be in. This is the whole point of validation.

Alternatively, you can reference $this->input->post('start_time') from within your validation method, rather than passing it as a rule argument.

#10

[eluser]terrycyleung[/eluser]
Thank you so much. Thanks for everyone who reply for me and let me learn more about it.
Let me revise the code and try again.
^_^




Theme © iAndrew 2016 - Forum software by © MyBB