Welcome Guest, Not a member yet? Register   Sign In
CI 2.2 Form Validation not working as expected
#7

(This post was last modified: 10-27-2015, 08:54 AM by joseph.dicdican. Edit Reason: code indention )

SRG,

I apologize bout the previous post, I didn't read it carefully.

Now I got you somehow, you are using array for input
Code:
<form action="<?php echo base_url('test/index'); ?>" method="post">
   <p>
       <label>Name</label>
       <input type="text" name="opt[]" value="<?php echo set_value('opt['.(0).']'); ?>" />
   </p>
   <p>
       <label>Email</label>
       <input type="text" name="opt[]" value="<?php echo set_value('opt['.(1).']'); ?>" />
   </p>
   <p>
       <label>Phone</label>
       <input type="text" name="opt[]" />
   </p>
   <p>
       <input type="submit" value="Submit" />
       <a href="<?php echo base_url('test/index'); ?>">reset</a>
   </p>
</form>
<?php echo validation_errors(); ?>
I have searched from the internet, also from the user_guide of codeigniter. I found out that we can set validation rules on input arrays by using their indexes or the whole input name array.

I coded this on my local and had it running.
PHP Code:
public function index() 
{
 
   $this->load->library('form_validation');
 
   
    
// I hard coded the index number for each option. (e.g. opt[0] for Name and opt[1] for email
 
   // Enclose it with parenthesis "()" to obtain the integer value
 
   $config = array(
 
       array('field' => 'opt['.(0).']''label' => 'Name''rules' => 'trim|required|max_length[50]'),
 
       array('field' => 'opt['.(1).']''label' => 'Email''rules' => 'trim|valid_email')
 
   );
 
   
    
// setting rules
 
   $this->form_validation->set_rules($config);
 
 
   // intended for debugging
 
   $this->prePrintR($config);
 
  
    if
($_POST) {
 
       $input $this->input->post();
 
       $this->prePrintR($input);
 
   }
 
   
    
// checking if form is valid else, load the view and show validation_errors in test/index
 
   if($this->form_validation->run()) {
 
       echo 'form is valid';
 
   

 
   $this->load->view('test/index');
}

// Customized debugger to print_r variables
private function prePrintR($expression
{
 
   echo '<pre>';
 
   echo print_r($expression);
 
   echo '</pre>';


In this part, I enclosed the index value of each opt with parenthesis "()" to obtain the integer value. The value of index depends on the arrangement of inputs on view page. 
Code:
$config = array(
   array('field' => 'opt['.(0).']', 'label' => 'Name', 'rules' => 'trim|required|max_length[50]'),
   array('field' => 'opt['.(1).']', 'label' => 'Email', 'rules' => 'trim|valid_email')
);

If we write it merely like below,
Code:
$config = array(
   array('field' => 'opt[0]', 'label' => 'Name', 'rules' => 'trim|required|max_length[50]'),
   array('field' => 'opt[1]', 'label' => 'Email', 'rules' => 'trim|valid_email')
);
then PHP by default considers them as strings not with index. I suppose these cause the errors. 
Additionally, based on what I just learned, we do not need to check if input is not null before setting validation rules, we can just set a validation rule directly without required in rules. This validates the specific field if it is not null.

You can see the attachment what I got in my testing. By the way, I did not include the phone validation for this is just for testing. 
           

I hope this time this can help. Thank you.

- joseph.dicdican  Smile
Joseph K. Dicdican
Softshore Global Solutions Inc. | Junior Web Developer Intern
Passerelles numériques Philippines | Scholar
[email protected] 
Reply


Messages In This Thread
RE: CI 2.2 Form Validation not working as expected - by joseph.dicdican - 10-27-2015, 08:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB