Welcome Guest, Not a member yet? Register   Sign In
Moving validation to models
#1

[eluser]JonoB[/eluser]
CI validation currently runs in the controller.

Is it possible to move this into Models? My main rationale is that in addition to my web front end, I would like to use the same models for a Rest API.

I am aware that the DataMapper ORM allows for model validation. Are there any other non-orm solutions that allow this?
#2

[eluser]Isern Palaus[/eluser]
Take a look to the base model by Jamie Rumbelow and the _run_validation() function by Dan Horrigan:

https://bitbucket.org/jamierumbelow/code...php#cl-517

I think it is very descriptive.

Wish it helps! ;-)
#3

[eluser]JonoB[/eluser]
[quote author="Isern Palaus" date="1300683671"]Take a look to the base model by Jamie Rumbelow and the _run_validation() function by Dan Horrigan:

https://bitbucket.org/jamierumbelow/code...php#cl-517

I think it is very descriptive.

Wish it helps! ;-)[/quote]

That looks like it will do the trick, thank you!
#4

[eluser]JonoB[/eluser]
I have now implemented validation in my models based on the link above, and using a MY_Model extends CI_Model methodology.

For the most part, this works well.

However, the one problem that I have not been able to solve is that my custom callbacks do not work. I eventually tracked this down to line 588 in the Form_validation library - basically the method_exists function does not return true.

Code:
if ( ! method_exists($this->CI, $rule))
{
    continue;
}

I dumped the $rule param to double check that the name of the callback function was correct.

I am missing something obvious here, but not quite sure what. Relevant excerpt from my model is below.

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Category_model extends MY_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    protected $validate = array(
               array(
                     'field'   => 'code',
                     'label'   => 'Code',
                     'rules'   => 'required|trim|callback_unique_code'
                  ),
               array(
                     'field'   => 'name',
                     'label'   => 'Name',
                     'rules'   => 'max_length[50]|trim'
                  )
            );


    /**
     * Callback function to determine if the code is not a duplicate
     *
     * @param string $code
     * @return boolean
     */
    public function unique_code($code)
    {
        return TRUE;
    }
}

?>
#5

[eluser]Madmartigan1[/eluser]
[quote author="JonoB" date="1300672592"]CI validation currently runs in the controller.[/quote]

CI Validation runs wherever you tell it to.
You can run it in your view if you really want to :-S

@callbacks: The form validation class uses the $CI variable internally to do callbacks, which is assigned to the current controller. Try the function below and pass the object you want to do callbacks to the first parameter.

Code:
// Like this: $this->form_validation->run($my_callback_object);

class MY_Form_validation extends CI_Form_validation {

    function run($obj = '', $group = '')
    {
        (is_object($obj)) AND $this->CI =& $obj;
        return parent::run($group);
    }
}

* Thank you wiredesignz
#6

[eluser]JonoB[/eluser]
Yep, thanks for the reply. I eventually figured out that callbacks, by default, only look in the controller. So, I extended Form_validation along the lines of http://codeigniter.com/wiki/MY_Form_vali...to_Models/

Callbacks now work properly!

Next step is that prepping doesnt work at all for some reason...working on that now.




Theme © iAndrew 2016 - Forum software by © MyBB