Cannot get my custom validation to work, - 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: Cannot get my custom validation to work, (/showthread.php?tid=41468) |
Cannot get my custom validation to work, - El Forum - 05-09-2011 [eluser]boltsabre[/eluser] Hi guys, I've got a custom funciton I want to use for my validation, it's just a basic 'bad word' checker. So I extended to parent CI_Form_validation class, but I'm getting an error message when I load the form. It was working fine until I extended the class, so I know the problem is there somewhere any help would be great - I read all the documentation about custom validation, but it only talks about putting it directly in the controller itself, which is not what I want to do as I'll be using it in many controllers! My controller function for loading the validation rules: Code: ... My extension of the CI_Form_validation class (saved under /application/libraries/MY_Form_validation.php): Code: class MY_Form_validation extends CI_Form_validation { And the error message I'm now getting when I navigate to the page with the form is: A PHP Error was encountered Severity: Notice Message: Undefined property: MY_Form_validation::$form_validation Filename: libraries/MY_Form_validation.php Line Number: 23 Fatal error: Call to a member function set_message() on a non-object in C:\xampplite\htdocs\www.workandtravelaustralia.com\thisone\application\libraries\MY_Form_validation.php on line 23 Any help would be great, thanks guys!!! Cannot get my custom validation to work, - El Forum - 05-09-2011 [eluser]boltsabre[/eluser] Ah, quick edit. I first developed the 'filterBadWords' function in the controller and called it using the standard callback_filterBadWords as outlined in the documentation, and it was working perfect. I am now only getting this message since I moved the function to the class extension 'MY_Form_validation extends CI_Form_validation', so that is where the problem lies... any thoughts would be great, I'm realively new to CI and this is the first time I've tried extending a core class (have I saved it in the correct folder?) Okay, cheers guys, hope someone can help me, many thanks in advance!!! Cannot get my custom validation to work, - El Forum - 05-09-2011 [eluser]darrentaytay[/eluser] In MY_Form_Validation, you don't have access to the CI Super Object ($this). If add this at the top: Code: $CI =& get_instance(); And then instead of Code: $this->form_validation->set_message('filterBadWords', 'You are not allowed to use swear words'); Use this Code: $CI->form_validation->set_message('filterBadWords', 'You are not allowed to use swear words'); Cannot get my custom validation to work, - El Forum - 05-09-2011 [eluser]boltsabre[/eluser] Ah your a magician, will try this as soon as I get home from work. (I really should have read up on extending core classes, I imagine it's there 'loud and clear' about $this not being available). Sorry to have wasted your time, but thanks for the heads-up!!! Cannot get my custom validation to work, - El Forum - 05-09-2011 [eluser]boltsabre[/eluser] Okay, so if anyone else is reading this, darrentaytay was spot on, except for one small typo, you need to type this to get it to work: Code: $this->CI->form_validation->set_message('filterBadWords', 'You are not allowed to use swear words'); not Code: $CI->form_valida... Thanks darrentaytay for the help!!! |