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


Messages In This Thread
Form validation and Callback function Rule - by El Forum - 03-08-2012, 10:55 AM
Form validation and Callback function Rule - by El Forum - 03-08-2012, 11:19 AM
Form validation and Callback function Rule - by El Forum - 03-12-2012, 12:52 AM



Theme © iAndrew 2016 - Forum software by © MyBB