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

[eluser]Référencement Google[/eluser]
Hi,

From a MVC point of view, it seems a correct idea to use a model to validate forms.
Has someone already done this? If yes, could you post some code examples on how to do that?
#2

[eluser]thurting[/eluser]
I agree with you and have done this in the past. Let me look through my code and post it for you.
#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.
#4

[eluser]Référencement Google[/eluser]
That's a very intersting way to do. I will give it some tests right now.
Thanks Thurting !
#5

[eluser]thurting[/eluser]
Enjoy. Use it as a jumping off point.

BTW, here is a simple schema you can use to test:

create table admins (
id int unsigned not null auto_increment,
username char(40) not null,
primary key (id)
);
#6

[eluser]sikkle[/eluser]
Let's be honest, this is an approach indeed, but humm not so sure about how this can be structured in application development.

i'm still wondering if there any way to create something more "method related"

i mean, anyone already make some test to build validation as a model method, no extention, just another method build someway on model ?
#7

[eluser]wiredesignz[/eluser]
I posted nearly a week ago, this change to the validation library allows callbacks into models or libraries.

http://ellislab.com/forums/viewthread/69797/
#8

[eluser]tomcode[/eluser]
edit : sorry, posted in the wrong thread




Theme © iAndrew 2016 - Forum software by © MyBB