Welcome Guest, Not a member yet? Register   Sign In
Validation callbacks into Models
#11

[eluser]wiredesignz[/eluser]
A nice addition might be for MY_Validation to test isset($parent) and then use $parent =& get_instance() if not valid, so you don't have to pass $CI.

First altering run(&$parent = NULL) to prevent errors of course.

If someone would like to test for me, I will add it to the wiki if it's successful.
#12

[eluser]beemr[/eluser]
[quote author="wiredesignz" date="1208951222"]so you don't have to pass $CI.[/quote]

Actually, I don't think you have to pass anything. Since this library is an extension of CI_Validation, there is already a CI instance, namely $this->CI. I just now replaced $parent with $this->CI and removed the run() argument, and it all worked from my library. I only use this as an extension of the ActiveRecord_Class_Mod, so if you can verify that controller calls will work, I think this extension just got DRY-er.
#13

[eluser]wiredesignz[/eluser]
Yes but the $parent argument allows MY_Validation to work with Modular Extension Models also. Thanks for testing beemr Wink
#14

[eluser]beemr[/eluser]
OK. Not knowing how this would affect Modular Extension Models, I did just what you asked and the function was successful from my library whether I passed run($this->CI) or not.

The new run() opening looks like this:

Code:
function run(&$parent = NULL)
    {
        if (!isset($parent)) $parent =& get_instance();

Hope that helps.

P.S. Any good links on the merits of an HMVC vs. standard MVC?
#15

[eluser]wiredesignz[/eluser]
Thanks beemr, I will update the wiki to use this. Wink

This original description of the MVC design pattern might interest you.
How to use Model-View-Controller (MVC)
#16

[eluser]beemr[/eluser]
CI 1.7 is out now, and the validation lib has been revamped for consistency and clarity. I modified this extension so that I might yet bask in its radiance. So far so good, but since I don't use Modular Extensions, I can't verify whether that connection is still intact. I've kept the $parent variable alive, though it required a slightly awkward tweak. Modular users, please take a look at my note on the wiki, and let us know how it goes.

Gracias,
#17

[eluser]lordmontie[/eluser]
Has anyone ever thought of doing something like this?

Create a new MY_Form_validation.php library file in your application/libraries folder:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {
    
    protected $account            = NULL;
    
    // Checks to see if username is avaliable
    protected function username($username)
    {
        $this->CI->load->model('accounts_model');
        
        $account = $this->CI->accounts_model->get_record(array('username' => $username));
        
        if (FALSE !== $account)
        {
            $this->set_message('_check_username', $username . $this->CI->lang->line('form_val_check_username'));
            return FALSE;
        }
        
        return TRUE;
    }
    
    // Checks to see if username exists in DB
    protected function username_exists($username)
    {
        $this->CI->load->model('accounts_model');
        
        $account = $this->CI->accounts_model->get_record(array('username' => $username));
        
        if (FALSE === $account || !is_array($account) || empty($account) || Accounts_Model::STATUS_ACTIVE != $account['status'])
        {
            $this->set_message('_check_username_exists', $username . $this->CI->lang->line('form_val_check_username_exists'));
            return FALSE;
        }
        
        $this->account = $account;
        return TRUE;
    }
    
    // Checks password with account auth_key
    protected function login($password)
    {
        $this->CI->load->library('Authentication');
        
        if (FALSE === $this->CI->authentication->login($this->account['username'], $this->account['auth_key'], $password))
        {
            $this->set_message('_check_master_auth_key', $this->CI->lang->line('form_val_check_master_auth_key'));
            return FALSE;
        }
        
        return TRUE;
    }
    
    // Checks invite code
    protected function invite($invite_id)
    {
        $this->CI->load->model('invites_model');
        
        $invite = $this->CI->invites_model->get_record(array('invite_id' => $invite_id));
        
        if (FALSE !== $invite)
        {
            $this->set_message('_check_invite', $invite_id . $this->CI->lang->line('form_val_check_invite'));
            return FALSE;
        }
        
        return TRUE;
    }
    
    // Checks invite code
    protected function invite_valid($invite_id)
    {
        $this->CI->load->model('invites_model');
        $this->CI->load->model('invite_accounts_model');
        
        $invite = $this->CI->invites_model->get_record(array('invite_id' => $invite_id));
        
        if (FALSE === $invite || !is_array($invite) || empty($invite))
        {
            $this->set_message('_check_invite_valid', $this->CI->lang->line('form_val_check_invite_valid'));
            return FALSE;
        }
        
        if ($this->CI->invite_accounts_model->count(array('invite_id' => $invite_id) >= $invite['allowed'])
        {
            $this->set_message('_check_invite_valid', $this->CI->lang->line('form_val_check_invite_valid'));
            return FALSE;
        }
        
        return TRUE;
    }
}

/* End of file MY_Form_validation.php */
/* Location: ./app/libraries/MY_Form_validation.php */


Then in your controller use the validation functions like so:
Code:
$this->form_validation->set_rules('username', $this->lang->line('field_username'), 'trim|required|valid_email|max_length[250]|username_exists');
        $this->form_validation->set_rules('password', $this->lang->line('field_password'), 'required|min_length[5]|login');
        
        if (FALSE === $this->form_validation->run())
        {
            $this->data['message'] = $this->session->flashdata('message');
            $this->data['error'] = $this->session->flashdata('error');
            $this->session->keep_flashdata('redirect');
            $this->load->view('account/login', $this->data);
            return;
        }

OR

Code:
$this->form_validation->set_rules('invite', $this->lang->line('field_invite'), 'trim|required|alpha_numeric|max_length[50]|strtolower|invite_valid');
        $this->form_validation->set_rules('username', $this->lang->line('field_username'), 'trim|required|valid_email|max_length[250]|username');
        $this->form_validation->set_rules('password', $this->lang->line('field_password'), 'required|min_length[5]|matches[passconf]');
        $this->form_validation->set_rules('passconf', $this->lang->line('field_passconf'), 'required');
        $this->form_validation->set_rules('firstname', $this->lang->line('field_firstname'), 'trim|max_length[50]');
        $this->form_validation->set_rules('lastname', $this->lang->line('field_lastname'), 'trim|max_length[50]');
        
        if (FALSE === $this->form_validation->run())
        {
            $this->load->view('account/create');
            return;
        }




Theme © iAndrew 2016 - Forum software by © MyBB