• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation with Arrays as Field Names

#1
I am trying to use Form Validation with an Array for field names. I was hoping that it was possible to validate each field individually, I've followed the documentation closely but I when I submit the form the validation message should display for one field but it displays for all fields. What have I done wrong?
[Image: screenshot%202019-02-07%2016-47-50.png?raw=1]

View:
Code:
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Document</title>
</head>
<body>
   <form method="post">
       <input type="text" name="options[]" value="<?php echo set_value('options[]'); ?>">
       <?php echo form_error('options[]'); ?>
       <br>
       <input type="text" name="options[]" value="<?php echo set_value('options[]'); ?>">
       <?php echo form_error('options[]'); ?>
       <br>
       <input type="text" name="options[]" value="<?php echo set_value('options[]'); ?>">
       <?php echo form_error('options[]'); ?>
       <br>
       <input type="submit">
   </form>
</body>
</html>

Controller Method:
PHP Code:
public function array_validation()
{
    
$this->load->helper'form' );
    
$this->load->library('form_validation');
    
$this->form_validation->set_rules('options[]''Options''trim|numeric');
    if (
$this->form_validation->run() == FALSE) {
        
// form empty or validation failed
    
} else {
    }
    
$this->load->view('welcome/array_validation');

Reply

#2
@rich8374,

You could potentially have a name for each one such as name_01, name_02, name_03 instead of one name of options[] ...Or...
You could keep it the way it currently is and do so extra validation after it is determined that it meets the basic field name requirements
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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