Part of the EllisLab Network
   
 
MY Form validation - Simple Callbacks into Models
Posted: 21 July 2009 10:53 AM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  60
Joined  05-17-2008

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)

$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

$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

$this->form_validation->set_rules('authcode', 'Authorization Code', 'callback__validate_authcode_model[public]');


Check the full documentation on the wiki

 Signature 

margenn.com | web studio in san sebastian $CI = new $class();

Profile
 
 
Posted: 21 July 2009 01:35 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  404
Joined  03-28-2008

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…

 Signature 

———————————————————————————————————————————-
Imac 24” C2D 3.06Ghz / 4GB RAM / Geforce 8800GS 512MB
Macbook Pro 15” C2D 2.53Ghz / 4GB RAM / NVIDIA GeForce 9400M + 9600M GT 512MB
iPhone 3G 16Gb Black

http://www.rockkarusellen.se

Profile
 
 
Posted: 21 July 2009 02:35 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  60
Joined  05-17-2008

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.

 Signature 

margenn.com | web studio in san sebastian $CI = new $class();

Profile
 
 
Posted: 21 July 2009 02:45 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  404
Joined  03-28-2008

It’s alot clearer! smile

Thanks for the contribution! Will try it out asap.

 Signature 

———————————————————————————————————————————-
Imac 24” C2D 3.06Ghz / 4GB RAM / Geforce 8800GS 512MB
Macbook Pro 15” C2D 2.53Ghz / 4GB RAM / NVIDIA GeForce 9400M + 9600M GT 512MB
iPhone 3G 16Gb Black

http://www.rockkarusellen.se

Profile
 
 
Posted: 21 July 2009 06:18 PM   [ Ignore ]   [ # 4 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  2634
Joined  06-10-2007

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

<?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.

$this->form_validation->run($object);
 Signature 

URI Language Identifier | Modular Extensions - PHP5 | Modular Separation - PHP5 | Widget plugin | Access Control library

Profile
 
 
Posted: 03 August 2009 09:16 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  404
Joined  03-28-2008

I’ve tried it and it works very good!

Just some thoughts:

// 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

 Signature 

———————————————————————————————————————————-
Imac 24” C2D 3.06Ghz / 4GB RAM / Geforce 8800GS 512MB
Macbook Pro 15” C2D 2.53Ghz / 4GB RAM / NVIDIA GeForce 9400M + 9600M GT 512MB
iPhone 3G 16Gb Black

http://www.rockkarusellen.se

Profile
 
 
Posted: 04 August 2009 02:25 PM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  365
Joined  05-17-2009

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.

 Signature 

Brian’s Web Design - Temecula
Community Cart
Community Auth
UPS Shipping Rates Library

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120462 Total Logged-in Users: 38
Total Topics: 126539 Total Anonymous Users: 2
Total Replies: 665367 Total Guests: 304
Total Posts: 791906    
Members ( View Memberlist )