• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot get my custom validation to work,

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:

$this->form_validation->set_rules('title', 'Title', 'required|max_length[100]|alpha_numeric|filterBadWords');

My extension of the CI_Form_validation class (saved under /application/libraries/MY_Form_validation.php):
class MY_Form_validation extends CI_Form_validation {

    function __construct()
    function filterBadWords($string){
        $badWords = array("ban","bad","user","pass","stack","name","html");

        $string = $string;
        $matches = array();
        $matchFound = preg_match_all(
                        "/\b(" . implode($badWords,"|") . ")\b/i",

        if ($matchFound) {
            $this->form_validation->set_message('filterBadWords', 'You are not allowed to use swear words');
            return false;
        } else{
            return true;

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!!!

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!!!

In MY_Form_Validation, you don't have access to the CI Super Object ($this).

If add this at the top:

$CI =& get_instance();

And then instead of

$this->form_validation->set_message('filterBadWords', 'You are not allowed to use swear words');

Use this

$CI->form_validation->set_message('filterBadWords', 'You are not allowed to use swear words');

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!!!

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:

$this->CI->form_validation->set_message('filterBadWords', 'You are not allowed to use swear words');



Thanks darrentaytay for the help!!!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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