CodeIgniter Forums

Full Version: Custom validation function doesn't work.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

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

El Forum

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

El Forum

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

El Forum

[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.

El Forum

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

Thanks a lot, everything works fine now.