2013-03-03 152 views
0

我有一個codeigniter應用程序。我的視圖使用數據庫行ID附加到輸入名稱以獲取唯一的ID。這允許我在我的表單操作中使用所有輸入,這是更新。動態表單輸入名稱的codeigniter表單驗證

我查看語法:

<?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 as $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 currently in database</h4> 
<?php } ?> 

,你可以看到輸入name'sid's然後獨一無二的。

我的控制器語法如下:

function manage_customers() 
    { 

     $data['title']="Manage Customers"; 

      //query model to get data results for 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 as $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'); 
      } 


    } 

我如何利用笨驗證類,以確保用戶修改輸入框與可信的數據?

$this->form_validation->set_rules("primary_contact_tell","聯繫人怎麼能告訴","required|xss_clean|min_length[10]|max_length[14]");

參考輸入字段的正確的動態名字?表格目前只有客戶名稱和郵政編碼,但需要添加其餘的字段。

在此先感謝,一如既往。

回答

1

您可以循環訪問控制器中的$記錄,因爲您正在執行該操作以實現動態輸入驗證規則。

foreach($records as $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]"); 
} 


編輯:

想一點。我無法檢查控制器中的變量。據我所知,基於你寫在這裏的代碼,這應該是工作:

foreach($editcustomer as $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]"); 
} 
+0

感謝yabol,試圖如你所說,但未能按有關問題的更新?在我看來,我的正常的foreach語句是,你的建議是針對模型,而foreach在那裏失敗。請指教。謝謝, – Smudger 2013-03-03 15:54:10

+1

將它更改爲:foreach($ editcustomer as $ row_id),如下所示更新每一行。 – 2013-03-03 22:16:47

+0

謝謝雅博爾。試着用這個,並得到一個錯誤「試圖獲取非對象的屬性」對我來說這是$ row_id-> id'沒有值的結果。我會馬上更新我的當前語法,並向您展示輸出的示例。再次感謝 – Smudger 2013-03-04 06:36:17

0

工作解決方案,非常感謝@yabol在這一個。我仍然需要清理一些語法,但所需的功能工作。

查看

<?php 
    $attributes=array(
     'name'=>'updatecustomer', 
     'id'=>'updatecustomer', 
     ); 
    echo form_open('masterdata/manage_customers',$attributes); 
?> 
<div id="validation_failed"> 
    <?php 
     echo validation_errors(); 
    ?> 
</div> 
<?php if(isset($records)) {?> 
<table id="hor-minimalist-a"> 
    <tr> 
     <th>&nbsp;</th><th>&nbsp;</th><th>Customer Name</th><th>Address Line 1</th><th>Address Line 2</th><th>Suburb</th><th>City</th><th>Postal Code</th><th>Contact Name</th><th>Contact Email</th><th>Contact Tel</th> 
    <tr> 

<?php if(isset($records)) : foreach ($records as $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="inputmedium" type="text" name="address_line_1_<?php echo $row->id ?>" id="address_line_1_<?php echo $row->id ?>" value="<?php echo $row->address_line_1 ; ?>" > 
     </td> 
     <td> 
      <input class="inputmedium" type="text" name="address_line_2_<?php echo $row->id ?>" id="address_line_2_<?php echo $row->id ?>" value="<?php echo $row->address_line_2 ; ?>" > 
     </td> 
     <td> 
      <input class="inputmedium" type="text" name="suburb_<?php echo $row->id ?>" id="suburb_<?php echo $row->id ?>" value="<?php echo $row->suburb ; ?>" > 
     </td> 
     <td> 
      <input class="inputmedium" type="text" name="city_<?php echo $row->id ?>" id="city_<?php echo $row->id ?>" value="<?php echo $row->city ; ?>" > 
     </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> 
     <td> 
      <input class="inputmedium" type="text" name="primary_contact_name_<?php echo $row->id ?>" id="primary_contact_name_<?php echo $row->id ?>" value="<?php echo $row->primary_contact_name ; ?>" > 
     </td> 
     <td> 
      <input class="inputmedium" type="text" name="primary_contact_email_<?php echo $row->id ?>" id="primary_contact_email_<?php echo $row->id ?>" value="<?php echo $row->primary_contact_email ; ?>" > 
     </td> 
     <td> 
      <input class="inputmedium" type="text" name="primary_contact_tell_<?php echo $row->id ?>" id="primary_contact_tell_<?php echo $row->id ?>" value="<?php echo $row->primary_contact_tell ; ?>" > 
     </td> 

    </tr> 
<?php endforeach ; ?> 
    </table><br> 
<input type="submit" value="Update Checked Customers"> 
<?php endif; ?> 

<?php echo form_close(); ?> 

控制器

function manage_customers() 
    { 

     $data['title']="Manage Customers"; 
      //query model to get data results for form 
      $data=array(); 

      if($query=$this->model_master_data->get_customer_records()){ 
       $data['records']=$query; 
      } 
      $editcustomer = $this->input->post('editcustomer'); 

      if($this->input->post('editcustomer') != false){ 
      foreach($editcustomer as $row_id) 
      { 
       $this->form_validation->set_rules("customer_name_" . $row_id, "Customer name", "required|min_length[6]"); 
       $this->form_validation->set_rules("address_line_1_". $row_id,"`Address Line 1`","required|xss_clean|min_length[6]"); 
       $this->form_validation->set_rules("address_line_2_". $row_id,"`Address Line 2`","xss_clean|min_length[6]"); 
       $this->form_validation->set_rules("suburb_". $row_id,"`Suburb`","required|xss_clean|min_length[6]"); 
       $this->form_validation->set_rules("city_". $row_id,"`City`","required|xss_clean|min_length[6]"); 
       $this->form_validation->set_rules("postalcode_". $row_id,"`Postal Code`","required|xss_clean|min_length[4]|max_length[5]"); 
       $this->form_validation->set_rules("primary_contact_name_". $row_id,"`Contact Person Name`","required|xss_clean|min_length[6]"); 
       $this->form_validation->set_rules("primary_contact_email_". $row_id,"`Contact Person email`","required|valid_email|xss_clean"); 
       $this->form_validation->set_rules("primary_contact_tell_". $row_id,"`Contact Person tell`","required|xss_clean|min_length[10]|max_length[14]"); 

      } 
      } 

      if ($this->form_validation->run() == FALSE){ 

       $data["message"]=""; 

       $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"); 

      } else { 
       // single update - working 
       if($this->input->post('editcustomer') != false) 
       { 
        foreach ($editcustomer as $row_id) 
        { 
         $data = array( 
         'customer_name' => $this->input->post('customer_name_'.$row_id), 
         'address_line_1' => $this->input->post('address_line_1_'.$row_id), 
         'address_line_2' => $this->input->post('address_line_2_'.$row_id), 
         'suburb' => $this->input->post('suburb_'.$row_id), 
         'city' => $this->input->post('city_'.$row_id), 
         'postalcode' => $this->input->post('postalcode_'.$row_id), 
         'primary_contact_name' => $this->input->post('primary_contact_name_'.$row_id), 
         'primary_contact_email' => $this->input->post('primary_contact_email_'.$row_id), 
         'primary_contact_tell' => $this->input->post('primary_contact_tell_'.$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'); 
        } 

      } 
    }