Welcome Guest, Not a member yet? Register   Sign In
Logic of my own form_validation function!
#1

[eluser]brucebat[/eluser]
Hi all,

I am trying to make a formvalidation function that compares a series of time inputs and makes sure they are in a chronological order.

I have spent a few hours trying to get it to work but my logic is flawed as there isnt a way of me preserving a value so the function can compare that value.

So my inputs are like this:

time0, time1, time2, time3

So if its time0 field then the validaiton function should recognize that its the first time and not perform validation obviously because there is no time before it.

However if its not the first time then it must compare that time to the time before it.

Here is my for calling the validation for all the fields:

Code:
for ($i=0 ; $i<$completedeventrows; $i++)
{
//this stores a value of the count for time_order validation function
$this->session->set_flashdata('count', $i);

//performs standard CI validations + custom validation to check the time is in correct format and also check the times are in chrono order
                          
$this->form_validation->set_rules('time'.$i, 'Time'.($i+1), 'required|xss_clean|callback_valid_time|callback_time_order'); // time0, time1, time2 etc
}

The validation function "time_order()"

Code:
//this function checks to ensure all events are in a chronological order
            public function time_order ($str)
            {
                
                
                //if the time is the first event then do not compare to previous time as there is none
                if ($this->session->flashdata('count') <= 0)
                {
                    //This part seperates the string into three segments using the ":" as the divide
                    $temp = explode(':',$str);
                    return TRUE;
                
                }
                
                //compare the current event time to its previous one
                else
                {
                    //get current event time
                    $current = explode (':', $str);
                    
                        
                        //if the current times hours part is less than previous hours part then false
                        if ($current[0] < $temp[0])
                        {
                            $this->form_validation->set_message('time_order', 'The %s fields Hour segment is before the the previous events Hour');
                            return FALSE;
                        
                        }
                        
                        //if current time hours sgement is greater than or equal then check minutes segment
                        else if ($current[0] == $temp[0])
                        {
                            
                            if ($current[1] < $temp[1])
                            {
                                $this->form_validation->set_message('time_order', 'The %s fields Minutes segment of time is before the the events time');
                                return FALSE;
                        
                            }
                            
                            else if ($current[1] == $temp[1])
                            {
                                
                                //if seconds are lesser than previous times seconds then false
                                if ($current[2] < $temp[2])
                                {
                                
                                    $this->form_validation->set_message('time_order', 'The %s fields Seconds segment of time is before the the previous events time ');
                                    return FALSE;
                                }
                                
                                else
                                {
                                
                                    return TRUE;
                                }
                            
                            
                            }
                            
                            else
                            {
                            
                                return TRUE;
                            }
                        
                        }
                        
                        //if current times hour segment is greater then no need to check mins or seconds
                        else
                        {
                            return TRUE;
                        
                        }
                
                
                }
            
            
            
            }

The problem is that I keep losing Temp so there is no way to compare as

Any suggestions?

Thanks
#2

[eluser]skunkbad[/eluser]
I think what you need to do is forget about loading the values in the session. When a form is submitted, and it is being validated by the form validation class, the session information that you set isn't available until the next pageload, due to the way that cookie data is stored/retrieved.

What you should do is have your callback function get the values of the posted data through $this->input->post('time0'), etc.

Then, if you are trying to compare the actual time values and get the most recent, consider php's uasort() function. It sounds like you are trying to over-complicate this.
#3

[eluser]boltsabre[/eluser]
And SESSIONS may not be an ideal solution for another reason... people can turn them off!

I'd suggest writing your times to a DB, that way you have a 'solid state' variable that will always be there (but then that raises the question of DB bloating, you'll have to come up with some CRON fix for this to delete old stuff you no longer need.). But this way you can just get the 'last entry' from the db and compare it with your new POST time.
#4

[eluser]brucebat[/eluser]
What about for looping all my times into an array and then passing that array to my validation function?
#5

[eluser]skunkbad[/eluser]
[quote author="brucebat" date="1312833689"]What about for looping all my times into an array and then passing that array to my validation function?[/quote]

Yeah, why not. There are a couple of ways you could do that. One is use a post variable that is an array, like name="time[]". The second option would be to change the values in the post array before loading the form validation class.




Theme © iAndrew 2016 - Forum software by © MyBB