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

#1
[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:
...
$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):
Code:
class MY_Form_validation extends CI_Form_validation {

    function __construct()
    {
        parent::__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",
                        $string,
                        $matches
                      );

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


Messages In This Thread
Cannot get my custom validation to work, - by El Forum - 05-09-2011, 03:31 AM
Cannot get my custom validation to work, - by El Forum - 05-09-2011, 04:20 AM
Cannot get my custom validation to work, - by El Forum - 05-09-2011, 04:32 AM
Cannot get my custom validation to work, - by El Forum - 05-09-2011, 05:15 AM
Cannot get my custom validation to work, - by El Forum - 05-09-2011, 02:23 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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