0
votes

My User_model have a public method called is_unique_email($email). This method checks if a user has a uniqe mail adress with some status flag checks. This is also the reason why I can't use the standard is_unique validation rule from CodeIgniter.

I'm using a form_validation.php with config array for my validation rule groups. My question is: How can I call the model method for checking the new user's e-mail address? I searched and tried so many things, but nothing work. My preferred call would be with | pipe separator.

Like: trim|required|max_length[70]|valid_email|<~ here comes the model callback ~>

Is there any solution for this callback or is there no way and I have to extend the Form_validation system library?

I'm using CodeIgniter 3.1.7.

Thanks in advance!

1

1 Answers

0
votes

UPDATE:

Because I've always done things via extending the form_validation library I forgot about this:

https://codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods

and this (anonymous functions):

https://codeigniter.com/user_guide/libraries/form_validation.html#callable-use-anything-as-a-rule

Might be better for you. When in doubt, always read the docs ;)


Yes you can extend the form_validation library. In application/library make a MY_Form_validation.php and have it extend CI's as such:

class MY_Form_validation extends CI_Form_validation {

then in it you can do something like this:

/**
 * Checks to see if bot sum is valid 
 * e.g. equals the session stored values
 * 
 * @param int $sum
 * @return boolean
 */
public function valid_bot_sum($sum) {
    $generated = $this->CI->session->bot_first_number + $this->CI->session->bot_second_number;
    if ($generated !== intval($sum)) {
        $this->set_message('valid_bot_sum', 'Invalid bot sum.');
        return false;
    } else {
        return true;
    }
}

and now your function can be accessed via pipe separators as any native form_validation validation function. Just be sure to set the message on false as I've done, otherwise you will get an error. You can access the CI instance like so $this->CI.

In your case you can either migrate the function from the model into this file, or you can call it by loading the model in the function and calling the function and just testing to see if it evaluates to true/false and handling as above.