Welcome Guest, Not a member yet? Register   Sign In
Custom validation function doesn't work.
#1

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

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

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

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

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

Thanks a lot, everything works fine now.




Theme © iAndrew 2016 - Forum software by © MyBB