codeigniter form validation for dynamic form input names -


i have codeigniter app. view uses database row id append input name unique id. allows me use inputs in form action, update.

my view syntax:

<?php if(isset($records)) {?> <table id="hor-minimalist-a">     <tr>         <th>&nbsp;</th><th>&nbsp;</th><th>customer name</th><th>postalcode</th>     <tr>  <?php if(isset($records)) : foreach ($records $row) : ?>     <tr>         <td> <?php echo anchor('masterdata/confirm_delete_customer/'.$row->id, img(array('src'=>'images/delete_icon.png','border'=>'0','alt'=>'delete'))); ?>         </td>         <td>             <input type=checkbox name="editcustomer[]" id="editcustomer[]" value="<?php echo $row->id ?>">         </td>         <td>             <input class="inputwide" type="text" name="customer_name_<?php echo $row->id ?>" id="customer_name_<?php echo $row->id ?>" value="<?php echo $row->customer_name ; ?>" >         </td>         <td>             <input class="inputnarrow" type="text" name="postalcode_<?php echo $row->id ?>" id="postalcode_<?php echo $row->id ?>" value="<?php echo $row->postalcode ; ?>" >         </td>     </tr> <?php endforeach ; ?>     </table> <input type="submit" value="update checked customers"> <?php endif; ?>  <?php echo form_close(); ?> <?php } else {?> <h4 id="warning"> no customers in database</h4> <?php } ?> 

as can see input name's , id's unique.

my controller syntax below:

function manage_customers()     {          $data['title']="manage customers";              //query model data results form             $data=array();              if($query=$this->model_master_data->get_customer_records()){                 $data['records']=$query;             }              $this->load->view("master_data/view_master_data_header",$data);             $this->load->view("master_data/view_master_data_nav");             $this->load->view("master_data/view_content_master_data_manage_customers",$data);             $this->load->view("master_data/view_master_data_footer");               $editcustomer = $this->input->post('editcustomer');              // single update - working              if( $this->input->post('editcustomer') != false )             {                 foreach ($editcustomer $row_id)                 {                     $data = array(                          'postalcode' => $this->input->post('postalcode_'.$row_id),                         'customer_name' => $this->input->post('customer_name_'.$row_id) );                     $this->model_master_data->update_customer_records( $row_id, $data );                 }              $this->session->set_flashdata('dbaction', 'selected records have been updated successfully');             redirect('masterdata/manage_customers', 'refresh');             }       } 

how make use of codeigniter validation class ensure users modify input boxes credible data?

how can the

$this->form_validation->set_rules("primary_contact_tell","contact person tell","required|xss_clean|min_length[10]|max_length[14]");

reference correct dynamic name of input field? form has customer name , postal code need add rest of fields.

thanks in advance, always.

you can loop through $records in controller doing in view achieve dynamic input validation rules.

foreach($records $row) {     $this->form_validation->set_rules("customer_name_" . $row->id, "customer name", "required|xss_clean|min_length[10]|max_length[14]");     $this->form_validation->set_rules("postalcode_" . $row->id, "customer name", "required|xss_clean|min_length[10]|max_length[14]"); } 


edit:

think little. don't have ability check variables in controller are. far know basing on code wrote here, should working:

foreach($editcustomer $row_id) {     $this->form_validation->set_rules("customer_name_" . $row_id, "customer name", "required|xss_clean|min_length[10]|max_length[14]");     $this->form_validation->set_rules("postalcode_" . $row_id, "customer name", "required|xss_clean|min_length[10]|max_length[14]"); } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -