27

I have a callback function that check_captcha which sees if $row is ==0 or == 1 (this information is queried from sql).

The problem is that I can not call it from $self->form_validation->set_rule('captcha', 'call_back_check_captcha') due to the fact that my function takes in a $row var. The way I'm calling it now I get a Unable to access error message. How can I make this work?

function check_captcha( $row)
{
    if($row ==0)//didnt find any
    {
        $this->form_validation->set_message('captcha', 'text dont match captcha');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}


function create_member() 
        {   

            $past = time() - 7200; 

        $this->db->query("DELETE FROM captcha WHERE captcha_time <".$past);                 
        $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word =? AND ip_address =?";     
        $binds = array($_POST['captcha'], $this->input->ip_address(), $past);
        $query= $this->db->query($sql, $binds);

        $row = $query->row(); //row query rows : if it found an entry =1 

            $self->check_captcha($row->count);

        //VALIDATIONS
        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');      
        $this->form_validation->set_rules( 'email_address', 'Email Address', 'trim|required|valid_email|unique[user.email_address]');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|unique[user.username]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_leng[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation','trim|required|matches[password]');
        if(!$_POST['captcha']){
        $this->form_validation->set_rules('captcha', 'Captcha','trim|required');}else{
        $this->form_validation->set_rules('captcha', 'Captcha', 'callback_check_captcha');}

        if($this->form_validation->run()==FALSE)
        {   //this -> to the curr obj(UserController) && registraion() points to the the function in controller
            $this->registration();  //reloads reg page so they can fill out right stuff
        }
        else
Mat
  • 202,337
  • 40
  • 393
  • 406
David Yang Liu
  • 1,170
  • 2
  • 10
  • 19
  • you shoul add a `_` in front of function name , so that i could not be called from url. `check_captcha` should be `_check_captcha` – Ravinder Payal Apr 17 '16 at 05:07

6 Answers6

52
$this->form_validation->set_message('check_captcha', 'text dont match captcha');

The message name corresponds to the function, not the field. So setting it to "check_captcha" will fix your bug. The error message will use the correct field name.

Laurence
  • 58,936
  • 21
  • 171
  • 212
  • 2
    did the same mistake thnx – Arzon Barua Feb 02 '16 at 07:52
  • It confused me, you are using `set_message()` here but the question starter uses `set_rules()`. Is this a typo by you? Btw, I also use `set_rules()` and have same message. – Roland Mar 27 '18 at 14:06
  • 1
    I used the function name instead of the field name but it still shows the same error message.. I already have a separate language file containing error messages so I'd prefer not to add more lines in the form_validation_lang file to avoid discrepancies in the future. what should I do now? – dapidmini Apr 06 '20 at 05:58
11

Actually the best way, instead of write the error message directly on controller, would be add this entry "check_captcha" on languages.

In my case, the message for validation rule (form validation) "less_than" was not present.

I changed the file /system/language/??/form_validation_lang.php. I've added the missing entry.

Colin Dumitru
  • 3,327
  • 6
  • 30
  • 47
Helton Ritter
  • 111
  • 1
  • 2
2

That helped me

go to application/config/autoload.php and add "Security" helper class there.

$autoload['helper'] = array('security');

Or add this before your form validation

$this->load->helper('security');
Muhammad Tahir
  • 2,351
  • 29
  • 25
1

You can set error message in set_rules :

$this->form_validation->set_rules('captcha', 'Captcha', 'callback_check_captcha',

array('check_captcha' => 'text dont match captcha'));

Agilanbu
  • 2,747
  • 2
  • 28
  • 33
Goshika Mahesh
  • 689
  • 6
  • 5
0

add a entry to your language file with named of the part inside the (yourfieldname) of the errormessage - thats solved the problem

StefB
  • 81
  • 1
  • 11
0

Even if the question is already answered, there is another error that can lead to the same error message:

If you call your callback checkCaptcha, it will not work. Prefer always a name like check_captcha as recommended in Codeigniter/General Topics/PHP style guide.