• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can not extend Form Validator core library

#1
[eluser]Neerav[/eluser]
Hello Guys,

I am using codeigniter version 2.0.

I have followed this instruction to extend core form validation library.


I try placing my new file named 'MY_Form_validation.php' inside 'application/libraries' and 'application/core' directory respectively. But neither one is working for me. I can not get my valid_url function working. Following is the content of my 'MY_Form_validation.php' file,

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

/**
* MY_Form_Validation Class
*
* Extends Validation library
*
* Adds one validation rule, valid_url to check if a field contains valid url
*/

class MY_Form_validation extends CI_Form_validation
{

    function __construct()
    {

        parent::__construct();
    }

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

    /**
     * valid_url
     *
     * @access    public
     * @param    field
     * @return    bool
     */
    function valid_url($field)
    {
        $CI =& get_instance();

        $CI->form_validation->set_message('valid_url', 'The %s field must contain a valid url.');

        return (!preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $field)) ? FALSE : TRUE;
    }
}

// end file

I am loading form validation library in my controller and default validation methods are working fine.

Please advise me what's wrong with this or am I missing any steps.

Thanks in advance.

Regards,
Neerav

#2
[eluser]InsiteFX[/eluser]
The should go into application/libraries
If it does not work then something else is wrong check the CodeIgniter User Guide change log to see what has been changed.

If the Class you are extending is in system/core then your Class goes into application/core

If the Class you are extending is in system/libraries then your Class goes into application/libraries

InsiteFX

#3
[eluser]patwork[/eluser]
You don't need to use get_instance() when you're in extended class. This will work:

Code:
$this->set_message('valid_url', 'The %s field must contain a valid url.');

If you're not sure MY_Form_validation is properly loaded, the best place to check it is it's constructor. Try to add something like:

Code:
function __construct()
{
    echo "HELLO FROM MY_FORM_VALIDATION";
    parent::__construct();
}

Only for debug purposes, of course Wink

#4
[eluser]Neerav[/eluser]
Hi InsiteFX,

thanks for reply, I now have it in 'application/libraries' but its not working. I read change log, but I do not find anything relevant there.

Hi Patwork,

thanks for reply and advise, Its really helpful.

I try to check if the library is loading as per your suggestion, but its not loading at all.

I read somewhere that I need to place it in 'system/libraries' directory, but I don't think its true, is it?

Please advise.

Regards,
Neerav Dobaria

#5
[eluser]InsiteFX[/eluser]
Code:
// This is wrong!
<?php if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

// should be like this!
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

InsiteFX

#6
[eluser]Neerav[/eluser]
Hi InsiteFx,

They both are identical as far as end result concerned.

Thanks,
Neerav Dobaria

#7
[eluser]InsiteFX[/eluser]
I just stick to the CodeIgniter standards this way I do not run into other problems.

Turn on your logging in application/config/config.php
Code:
$config['log_threshold'] = 4;
Then view your log file in application/logs

InsiteFX

#8
[eluser]Neerav[/eluser]
Hi InsiteFX,

agree, I should too stick to Codeigniter standards.

I set log threshold to 4 as per your suggestion. It says Form Validation class loaded but it does not tell which one. See below my log,

