[eluser]Grahack[/eluser]
[quote author="Pete Smith" date="1183842807"]Here's what I'm doing now:
Code:
function _ccdata($str)
{
if ($_POST['invoice'] == '1') {
return TRUE;
}
else
{
if ($str == '')
{
$this->validation->set_message('_ccdata', 'The %s field cannot be blank');
return FALSE;
}
else
{
return TRUE;
}
}
}
But this isn't very robust...
I'd like to be sure the cc number is numeric, for instance. So I was thinking if there was a way to call CI validate here instead of using, for example,
($str == '')
[/quote]
Why isn't it "robust" ?
For the call of CI's validation, I have an idea, see below after the 'extension' explanation.
Quote:Maybe if I set up a completely new set of validation rules inside this callback function? Or maybe I'm worrying too much, since the store's credit card processor will flag problems here itself when the system tries to authorize the card.
Don't know about your processes, I guess it's better to notice the user as early as possible.
Quote:Quote:Note: extending the Validation class is a very wise thing to do, instead of putting the callback in the controller.
Can you elaborate on this a little? What are the drawbacks of having the callback in the controller? Thanks for any advice you can offer.
I guess you don't want to have duplicate code if ever you need to do the same validation in another controller.
Moreover, this trick allows you to omit the 'set_message' function, just add a line with the relevant key (function name) in a lang file, that you will load in the constructor.
I don't know if this is explained somewhere else. It's quite basic but very handy for the validation custom processes.
It goes like this:
create a
MY_Validation.php file in
system/application/libraries folder, with
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access permitted.');
class MY_Validation extends CI_Validation {
function My_Validation()
{
parent::CI_Validation();
// here you could load your lang file
}
// put your new functions here
}
You'll have new validation functions as if they were in the base CI
Validation.php file.
Now I think (not sure, but you'll test for me), that you can call
is_numeric() or any validation function with
$this->function_name() since your class extends the base valisation one.
$this->function_name() will replace the
set_message() +
return BOOL thing.