Welcome Guest, Not a member yet? Register   Sign In
Custom Form Validation with Database
#1

[eluser]terrycyleung[/eluser]
Hi Everyone,
I would like to ask for advice how can I create a custom function to check my appointment form with database.

Checking the start time and end time in my form with database "start_time" and "end_time" of the same appointment date.

Thank you so much.



#2

[eluser]TheFuzzy0ne[/eluser]
You can call a database method from your validation callback. You can attach a callback to ONE of those fields, and use the callback pass both values to your database method.

If you attach the callback to the first field (but define your rules with the second field first), you should be able to check that there are no errors in the first field (by using form_error()), and then pass both values to the database to check if there's an overlap.
#3

[eluser]terrycyleung[/eluser]
As I'm a beginner, I don't know which database function can help to me to do that, could everyone please help to write some sample for my reference, thank you so much.

#4

[eluser]TheFuzzy0ne[/eluser]
I'm sorry, but I'm not going to write this for you. I expect you to give it a damn good try first, and then we'll help you sort it out.

The user guide is extremely clear and concise. Everything you need to know is in there. If not, there are lots of tutorials around that will help you.

Good starting points:
http://ellislab.com/codeigniter/user-gui...odels.html
http://ellislab.com/codeigniter/user-gui...ecord.html
http://ellislab.com/codeigniter/user-gui...ation.html

So, first of all you need a callback method that makes sure the times are in the format you expect them to be in. If the times are not in this format, a message should be displayed.

Assuming you are displaying your errrors inline, I would define my rules in this order:
Code:
$validation_rules = array(
    // ...
    array(
        'field' => 'end_time',
        'label' => 'End Time',
        'rules' => 'required|callback__check_time[end]',
    ),
    array(
        'field' => 'start_time',
        'label' => 'Start Time',
        'rules' => 'required|callback__check_time[start]',
    ),
    // ...
);

Now, we define the end time first, so that it's validated first. When the start time is validated, we do a little more checking to make sure the appointment doesn't overlap with another. If it does, the 'start_time' field will be where the error is attached to. The validation callback takes an extra argument, so it knows whether or not it's validating the start time or end time - since start time requires a little extra validation - but this extra validation should only be performed if the end time and start time are both valid.

Your validation rule should check the following:
That an argument has been passed, and it is either 'start' or 'end'
That the time is a valid time.

And extra validation for when 'start' is passed as a parameter:
Check that end time has no errors.
Check that there is no overlapping appointment.

I hope this helps.
#5

[eluser]terrycyleung[/eluser]
Hi thefuzzy0ne,
I would like say thank you for your reply and help first, thanks for your kindness and I should try before asking help. ^_^

I try to simplify this function and I can modify it after the function worked but it doesn't could you and anyone can consult for me, thank you so much.


I want to create the function to check appointment start time cannot be same as database record but the result after I submit the appointment and the browser show blank. I also check with database no record insert to it.

Code:

function booking_check($appointment_date, $start_time, $end_time){
$appointment_date = $this->input->post('appointment_date');
$start_time = $this->input->post('start_time');
$end_time = $this->input->post('end_time');

$query = $this->appointment_model->get_appointments($appointment_date);

foreach ($query->result() as $row)
{
if(strtotime($start_time) == $row->start_time){
$this->form_validation->set_message('booking_check', '%s Another Patient booked at this time');
return FALSE;} //End If
}
#6

[eluser]TheFuzzy0ne[/eluser]
A blank page is normally caused by outputting a space or tab after a PHP closing tag. You can safely omit closing tags at the end of a file, and it helps prevent this kind of thing. Also, be sure that you have error reporting enabled. At the top of your index.php file, add the following:

Code:
ini_set('display_errors', '1');
error_reporting(E_ALL);

Once we have fixed the problem, we can move on from there.
#7

[eluser]terrycyleung[/eluser]
Master TheFuzzy0ne and everyone,

Thanks for your opinion, I guess my function has something wrong, could you please help to check and give me some advice, thank you so much.

is my function has any logical error? In my case, I want to check the start time with query record, is it some error in foreach query?

function booking_check($appointment_date, $start_time, $end_time){
$appointment_date = $this->input->post(‘appointment_date’);
$start_time = $this->input->post(‘start_time’);
$end_time = $this->input->post(‘end_time’);

$query = $this->appointment_model->get_appointments($appointment_date);

foreach ($query->result() as $row)
{
if(strtotime($start_time) == strtotime($row->start_time)){
$this->form_validation->set_message('booking_check', '%s Another Patient booked at this time');
return FALSE;} //End If
}


function get_appointments($appointment_date)
{
$query = $this->db->get_where('appointments', array('appointment_date' => $appointment_date));
return $query->result_array();
}
#8

[eluser]TheFuzzy0ne[/eluser]
Have you solved the "blank page" issue yet?
#9

[eluser]davidMC1982[/eluser]
I hope that's not the actual code. If it is, there's a function nested inside another function and/or no closing } for the booking_check function.
#10

[eluser]terrycyleung[/eluser]
Hi, I haven't solved the "blank page" issue yet because I still don't know how to do it and the "blank page" only will when I enable a new custom validation function. So, is it my function have any logical problem?
thank you so much.




Theme © iAndrew 2016 - Forum software by © MyBB