CodeIgniter Forums
Custom validation function doesn't work. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Custom validation function doesn't work. (/showthread.php?tid=11838)



Custom validation function doesn't work. - El Forum - 09-25-2008

[eluser]loathsome[/eluser]
Hello,

I've tried this numerous times now, and I can never get the validation class to accept my custom rules. Take this example.

Code:
<?php

class Welcome extends Controller {
    
    function __construct()
    {
        parent::Controller();
    }
    
    function index()
    {
        $this->load->helper('form');
        $this->load->library('validation');
        
        $rules['name'] = 'mekk';
        $fields['name'] = 'username';
        
        $this->validation->set_rules($rules);
        $this->validation->set_fields($fields);
        
        if(FALSE == $this->validation->run())
        {
            $this->load->view('theform');
        }
        /**
         * the form won
         **/
        else
        {
            $this->load->view('success');
        }
    }
    
    /**
     * callback functions
     **/
    
    function mekk($input)
    {
        if($input == 'hello')
        {
            return true;
        }
        else return false;
    }
    
}

No matter what I type into the field, it goes through. I want it ONLY to validate if I write "hello".

Appreciate any help. Thanks a lot!


Custom validation function doesn't work. - El Forum - 09-25-2008

[eluser]xwero[/eluser]
You have to prefix the callback with callback_ so in your example it will be
Code:
$rules['name'] = 'callback_mekk';



Custom validation function doesn't work. - El Forum - 09-25-2008

[eluser]loathsome[/eluser]
Yeah, sorry forgot to tell that I've tried with that too -- still no go. Many thanks, xwero.


Custom validation function doesn't work. - El Forum - 09-25-2008

[eluser]xwero[/eluser]
Without the prefix it won't work at all.

If you have no input the mekk rule is not going to be reached so you need to add required.


Custom validation function doesn't work. - El Forum - 09-25-2008

[eluser]loathsome[/eluser]
Ah, that's right! That just makes sense too.

Thanks a lot, everything works fine now.