[solved]Custom Validation? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: [solved]Custom Validation? (/showthread.php?tid=44083) |
[solved]Custom Validation? - El Forum - 08-02-2011 [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-guide/libraries/form_validation.html#validationrules 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 Example code of someones own validation would be great so I can work from it. Thanks for your time! Cheers [solved]Custom Validation? - El Forum - 08-02-2011 [eluser]Mirge[/eluser] See user guide: http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks [solved]Custom Validation? - El Forum - 08-02-2011 [eluser]brucebat[/eluser] Thanks I used the example to make this: Code: function valid_time($str) 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 [solved]Custom Validation? - El Forum - 08-02-2011 [eluser]Mirge[/eluser] What does it do or not do? Explain what isn't working. [solved]Custom Validation? - El Forum - 08-02-2011 [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 Using the example I used "$str" which I am guessing is a variable passed to my custom function from the form_validation class? [solved]Custom Validation? - El Forum - 08-02-2011 [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 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: <?php Output: The %s fields Hour segment is not correctbool(false) perhaps check the value of $i? [solved]Custom Validation? - El Forum - 08-02-2011 [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 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. [solved]Custom Validation? - El Forum - 08-02-2011 [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) 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. [solved]Custom Validation? - El Forum - 08-02-2011 [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? [solved]Custom Validation? - El Forum - 08-02-2011 [eluser]brucebat[/eluser] Correct My function is within the Controller class. The example uses the $str as an argument, Code: function username_check($str) |