[eluser]Adam_R[/eluser]
What is wrong with this:
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
function MY_Form_validation()
{
parent::CI_Form_validation();
}
function valid_us_date($str)
{
return ( ! ereg("^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$", $str)) ? FALSE : TRUE;
}
function valid_exp_date($str)
{
return ( ! ereg("^[0-9]{1,2}/[0-9]{4}$", $str)) ? FALSE : TRUE;
}
function valid_phone($str)
{
return ( ! ereg("^[0-9]{3}-[0-9]{3}-[0-9]{4}$", $str)) ? FALSE : TRUE;
}
function valid_ssn($str)
{
return ( ! ereg("^[0-9]{3}-[0-9]{2}-[0-9]{4}$", $str)) ? FALSE : TRUE;
}
function valid_zip($str)
{
return ( ! ereg("^[0-9]{5}$", $str)) ? FALSE : TRUE;
}
}
I'm not able to get any validation from it base on called rules like
Code: <?php
$config['form_validation'] = array(
array(
'field' => 'firstname',
'label' => 'First Name',
'rules' => 'trim|required',
),
array(
'field' => 'lastname',
'label' => 'Last Name',
'rules' => 'trim|required',
),
array(
'field' => 'dob',
'label' => 'DOB',
'rules' => 'trim|required|valid_us_date',
),
array(
'field' => 'recaptcha_response_field',
'label' => 'Security Question',
'rules' => 'required|callback_check_captcha',
)
);
[eluser]solid9[/eluser]
How did you get this?
Code: class MY_Form_validation extends CI_Form_validation {
Isn't it like this,
Code: class MY_Form_validation extends CI_Controller {
What CI version you are using?
also review your constructor.
[eluser]CroNiX[/eluser]
@solid9, the form validation library is NOT a controller...
[eluser]CroNiX[/eluser]
is your MY_Form_validation.php file in
/application/libraries?
Yes, for CI v2+, constructor should be:
Code: function __construct()
{
parent::__construct();
}
[eluser]CroNiX[/eluser]
Also, none of your rules set an error message.
[eluser]Aken[/eluser]
If you're extending a library and don't need to use the constructor, then you don't need to define it again.
ereg() is also a deprecated function. use preg_match() instead.
[eluser]Adam_R[/eluser]
So I have switched code to
Code: function __construct()
{
parent::__construct();
}
and changed ereg to preg_match but still it doesn't work. If I move for an example validation for valid_us_date to the forms.php controller it works. No clue why is not extending core form validation.
[eluser]Adam_R[/eluser]
OK, I have got this to work with that code:
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
function valid_us_date($str)
{
if (! preg_match("/^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$/", $str)){
$this->set_message('valid_us_date', 'Invalid DOB format.');
return FALSE;
}
else {
return TRUE;
}
}
function valid_exp_date($str)
{
if (! preg_match("/^[0-9]{1,2}/[0-9]{4}$/", $str)){
$this->set_message('valid_exp_date', 'Invalid expiration date.');
return FALSE;
}
else {
return TRUE;
}
}
function valid_phone($str)
{
if (! preg_match("/^[0-9]{3}-[0-9]{2}-[0-9]{4}$/", $str)){
$this->set_message('valid_phone', 'Invalid phone number format.');
return FALSE;
}
else {
return TRUE;
}
}
function valid_ssn($str)
{
if (! preg_match("/^[0-9]{3}-[0-9]{2}-[0-9]{4}$/", $str)){
$this->set_message('valid_ssn', 'Invalid Social Security Number.');
return FALSE;
}
else {
return TRUE;
}
}
function valid_zip($str)
{
if (! preg_match("/^[0-9]{5}$/", $str)){
$this->set_message('valid_zip', 'Invalid ZIP code.');
return FALSE;
}
else {
return TRUE;
}
}
}
|