• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Callback is failed in Form Validation

#1
[eluser]CyrusTC[/eluser]
Hi Everyone,

I am new to codeigniter and i just wanna ask about a failure of executing the callback function in the form validation

Code:
$validation_rule = array(
        array(
          'field' => 'username',
          'label' => 'Username',
          'rules' => 'required|callback_unique_username'
        ),
        array(
          'field' => 'email',
          'label' => 'Email',
          'rules' => 'required|valid_email'
        ),
        array(
          'field' => 'password',
          'label' => 'Password',
          'rules' => 'required|callback_same_password['.$this->CI->input->post('confpassword').']',
        ),
        array(
          'field' => 'confpassword',
          'label' => 'Confirm Password',
          'rules' => 'required|callback_same_password['.$this->CI->input->post('confpassword').']',
        )
      );
In Fact, the above are validation rules of a form and you are able to see i put callback_unique_username in the rule of username, where callback_unique_username is
Code:
function unique_username($str)
    {
     $this->CI->form_validation->set_message('unique_username', 'The %s field can not be the word "test"');
     return FALSE;
    }
This Callback validation function is just a test function, in which it gives a error message anyway. However, the error message isn't shown in validation_errors().
Could any guys tell me what's going wrong? Thanks.

#2
[eluser]elverion[/eluser]
Why are you using $this->CI->form_validation instead of just $this->form_validation? I'm not sure if that has anything to do with it, but it might.

Also, you should show the code where you are setting the rules and running the validation.

#3
[eluser]CyrusTC[/eluser]
Alright, here is the full code of the library(registration)
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Registration {

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

    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_same_password['.$this->CI->input->post('confpassword').']',
        ),
        array(
          'field' => 'confpassword',
          'label' => 'Confirm Password',
          'rules' => 'required|callback_same_password['.$this->CI->input->post('confpassword').']',
        )
      );
      $this->CI->form_validation->set_rules($validation_rule);
      $this->CI->form_validation->run();
      echo validation_errors();exit;
     }
     $this->CI->load->view('register/step_one');
    }

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

/* End of file registration.php */

#4
[eluser]elverion[/eluser]
I think it is because you are trying to echo it out inside the library. Depending on when/where it is called, that might get suppressed. Try returning the string to a controller and then output it from there to test this.

#5
[eluser]CyrusTC[/eluser]
But it gives a normal error results for the "required" rule
Code:
The Email field is required.
The Password field is required.
The Confirm Password field is required.

For the controller, it just simple load the library of registration and execute the step function

Code:
# Steup the registration library
  $this->load->library('registration');

  # Get Definition of the steps we are going for
  $this->load->library('session');
  $step = $this->session->userdata('registration_step');
  switch($step) {
   case '2' :
    $this->registration->step_2();
   break;
   case '3' :
    $this->registration->step_3();
   break;
   default:
    $this->registration->step_1();
   break;    
  }

#6
[eluser]InsiteFX[/eluser]
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.

#7
[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]

You mean i should put it in this way? But the callback function is still not working yet.
Code:
function step_1()
    {
     $CI =& get_instance();
     $CI->load->library('session');
     $CI->load->library('form_validation');
     $CI->load->helper('form');
    
     if($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_same_password['.$CI->input->post('confpassword').']',
        ),
        array(
          'field' => 'confpassword',
          'label' => 'Confirm Password',
          'rules' => 'required|callback_same_password['.$CI->input->post('confpassword').']',
        )
      );
      $CI->form_validation->set_rules($validation_rule);
      $CI->form_validation->run();
      echo validation_errors();exit;
     }
     $CI->load->view('register/step_one');
    }

#8
[eluser]InsiteFX[/eluser]
Code:
if ($CI->form_validation->run() == FALSE)
{
    // Validationn Failed!
    $CI->load->view('register/step_one');
}
else
{
    // Validation Passed!
    $CI->load->view('formsuccess');
}

Code:
'rules' => 'required|callback_password'
'rules' => 'required|callback_conf_password'

// callback
function password($str)
function conf_password($str)

You need a seperate callback for each password check.

Your echo validation_errors should be in your html.

Updated: Forgot the CI

#9
[eluser]CyrusTC[/eluser]
[quote author="InsiteFX" date="1329140682"]
Code:
if ($this->form_validation->run() == FALSE)
{
    // Validationn Failed!
    $this->load->view('myform');
}
else
{
    // Validation Passed!
    $this->load->view('formsuccess');
}

Code:
'rules' => 'required|callback_password'
'rules' => 'required|callback_conf_password'

// callback
function password($str)
function conf_password($str)

You need a seperate callback for each password check.

Your echo validation_errors should be in your html.
[/quote]
the errors is just echoed for testing
I made further test on this, which i found the callback function is skipped by
Code:
if ( ! method_exists($this->CI, $rule))
    {
     continue;
    }
As my registration is a library for handling the data validation,
is the error caused by this?

#10
[eluser]InsiteFX[/eluser]
It's not calling your callback because it is not configured correct like I mentioned to you!

The form passes the data back to the validation you do not have to specify it!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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