Welcome Guest, Not a member yet? Register   Sign In
Form validation and Callback function Rule
#1

[eluser]Glazz[/eluser]
Hi there =)

I have this code:
Code:
public function add()
{
  // Load the form validation library.
  //
  $this->load->library('form_validation');

  // Declare all the inputs.
  //
        $inputs = array(
         'firstname'  => array('name' => 'firstname'  , 'id' => 'firstname'  , 'type' => 'text'     , 'rules' => 'trim|required|min_length[1]|max_length[32]'),
         'lastname'   => array('name' => 'lastname'   , 'id' => 'lastname'   , 'type' => 'text'     , 'rules' => 'trim|required|min_length[1]|max_length[32]'),
         'address_1'  => array('name' => 'address_1'  , 'id' => 'address_1'  , 'type' => 'text'     , 'rules' => 'trim|required|min_length[5]|max_length[128]'),
         'address_2'  => array('name' => 'address_2'  , 'id' => 'address_2'  , 'type' => 'text'     , 'rules' => null),
         'city'       => array('name' => 'city'       , 'id' => 'city'       , 'type' => 'text'     , 'rules' => 'trim|required|min_length[2]|max_length[128]'),
         'postcode'   => array('name' => 'postcode'   , 'id' => 'postcode'   , 'type' => 'text'     , 'rules' => 'trim|required|min_length[2]|max_length[10]'),
         'country'    => array('name' => 'country'    , 'id' => 'country'    , 'type' => 'select'   , 'rules' => 'trim|required'),
         'zone'       => array('name' => 'zone'       , 'id' => 'zone'       , 'type' => 'text'     , 'rules' => 'trim|required|min_length[2]|max_length[128]'),
         'email'      => array('name' => 'email'      , 'id' => 'email'      , 'type' => 'text'     , 'rules' => 'trim|required|valid_email|callback_email_check'),
         'password'   => array('name' => 'password'   , 'id' => 'password'   , 'type' => 'password' , 'rules' => 'trim|required|min_length[4]|max_length[20]'),
         'password_c' => array('name' => 'password_c' , 'id' => 'password_c' , 'type' => 'password' , 'rules' => 'trim|required|matches[password]'),
        );

  // Set the rules.
  //
        foreach($inputs as $input => $value):
   //$$input = ( ($this->input->post('save')) ? $this->form_validation->set_value($input) : '' );
         if($value['rules'] != null):
    $this->form_validation->set_rules($input, lang('customer:entry_' . $input), $value['rules']);
         endif;
        endforeach;

        // Run the form validation.
        //
  if ($this->form_validation->run() == true):
   // Prepare the data.
   //
   $new = array();
   foreach($inputs as $input => $value):
                $new[$input] = ( ($this->input->post('save')) ? $this->form_validation->set_value($input) : '' );
   endforeach;

   // Create the customer.
   //
   $customer_id = $this->Customers_model->add($new);

   // Redirect to the page of the new customer
   //
   redirect('customer/edit/' . $customer_id);
  endif;

        // Create the inputs and repopulate them automagically.
        //
        foreach($inputs as $input => $value):
         // If the input is not a select menu.
         //
         if($input['type'] != 'select'):
          $data[$input] = array(
              'name'        => $value['name'],
              'id'          => $value['id'],
              'type'        => $value['type'],
              'class'       => 'full-width with-tip' . ( (form_error($input)) ? ' error' : '' ),
              'value'       => ( $this->input->post('save') ) ? $this->form_validation->set_value($input) : '' ,
              'title'       => ( $this->input->post('save') ) ? ( (form_error($input)) ? lang('customer:tooltip.error_' . $input) : lang('global:tooltip.ok')) : lang('customer:ph_' . $input),
              'placeholder' => lang('customer:ph_' . $input)
          );
        endif;
        endforeach;

  // Show the page.
  //
  $this->load->view('admin/customers/add', $data);
}

What it does is declare the rules for validation and create variables with my inputs so what i need to do in my vie is form_input($variable); and thats it.

Almost everything is working fine, except the callback function on the email rule...

callback_email_check function:
Code:
public function email_check($str)
{
  var_dump($str);
  die;
  return false;
  /*if ($str == 'test')
  {
   $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
   return FALSE;
  }
  else
  {
   return TRUE;
  }*/
}


I think this function is not being called at all, i don't seem to know why, am i missing something ?

Other than that, everthing works as expected Smile
#2

[eluser]Glazz[/eluser]
Well i solved it, since in this website we are using hmvc i needed to do this:

Code:
<?php
/** application/libraries/MY_Form_validation **/
class MY_Form_validation extends CI_Form_validation
{
    public $CI;
}
Code:
<?php
class Xyz extends MX_Controller
{
    function __construct()
    {
        parent::__construct();
        
        $this->load->library('form_validation');
        $this->form_validation->CI =& $this;
    }
}

And it started working =)
#3

[eluser]Aken[/eluser]
That seems really hackish for what you're trying to do.

Callback functions are meant to be placed in the same controller class as the method that's calling it. So if your add() method has a form validation callback being called in it, then the callback method needs to be in that same controller file.

If you want to make the validation rule more universal, you can extend the form validation library and just add the method to your extended file. Then you can call the rule just like you would any other one (without the "callback_" prefix). You shouldn't need to redefine the scope of the CI variable or anything.




Theme © iAndrew 2016 - Forum software by © MyBB