• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved]Custom Validation?

#1
[eluser]brucebat[/eluser]
Hi all,

I have been looking into the form validation and I want to make some of my own validation functions.

Using this : http://ellislab.com/codeigniter/user-gui...ationrules

However it does not go into detail in making your own validation functions.

For example I am needing a function that will check a field has a correctly formatted time of (HH:MM:SS). There does not appear to be anything like that in Codeigniter after googling.

Any sample code or links that will help me get started?
Thanks


These are all the validation functions I want to make:
Quote:-Field 1 is not greater than Field 2
-Field[0]1 is not the same as Field[1]1
-Time[0] is not greater than Time[1] or vice versa
-Time[x] is valid format.

Example code of someones own validation would be great so I can work from it.

Thanks for your time!

Cheers

#2
[eluser]Mirge[/eluser]
See user guide: http://ellislab.com/codeigniter/user-gui...#callbacks

#3
[eluser]brucebat[/eluser]
Thanks I used the example to make this:

Code:
function valid_time($str)
            {

                //This part seperates the string into three segments using the ":" as the divide
                $parts = explode(':', $str);
                
                //1st validation checks to make sure there are three parts made from string
                if (sizeof($parts) != 3)
                {
                    $this->form_validation->set_message('valid_time', 'The %s fields has too many time segments');
                    return FALSE;
                }
                
                else
                {
                    //if hours segment is not 24 hour compliant
                    if ($parts[0] < 24 || $parts[0] >= 0)
                    {
                        $this->form_validation->set_message('valid_time', 'The %s fields Hour segment is not correct');
                        return FALSE;
                    }
                    
                    //if minutes segment is not 24 hour compliant
                    else if ($parts[1] < 0 || $parts[1] > 59)
                    {
                        $this->form_validation->set_message('valid_time', 'The %s fields Minute segment is not correct');
                        return FALSE;
                    }
                    
                    //if seconds segment is not 24 hour compliant
                    else if ($parts[2] < 0 || $parts[2] > 59)
                    {
                        $this->form_validation->set_message('valid_time', 'The %s fields Seconds segment is not correct');
                        return FALSE;
                    }
                    
                //validation passed!
                return TRUE;
                }
    
            }

However it does not seem to work when testing it using this call:

Code:
$this->form_validation->set_rules('time'.$i, 'Time'.($i+1), 'required|xss_clean|callback_valid_time'); // time0, time1, time2 etc

#4
[eluser]Mirge[/eluser]
What does it do or not do? Explain what isn't working.

#5
[eluser]brucebat[/eluser]
Sorry

Well for instance I put in a time of this in my form "42:43:60"

It should return a message saying "The %s time fields Hour segment is not correct."

There is just a blank white page.

To test I have this just after my validation:

Code:
$this->form_validation->set_rules('time'.$i, 'Time'.($i+1), 'required|xss_clean|callback_valid_time'); // time0, time1, time2 etc

echo validation_errors();
return;

Using the example I used "$str" which I am guessing is a variable passed to my custom function from the form_validation class?

#6
[eluser]Mirge[/eluser]
[quote author="brucebat" date="1312327204"]Sorry

Well for instance I put in a time of this in my form "42:43:60"

It should return a message saying "The %s time fields Hour segment is not correct."

There is just a blank white page.

To test I have this just after my validation:

Code:
$this->form_validation->set_rules('time'.$i, 'Time'.($i+1), 'required|xss_clean|callback_valid_time'); // time0, time1, time2 etc

echo validation_errors();
return;

Using the example I used "$str" which I am guessing is a variable passed to my custom function from the form_validation class?[/quote]

What happens when you remove callback_valid_time from the rules?

Not a blank page?

Seems strange. I copied your code, tested it... and it works as expected. I modified it of course since I tested outside of a CI install... but it gave the expected output:

Code:
&lt;?php

var_dump(valid_time('42:43:60'));

function valid_time($str)
{
    
    //This part seperates the string into three segments using the ":" as the divide
    $parts = explode(':', $str);
    
    //1st validation checks to make sure there are three parts made from string
    if(sizeof($parts) != 3)
    {
        print('The %s fields has too many time segments');
        return FALSE;
    }
    
    else
    {
        //if hours segment is not 24 hour compliant
        if($parts[0] < 24 || $parts[0] >= 0)
        {
            print('The %s fields Hour segment is not correct');
            return FALSE;
        }
        
        //if minutes segment is not 24 hour compliant
        else if($parts[1] < 0 || $parts[1] > 59)
        {
            print('The %s fields Minute segment is not correct');
            return FALSE;
        }
        
        //if seconds segment is not 24 hour compliant
        else if($parts[2] < 0 || $parts[2] > 59)
        {
            // $this->form_validation->set_message('The %s fields Seconds segment is not correct');
            return FALSE;
        }
        
        //validation passed!
        return TRUE;
    }

}

Output: The %s fields Hour segment is not correctbool(false)


perhaps check the value of $i?

#7
[eluser]brucebat[/eluser]
Thanks:

I tried it with my full controller now but it still doesnt work.

To answer your question $i is used as I have many rows of the same fields.

Code:
// Validation part for events
                                //creates validation rules for all rows that have been completed by user
                                    for ($i=0 ; $i<$completedeventrows; $i++)
                                    {
                                        $this->form_validation->set_rules('time'.$i, 'Time'.($i+1), 'required|xss_clean|callback_valid_time'); // time0, time1, time2 etc
                                        $this->form_validation->set_rules('event'.$i, 'Event'.($i+1), 'required');
                                        $this->form_validation->set_rules('supplies'.$i, 'Supplies'.($i+1), 'xss_clean');
                                        $this->form_validation->set_rules('success'.$i, 'Success'.($i+1), '');
                                        $this->form_validation->set_rules('comment'.$i, 'Comment'.($i+1), 'xss_clean');
                                    };
                    
                    
                    
                    echo validation_errors();
                    return;

It should be a blank page but with validation errors echo'd I hope.

However no messages are echo'd so most likely there is a problem with the function.

I am going to remove callback and see what happens.

#8
[eluser]brucebat[/eluser]
Okay good news!

The function must not be getting called as I tried this in it

Code:
public function valid_time($str)
            {
                echo "hello world";
                return;  }

So it must be a problem with the function call.

The example says

Quote:The validation system supports callbacks to your own validation functions. This permits you to extend the validation class to meet your needs. For example, if you need to run a database query to see if the user is choosing a unique username, you can create a callback function that does that. Let's create a example of this.

In your controller, change the "username" rule to this:

$this->form_validation->set_rules('username', 'Username', 'callback_username_check');

#9
[eluser]Mirge[/eluser]
And you created a method called "valid_time" that accepts one argument "$str".

I am assuming you created this method within your class, right?

#10
[eluser]brucebat[/eluser]
Correct

My function is within the Controller class.


The example uses the $str as an argument,

Code:
function username_check($str)
    {

}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.