Welcome Guest, Not a member yet? Register   Sign In
Extended MY_Form_validation not working
#1

[eluser]a_bains[/eluser]
Hello,

I am using CodeIgniter 2.0 and I am trying to extend the Form Validation library.

In my controller I am loading the library like this: $this->load->library('form_validation');

I am able to use my extended function when I place the MY_Form_validation.php file into the libraries folder. But when I put into the applications/core folder it doesn't recognize my extended function... I would like to keep all the MY_ classes in the core folder because I think thats the what the best practice is and it keeps my files organized.

Here is the MY_Form_validation.php code:

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

/**
* MY_Form_validation Class
*
* Extends Form_Validation library
*
* Allows for custom error messages to be added to the error array
*
* Note that this update should be used with the
* form_validation library introduced in CI 1.7.0
*/
class MY_Form_validation extends CI_Form_validation {

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

    // --------------------------------------------------------------------

    /**
     * Set Error
     *
     * @access  public
     * @param   string
     * @return  bool
    */  

    function set_error($error = '')
    {
        if (empty($error))
        {
            return FALSE;
        }
        else
        {
            $CI =& get_instance();

            $CI->form_validation->_error_array['custom_error'] = $error;

            return TRUE;
        }
    }

}

?>
#2

[eluser]Jaketoolson[/eluser]
Correct me if I'm wrong someone, but if you want to keep the file in the 'applications/core' folder and because this is extending a 'Library', all you should have to do is include the file manually AFTER you've included the form_validation library.

Code:
$this->load->library('form_validation');
require_once(APPPATH.'core/MY_Form_validation.php');

// this should now work
$this->form_validation->set_error('help me!!');
#3

[eluser]InsiteFX[/eluser]
Jaketoolson is right, the reason it does not work when you place it in application/core is because it is not a core library!

So you will need to manually load it as Jaketoolson mentioned above!

InsiteFX
#4

[eluser]a_bains[/eluser]
Awesome, thanks for that explanation. I just took a look at the system/ folder and now I see which are core and which are libraries.
#5

[eluser]Johan André[/eluser]
Actually, you should place it in application/libraries (if nothing changed in the last couple of weeks).
#6

[eluser]Jaketoolson[/eluser]
He doesn't want to place it in the libraries folder, hence the reason for his post.




Theme © iAndrew 2016 - Forum software by © MyBB