Validation callbacks into Models |
[eluser]wiredesignz[/eluser]
I have created a Validation library modification which allows callbacks into Models. Save the existing Validation library as MY_validation with only the original run() function in it. Alter the class identifier to MY_ validation and extend it off CI_Validation. Change this code appropriatley: Code: // Call the function that corresponds to the rule my validation callback declaration then becomes: Code: $rules['username'] => ‘trim|required|callback_users_model->is_unique[username]’;
[eluser]xwero[/eluser]
It's great idea but i think it should be worked out some more. I think following format offers more flexibility. Code: $rules['username'] = 'trim|required|db_check[users,is_unique,password]' Code: function db_check($str,$param) edit : the model method Code: function is_unique($username,$arr)
[eluser]xwero[/eluser]
I've been thinking about this functionality the weekend and i think the possible combinations and the error checks are too much for a standard validation. The biggest problem with the current validation code is the very limited functionality of the rules, they only can output a boolean. Collecting the error string in the rules would help a lot. I'm busy hacking the validation class and this is something i will add.
[eluser]dawnerd[/eluser]
Oh please implement this. I'm tired of the way callbacks are setup. Also, how come the callback can't be prefixed with an underscore, say function _check_user rather than check_user.
[eluser]Chris Newton[/eluser]
I use callbacks with underscores all the time. You just have to call them like this; callback__myfunction
[eluser]dawnerd[/eluser]
Strange, I tried that and it failed. I'll give it another shot though.
[eluser]Chris Newton[/eluser]
The private callbacks can't be called if you're using the callbacks in libraries though. Libraries rely on the callback function to exist and be public in the calling controller.
[eluser]beemr[/eluser]
I've been able to use this in my library since wiredesignz gifted it to us. I added instructions to the wiki on how a library can call it. The instructions are for the latest revision which requires that the object gets passed into it own run() call. I'm also using the ActiveRecord_Class_Mod, so I added an example of an "is_unique" callback for use with that library. This validation enhancement makes a great complement for it.
[eluser]beemr[/eluser]
@mahuti Whoops. I didn't realize your post was off-topic. Anyhoo, it's still a great library to bring into ActiveRecord_Class_Mod. |
Welcome Guest, Not a member yet? Register Sign In |