Search code examples
phpkohana

Kohana validation - why my custom rule always return true?


Why my custom rule always return true? My Controller:

class Controller_Users extends Controller {
    public function action_func() {
        if ($_POST) {
            $validation = Validation::factory($_POST);
            $validation->rule('field', 'Test::func', array(':value'));
            $check = $validation->check(); // always true
        }
    }
}

And class Test:

class Test {
    static public function func($value) {
        return false;
    }
}

Clearly indicated - false. But ->check() returns true. Why? And how to fix it?


Solution

  • Because you need use $validation->error()

    class Controller_Users extends Controller {
        public function action_func() {
            if ($_POST) {
                $validation = Validation::factory($_POST);
                $validation->label('field','Some label')
                $validation->rule('field', 'Test::func', array(':value',':validation'));
                $check = $validation->check(); // always true
            }
        }
    }
    

    And class Test:

    class Test {
        static public function func($value, $validation) {
            $validation->error('field','Some error desc');
            return false;
        }
    }