Code:
DEBUG - 2011-04-12 11:05:13 --> Config Class Initialized
DEBUG - 2011-04-12 11:05:13 --> Hooks Class Initialized
DEBUG - 2011-04-12 11:05:13 --> Utf8 Class Initialized
DEBUG - 2011-04-12 11:05:13 --> UTF-8 Support Enabled
DEBUG - 2011-04-12 11:05:13 --> URI Class Initialized
DEBUG - 2011-04-12 11:05:13 --> Router Class Initialized
DEBUG - 2011-04-12 11:05:13 --> Output Class Initialized
DEBUG - 2011-04-12 11:05:13 --> Input Class Initialized
DEBUG - 2011-04-12 11:05:13 --> Global POST and COOKIE data sanitized
DEBUG - 2011-04-12 11:05:13 --> Language Class Initialized
DEBUG - 2011-04-12 11:05:13 --> Loader Class Initialized
DEBUG - 2011-04-12 11:05:14 --> Helper loaded: url_helper
DEBUG - 2011-04-12 11:05:14 --> Helper loaded: form_helper
DEBUG - 2011-04-12 11:05:14 --> Helper loaded: language_helper
DEBUG - 2011-04-12 11:05:14 --> Helper loaded: sidebar_helper
DEBUG - 2011-04-12 11:05:14 --> Language file loaded: language/english/mns_lang.php
DEBUG - 2011-04-12 11:05:14 --> Database Driver Class Initialized
DEBUG - 2011-04-12 11:05:14 --> Session Class Initialized
DEBUG - 2011-04-12 11:05:14 --> Helper loaded: string_helper
DEBUG - 2011-04-12 11:05:14 --> Session routines successfully run
DEBUG - 2011-04-12 11:05:14 --> Controller Class Initialized
DEBUG - 2011-04-12 11:05:14 --> Pagination Class Initialized
DEBUG - 2011-04-12 11:05:14 --> Encrypt Class Initialized
DEBUG - 2011-04-12 11:05:14 --> Model Class Initialized
DEBUG - 2011-04-12 11:05:14 --> Model Class Initialized
DEBUG - 2011-04-12 11:05:14 --> Form Validation Class Initialized
DEBUG - 2011-04-12 11:05:14 --> Language file loaded: language/english/form_validation_lang.php
DEBUG - 2011-04-12 11:05:14 --> File loaded: application/views/header.php
DEBUG - 2011-04-12 11:05:14 --> File loaded: application/views/navigation.php
DEBUG - 2011-04-12 11:05:14 --> File loaded: application/views/message.php
DEBUG - 2011-04-12 11:05:14 --> File loaded: application/views/sidebar.php
DEBUG - 2011-04-12 11:05:14 --> File loaded: application/views/footer.php
DEBUG - 2011-04-12 11:05:14 --> File loaded: application/views/passwords/add.php
DEBUG - 2011-04-12 11:05:14 --> Final output sent to browser
DEBUG - 2011-04-12 11:05:14 --> Total execution time: 0.0979

Do you find anything from it?

Regards,
Neerav Dobaria

#9
[eluser]patwork[/eluser]
Problem must be somewhere else then. Your library must be in:

/application/libraries/MY_Form_validation.php

With something like this:

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

class MY_Form_validation extends CI_Form_validation {

    public function __construct($rules = array())
    {
        log_message('debug', '*** Hello from MY_Form_validation ***');
        parent::__construct($rules);
    }

}

you should get log like this:

Code:
DEBUG - 2011-04-12 11:31:44 --> Config Class Initialized
DEBUG - 2011-04-12 11:31:44 --> Hooks Class Initialized
DEBUG - 2011-04-12 11:31:44 --> Utf8 Class Initialized
DEBUG - 2011-04-12 11:31:44 --> UTF-8 Support Enabled
DEBUG - 2011-04-12 11:31:44 --> URI Class Initialized
DEBUG - 2011-04-12 11:31:44 --> Router Class Initialized
DEBUG - 2011-04-12 11:31:44 --> No URI present. Default controller set.
DEBUG - 2011-04-12 11:31:44 --> Output Class Initialized
DEBUG - 2011-04-12 11:31:44 --> Security Class Initialized
DEBUG - 2011-04-12 11:31:44 --> Input Class Initialized
DEBUG - 2011-04-12 11:31:44 --> Global POST and COOKIE data sanitized
DEBUG - 2011-04-12 11:31:44 --> Language Class Initialized
DEBUG - 2011-04-12 11:31:44 --> Loader Class Initialized
DEBUG - 2011-04-12 11:31:44 --> Controller Class Initialized
DEBUG - 2011-04-12 11:31:44 --> *** Hello from MY_Form_validation ***
DEBUG - 2011-04-12 11:31:44 --> Helper loaded: form_helper
DEBUG - 2011-04-12 11:31:44 --> Form Validation Class Initialized
DEBUG - 2011-04-12 11:31:44 --> File loaded: application/views/welcome_message.php
DEBUG - 2011-04-12 11:31:44 --> Final output sent to browser
DEBUG - 2011-04-12 11:31:44 --> Total execution time: 0.3524

#10
[eluser]Neerav[/eluser]
Hi Patwork,

Its really weird. We got everything in common, except the log file.

I'll have to debug thoroughly to find out whats the issue.

I'll post issue here as soon as I find it.

Meanwhile, If you have other suggestion, do let me know. I will be happy to try it as well.

Regards,
Neerav Dobaria


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.