KO 2.3.4 - Accessing validation array from callbacks in models

Posted by kenny99 on Stack Overflow See other posts from Stack Overflow or by kenny99
Published on 2010-04-30T11:46:20Z Indexed on 2010/05/06 8:48 UTC
Read the original article Hit count: 327

Filed under:
|

Hi,

Apologies if this is an oversight or sheer stupidity on my part but I can't quite figure out how to access the validation array from a callback in a model (using ORM and KO 2.3.4). I want to be able to add specific error messages to the validation array if a callback returns false.

e.g This register method:

public function register(array & $array, $save = FALSE)
{
    // Initialise the validation library and setup some rules
    $array = Validation::factory($array)
            ->pre_filter('trim')
            ->add_rules('email', 'required', 'valid::email', array($this, 'email_available'))
            ->add_rules('confirm_email', 'matches[email]')
            ->add_rules('password', 'required', 'length[5,42]')
            ->add_rules('confirm_password', 'matches[password]');

    return ORM::validate($array, $save);
}

Callback:

public function email_available($value)
{
    return ! (bool) $this->db
        ->where('email', $value)
        ->count_records($this->table_name);
}

I can obviously access the current model from the callback, but I was wondering what the best way to add custom error from the callback would be?

© Stack Overflow or respective owner

Related posts about kohana

Related posts about kohana-orm