Welcome Guest, Not a member yet? Register   Sign In
Callback is failed in Form Validation
#11

[eluser]CyrusTC[/eluser]
[quote author="InsiteFX" date="1329137432"]You have to use the CI Super Object in a library class!

His problem is that he is calling the Form_validation wrong and needs to read the User Guide.
[/quote]
Sorry i dont understand with it.
Could you show me some examples for this??
#12

[eluser]InsiteFX[/eluser]
I showed you above how to do it post #7.
#13

[eluser]CyrusTC[/eluser]
[quote author="InsiteFX" date="1329142695"]I showed you above how to do it post #7.
[/quote]
Is there any difference between $this->CI and $CI ?
#14

[eluser]InsiteFX[/eluser]
Only if you set from the constructor and set a $CI variable.

I changed it to $CI->
#15

[eluser]InsiteFX[/eluser]
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Registration {

    protected $CI;

    public function __construct()
    {
        $this->CI =& get_instance();
    }

    function Registration()
    {
     # Load libraries
     $this->CI =& get_instance();
     $this->CI->load->library('session');
     $this->CI->load->library('form_validation');
     $this->CI->load->helper('form');
    }

    public function step_1()
    {
     if($this->CI->input->post('submit')) {
      $config = array(
        array(
          'field' => 'username',
          'label' => 'Username',
          'rules' => 'required|min_length[4]|callback_unique_username'
        ),
        array(
          'field' => 'email',
          'label' => 'Email',
          'rules' => 'required|valid_email'
        ),
        array(
          'field' => 'password',
          'label' => 'Password',
          'rules' => 'required|min_length[8]|callback_password',
        ),
        array(
          'field' => 'confpassword',
          'label' => 'Confirm Password',
          'rules' => 'required|callback_conf_password',
        )
      );

      $this->CI->form_validation->set_rules($config);

      if ($this->CI->form_validation->run() == FALSE)
      {
          // Validationn Failed!
          echo validation_errors();
          exit();

          $this->CI->load->view('register/step_one');
      }
      else
      {
          // Validation Passed!
          $this->CI->load->view('formsuccess');
      }
    }

    private function unique_username($str)
    {
     $this->CI->form_validation->set_message('unique_username', 'The %s field can not be the word "test"');
     return FALSE;
    }
      
    private function password($str)
    {
     if($str)
     {
      return $str;
     }
     else
     {
      $this->CI->form_validation->set_message('password', 'Password not match');
      return FALSE;
     }
      
    }

    private function conf_password($str)
    {
     if($str)
     {
      return $str;
     }
     else
     {
      $this->CI->form_validation->set_message('conf_password', 'Confrim Password not match');
      return FALSE;
     }
      
    }

}

/* End of file registration.php */
#16

[eluser]CyrusTC[/eluser]
[quote author="InsiteFX" date="1329143687"]
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Registration {

    protected $CI;

    public function __construct()
    {
        $this->CI =& get_instance();
    }

    function Registration()
    {
     # Load libraries
     $this->CI =& get_instance();
     $this->CI->load->library('session');
     $this->CI->load->library('form_validation');
     $this->CI->load->helper('form');
    }

    public function step_1()
    {
     if($this->CI->input->post('submit')) {
      $validation_rule = array(
        array(
          'field' => 'username',
          'label' => 'Username',
          'rules' => 'required|min_length[4]|callback_unique_username'
        ),
        array(
          'field' => 'email',
          'label' => 'Email',
          'rules' => 'required|valid_email'
        ),
        array(
          'field' => 'password',
          'label' => 'Password',
          'rules' => 'required|min_length[8]|callback_password',
        ),
        array(
          'field' => 'confpassword',
          'label' => 'Confirm Password',
          'rules' => 'required|callback_conf_password',
        )
      );

      $this->CI->form_validation->set_rules($validation_rule);

      if ($this->form_validation->run() == FALSE)
      {
          // Validationn Failed!
          echo validation_errors();
          exit();

          $this->CI->load->view('register/step_one');
      }
      else
      {
          // Validation Passed!
          $this->CI->load->view('formsuccess');
      }
    }

    private function unique_username($str)
    {
     $this->CI->form_validation->set_message('unique_username', 'The %s field can not be the word "test"');
     return FALSE;
    }
      
    private function password($str)
    {
     if($str)
     {
      return $str;
     }
     else
     {
      $this->CI->form_validation->set_message('password', 'Password not match');
      return FALSE;
     }
      
    }

    private function conf_password($str)
    {
     if($str)
     {
      return $str;
     }
     else
     {
      $this->CI->form_validation->set_message('conf_password', 'Confrim Password not match');
      return FALSE;
     }
      
    }

}

/* End of file registration.php */
[/quote]

I tried this code but an error is shown
Code:
Call to a member function set_rules() on a non-object in __ on line 48
#17

[eluser]InsiteFX[/eluser]
Code:
if ($this->form_validation->run() == FALSE)

// should be
if ($this->CI->form_validation->run() == FALSE)

I corrected it in the code above!
#18

[eluser]CyrusTC[/eluser]
[quote author="InsiteFX" date="1329144810"]
Code:
if ($this->form_validation->run() == FALSE)

// should be
if ($this->CI->form_validation->run() == FALSE)
[/quote]
I have fixed this one but the problem is in this line
Code:
$this->CI->form_validation->set_rules($validation_rule);
Call to a member function set_rules() on a non-object in
#19

[eluser]InsiteFX[/eluser]
Try changing the name to $config, I changed it in the code above. The $config is to be in a config file but try it if it does not work then you will need to specify each rule on it's own line...




Theme © iAndrew 2016 - Forum software by © MyBB