CodeIgniter Forums
extending CI_form_validation don't work - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: extending CI_form_validation don't work (/thread-57536.html)



extending CI_form_validation don't work - El Forum - 03-21-2013

[eluser]Unknown[/eluser]
So i extended my form_validation library.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Lt_form_validation extends CI_Form_validation {
    
    public function __construct() {
        parent::__construct();
    }

    public function clear_field_data() {

        $this->_field_data = array();
        return $this;
    }
    
}

but when i try to reach it, i get error:
Code:
Fatal error: Call to undefined method CI_Form_validation::clear_field_data()

I trying to call like that
Code:
$this->form_validation->clear_field_data();

What i am doing wrong?


extending CI_form_validation don't work - El Forum - 03-21-2013

[eluser]TheFuzzy0ne[/eluser]
If you want to extend the form validation class, you need to call your file MY_Form_validation.php, and the class name should be MY_Form_validation.

You can do it your way too, but you'll need to do it like this:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include BASEPATH.'libraries/Form_validation'.EXT;

class Lt_form_validation extends CI_Form_validation {
    ...

Then you'd need to load your library like this:
Code:
$this->load->library('lt_form_validation');

And then call the methods like this:
Code:
$this->lt_form_validation->clear_field_data();



extending CI_form_validation don't work - El Forum - 03-21-2013

[eluser]CroNiX[/eluser]
/application/libraries/MY_Form_validation.php

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {
    
    public function __construct() {
        parent::__construct();
    }

    public function clear_field_data() {

        $this->_field_data = array();
        return $this;
    }
    
}

Code:
$this->load->library('form_validation');  //loads form validation and extends it with MY_form_validation automatically.
$this->form_validation->clear_field_data(); //call your new method

See the Extending Native Libraries section for more info.