Welcome Guest, Not a member yet? Register   Sign In
Need help extending the CI_Form_validation class
#1

[eluser]heat23[/eluser]
I've spent several hours trying to get this working many CI Forum/ Google searches later, here I am... Here is what I did so far, please let me know what I am doing wrong. By the way, I am using PHP 4 and CI 1.7.1

1) I created the file MY_Form_validation.php in the /application/libraries folder
2) Inside this file, I have:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include_once(BASEPATH.'libraries\Form_validation.php');

class MY_Form_validation extends CI_Form_validation {
    

  function MY_Form_validation($config = array())
    {
      parent::CI_Form_validation($config);
      $this->CI =& get_instance();
    }


   function type_check($str)
{
        echo "111111";
    if($str==0){
       $CI->validation->set_message('trade_type_check','You must select a type');
       return false;
    }
    return true;
    }
}

3) In my controler which contains the form, I have:
Code:
$this->load->library(array('form_validation'));
$rules['type'] = "type_check";
$this->form_validation->set_rules($rules);
$fields['type'] = 'Type';
$this->form_validation->set_fields($fields);

        if($this->input->post('submit')){
            if ($this->form_validation->run() == FALSE) //Fail
            {
                echo 'Form had errors';
            }
            else //Success
            {
                echo 'Form submitted Successfully';
            }
        }

4) When I submit the form, I get the error
Code:
Fatal error: Call to undefined method: my_form_validation->set_fields() in C:\www\ci\system\application\controllers\c_eval.php on line 74
Which corresponds to the line:
$this->form_validation->set_fields($fields);

Can someone tell me what I am doing wrong here?
#2

[eluser]lifo101[/eluser]
I believe set_fields() is part of the old Validation class and does not exist in the newer Form_Validation class.
#3

[eluser]Pascal Kriete[/eluser]
You're mixing up the old and new validation libraries. The new one (form_validation) doesn't use set fields.

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

class MY_Form_validation extends CI_Form_validation {

    function MY_Form_validation($config = array())
    {
        parent::CI_Form_validation($config);
    }
    
    function type_check($str)
    {
        if ($str == 0)
        {
            $this->set_message('type_check','You must select a type');
            return FALSE;
        }
        return TRUE;
    }
}
/* End of file MY_form_validation.php */
/* Location: ./system/application/libraries/MY_form_validation.php */

And the controller:
Code:
$this->form_validation->set_rules('type', 'Type', 'required|type_check';
#4

[eluser]heat23[/eluser]
Thanks Pascal, that did the trick!!




Theme © iAndrew 2016 - Forum software by © MyBB