Welcome Guest, Not a member yet? Register   Sign In
Trying to extend the Validation class issues
#1

[eluser]Unknown[/eluser]
I tried to extend the Validation class by putting this code in /application/libraries and called it MY_Validation.php. When I try to include the validation class the method in this class comes back with an error that it does not exist. Please be patient with me I am very new to CI and need some help!

This is the extension: in /application/libraries
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Validation extends CI_Validation {

function My_Validation()
{
parent::CI_Validation();
}

function set_default_values($data, $value=NULL)
{
if (is_array($data) == TRUE)
{
foreach($data as $field => $value)
{
$this->$field = $value;
$_POST[$field] = $value;
}
}
else
{
$this->$data = $value;
$_POST[$data] = $value;
}
}
}
?>

This is the controller:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Formtest extends Controller {

function Formtest()
{
parent::__construct();
}

function index()
{
$this->load->library('validation');
$this->load->helper('form');
$rules['text'] = 'required';
$rules['option'] = 'required';
$rules['select'] = 'required';
$rules['radio'] = 'required';
$this->validation->set_rules($rules);

$fields['text'] = 'text field';
$fields['select'] = 'select field';
$fields['check'] = 'check box field';
$fields['radio'] = 'radio field';
$this->validation->set_fields($fields);

if (count($_POST) == 0)
{ // For initial load set defualt values
$this->validation->set_default_values('text','hello world');
$this->validation->set_default_values('select', 'select2');
$this->validation->set_default_values('check', 'check2');
$this->validation->set_default_values('radio', '3');
}

if ($this->validation->run() == FALSE)
{
$this->load->view('formtest_view');
}
else
{
$this->load->view('formtest_view');
}
}

}
?>

When trying to access $this->validation->set_default_values('text','hello World') I get the following error:
Fatal error: Call to undefined method CI_Validation:Confusedet_default_values() in /home/hector/www/test/ce/system/application/controllers/formtest.php on line 28

Note: I followed the user guide to follow this and don't understand why the method or my extension is not being loaded by CI ver. 1.6.3

Thanks in advance!
#2

[eluser]xavieremerson[/eluser]
hi...

I'm also new to it...
k
here you are not loaded your validation class
use ->load->library('my_validation')
#3

[eluser]WanWizard[/eluser]
You shouldn't use the validation library, it's deprecated since CI 1.7.0, and it's removed in CI 2.0. Use the Form validation library instead.

Your extension isn't loaded (the error refers to CI_Validation), and this is because the extension prefix is "MY_", and not "My_".




Theme © iAndrew 2016 - Forum software by © MyBB