Welcome Guest, Not a member yet? Register   Sign In
Validating in a model
#3

[eluser]thurting[/eluser]
Here you go. The below is a basic example that I scraped from my code. To be honest, I haven't used CI in a while, so the following may be sloppy. I checked it though and it ran well. There are many ways to do it. This is just one. BTW, I had been using a custom validation class (the core one sucks - sorry), but replaced it with the CI version in this example.

First thing is to create an abstract class for all models to inherit from (put it in your application/libraries folder):

Code:
<?php

require_once BASEPATH . 'libraries/Model.php';

abstract class ModelAbstract extends Model
{
    protected $_table;

    public function __construct()
    {
        parent::Model();
        $this->_initValidation();
    }

    public function create($params)
    {
        if ($this->_validate()) {
            if ($this->db->insert($this->table, $params)) {
                return true;
            }

            return false;
        }

        return false;
    }

    protected function _validate()
    {
        return true;
    }

    private function _initValidation()
    {
        $this->load->library('validation');
    }
}

Second thing is to create out model that inherits from this base class (put it in your application/models folder):

Code:
<?php

require_once APPPATH . 'libraries/ModelAbstract.php';

class AdminModel extends ModelAbstract
{
    protected $table = 'admins';

    // OVERRIDE SUPER
    protected function _validate()
    {
        $rules['username'] = 'required|alpha';
        $this->validation->set_rules($rules);

        $success = $this->validation->run();

        return $success;
    }
}

Third thing is our controller (put it in your application/controllers folder):

Code:
<?php

class IndexController extends Controller
{
    public function __construct()
    {
        parent::Controller();
    }

    public function index()
    {
        $this->load->model('AdminModel', 'admin');

        $_POST['username'] = 42;

        if ($this->admin->create($_POST)) {
            echo 'it worked - do something';
        } else {
            echo 'it failed - do something - like show any errors';
        }
    }
}

Notice that the validation instance is instantiated during model construction. This can be altered. In any case, the main part is the _validate() method. The default behavior is to return true. However, as it is protected, each subclass should override this method to provide specific validation behavior. As you can see with the create() method, before a query is run we run a conditional with _validate() and proceed accordingly. All queries through the model should be encapsulated in conditionals within the constructor, so when we encounter an error we can handle it properly. There are plenty of other ways to accomplish this, but the above is the simplest to understand.


Messages In This Thread
Validating in a model - by El Forum - 02-01-2008, 07:28 PM
Validating in a model - by El Forum - 02-01-2008, 08:26 PM
Validating in a model - by El Forum - 02-01-2008, 09:30 PM
Validating in a model - by El Forum - 02-01-2008, 09:36 PM
Validating in a model - by El Forum - 02-01-2008, 09:39 PM
Validating in a model - by El Forum - 02-02-2008, 01:42 AM
Validating in a model - by El Forum - 02-02-2008, 02:08 AM
Validating in a model - by El Forum - 02-02-2008, 02:38 AM



Theme © iAndrew 2016 - Forum software by © MyBB