Welcome Guest, Not a member yet? Register   Sign In
CI 2.0 - extended form_validation does not load config file
#1

[eluser]solidrockit[/eluser]
Hello

I know this is an already commented issue but I am not able to get it working although I have read the doc and many post in the forum:


1) We have a extended form_validation class and the added valid_date method works great

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

class MY_Form_validation extends CI_Form_validation {

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

  /**
   * Valid date
   * Checks if input is a valid date.
   * @param string $str
   * @return bool
   */
  function valid_date($str)
  {
    if (preg_match('/([0-3][0-9])\/([0-9]{1,2})\/([1-2][0-9]{3})/', $str, $date)) {
    //if (preg_match('/([0-3][0-9])\/([0-1][0-9])\/([1-2][0-9][0-9][0-9])/', $str, $date)) {
      //bool checkdate ( int $month , int $day , int $year )
      return checkdate($date[2], $date[1], $date[3]);
    }
    else{
      return false;
    }
  }
  
    // END Form Validation Class
}
/* End of file MY_Form_validation.php */
/* Location: ./application/libraries/MY_Form_validation.php */




2) We have the config file in .\application\config\form_validation.php something like this
Code:
$config = array(
                 'validate' => array(
                                    array(
                                            'field' => 'username',
                                            'label' => 'Usuario',
                                            'rules' => 'trim|required|min_length[4]|max_length[40]'
                                         ),
                                    array(
                                            'field' => 'pwd',
                                            'label' => 'Clave',
                                            'rules' => 'trim|required|min_length[6]|max_length[40]'
                                         )
                                    )
);


3) In the controller we have the
Code:
$this->load->library('form_validation');
but when we do the
Code:
if ($this->form_validation->run('validate')==FALSE){
the config file seems not to be loading the rules.


4) I have already try to autoload the config file but no way.

5) If I leave the rules inside the controler(I mean not using the config file), the
Code:
if ($this->form_validation->run()==FALSE){
validation works fine.




Am I missing something or is there something tricky?

Many thanks
#2

[eluser]kirkaracha[/eluser]
This might help:
Form_validation class fails to locate rule-sets for certain URIs
#3

[eluser]wiredesignz[/eluser]
@solidrockit, Your constructor overload is stopping the CI_Form_validation constructor from processing the config array().
#4

[eluser]Chimpski[/eluser]
In the controller use:

$this->form_validation->set_rules($this->config->item('name_of_form_validation_rule', 'form_validation'));

if ($this->form_validation->run() == true)
{
//do stuff
}
#5

[eluser]solidrockit[/eluser]
Many thanks for your replies.


This is what works for me in ..\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 {

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

Now the $config file is being loaded.
#6

[eluser]Chimpski[/eluser]
I would prefer that method to directly calling it in with the: $this->form_validation->set_rules($this->config->item(‘name_of_form_validation_rule’, ‘form_validation’));

Did you have to do anything to make the MY_Form_validation.php get called? Added the file with your code and received an error:
Call to undefined method CI_Form_validation::CI_Form_validation()

I can't seem to find anything about this error.
#7

[eluser]Chimpski[/eluser]
Oops that was a different MY. This is the errors I get with you MY_Form_validation:


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

class MY_Form_validation extends CI_Form_validation {

    function __construct($config)
    {
        parent::__construct($config);
    }  
}
?>

A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for MY_Form_validation::__construct(), called in C:\xampp\htdocs\ci2\system\core\Loader.php on line 949 and defined

Filename: libraries/MY_Form_validation.php

Line Number: 5
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: config

Filename: libraries/MY_Form_validation.php

Line Number: 7

Not quite sure. I must be missing something. (Just learning CodeIgniter)
#8

[eluser]steelaz[/eluser]
This is PHP related error (warning), not CI. Try this:

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

class MY_Form_validation extends CI_Form_validation {

    function __construct($config = array())
    {
        parent::__construct($config);
    }  
}
?>
#9

[eluser]Chimpski[/eluser]
I fixed the issue with your code by removing the set rules call altogether and removing the $config from the ->run($config).

Thanks for your help.




Theme © iAndrew 2016 - Forum software by © MyBB