Welcome Guest, Not a member yet? Register   Sign In
MY Form validation - Simple Callbacks into Models
#1

[eluser]janogarcia[/eluser]
IMPORTANT! DON'T USE THIS

This code is from June 2009. It is as of now (December 2011) untested and unmantained.

I recommend using a simpler solution that doesn’t imply hacking any core file:

CI 2.1.0 form validation external callbacks via the newly available param (by skunkbad)
http://ellislab.com/forums/viewthread/205469/

Description

I needed a simple way of storing Validation rules callbacks into Models. This had to be made with minimal modifications to the Form validation library and resembling as much as possible the CI conventions for validation rules.

Once installed the library you will only need to append _model[your_model_name] to your validation rule callback name, where your_model_name indicates the model which contains the callback function.

If no model is specified, it is assumed that the callback resides in the Controller code (as CI behaves by default).

Example

Let’s say you want to assign the _validate_authcode callback to the authcode form field:

callback__validate_authcode
‘_validate_authcode’ callback is located in the requested Controller (the default CI’s callback behavior)
Code:
$this->form_validation->set_rules('authcode', 'Authorization Code', 'callback__validate_authcode');


callback__validate_authcode_model[admin]

‘_validate_authcode’ callback is located in the ‘admin’ Model
Code:
$this->form_validation->set_rules('authcode', 'Authorization Code', 'callback__validate_authcode_model[admin]');


callback__validate_authcode_model[public]
‘_validate_authcode’ callback is located in the ‘public’ Model
Code:
$this->form_validation->set_rules('authcode', 'Authorization Code', 'callback__validate_authcode_model[public]');


Check the full documentation on the wiki
#2

[eluser]Johan André[/eluser]
I actually think this has been done before (by Wiredesign).
Nice contribution though.
I really don't get why CI wants the validation to be made in the controller.
The model seems to be the right place to do it...
#3

[eluser]janogarcia[/eluser]
Thanks Johan

The Wiredesignz version is for the now deprecated Validation library. Additionaly, he added some logic and had to override the "run" method in order to make it compatible with Modular Extensions HMVC (if I'm not wrong). beemr took the Wiredesignz library an updated it to make it work with the new Form Validation library.

These are the solutions I was aware of. However I wanted to follow a more "CI like" validation rule naming convention:

(wiredesignz) callback_users_model->is_unique[username] VS (simple callbacks) callback_is_unique_model[users]

With this last convention it is easier to switch between the Controller callback (callback_is_unique) and the Model callback (callback_is_unique_model[users]).

And I don't needed to support HMVC, so the code would be much simpler.

I hope it is a bit clearer now what was my original intention.
#4

[eluser]Johan André[/eluser]
It's alot clearer! Smile

Thanks for the contribution! Will try it out asap.
#5

[eluser]wiredesignz[/eluser]
I had already posted a fix to make Callbacks into Modules and Models with CI 1.7.1 and the Form_validation library.

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

class MY_Form_validation extends CI_Form_validation
{
    function run($module = NULL, $group = '') {        
        if (is_object($module)) $this->CI =& $module;
        return parent::run($group);
    }
}

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

Simply pass the "object" (HMVC module or model) to use for callbacks into the run method.
Code:
$this->form_validation->run($object);
#6

[eluser]Johan André[/eluser]
I've tried it and it works very good!

Just some thoughts:



Code:
// Maybe make it check for callbacks like this instead
$this->form_validation->set_rules('fieldname', 'Human name', 'required|callback__my_callback[categories_model]');

// Instead of this
$this->form_validation->set_rules('fieldname', 'Human name', 'required|callback__my_callback_model[categories_model]');

I personally name my models with _model added to the end of the modelname.

Also, someone created an extension to validate the $_FILES-array with form_validation-class. I think your contribution should merge with that one. I did a quick and dirty merge which has not been tested 100%, but if I'll find it usable, I'll post it (if you let me do it ofcourse).

Great contribution anyway!
Regards
Johan
#7

[eluser]skunkbad[/eluser]
I had been working on a solution to this problem yesterday, and had not seen this thread until I wasted a lot of my time! I'm going to use your solution, because it makes for a very clean controller. Thanks for sharing.
#8

[eluser]dallen33[/eluser]
Life saver! Thank you!
#9

[eluser]umbongo[/eluser]
I am getting the following error
Code:
Message: Undefined property: Account::$val

Filename: libraries/MY_Form_validation.php

Line Number: 171
Where 'account' is the name of the controller calling the model 'val' like so;
Code:
class Account extends Controller {
function update_user(){
$this->form_validation->set_rules('user_name', 'name', 'required|callback_is_text_model[val]');

if($this->form_validation->run())...

any ideas what the problem is / fix?
#10

[eluser]janogarcia[/eluser]
Hi umbongo, Did you remeber to load the "val" model before running the validation?




Theme © iAndrew 2016 - Forum software by © MyBB