codeigniter form validation for dynamic form input names

前端 未结 2 1463

I have a codeigniter app. My view uses the database row ID to append to the input name to get a unique ID. this allows me to use all inputs in my form action, which is update.

2条回答
  •  佛祖请我去吃肉
    2021-01-28 05:32

    Working solution, big thanks to @yabol on this one. I still need to clean up the syntax a little but desired functionality working.

    View

    'updatecustomer',
            'id'=>'updatecustomer',
            );
        echo form_open('masterdata/manage_customers',$attributes);
    ?>
    
      Customer NameAddress Line 1Address Line 2SuburbCityPostal CodeContact NameContact EmailContact Tel
    id, img(array('src'=>'images/delete_icon.png','border'=>'0','alt'=>'Delete'))); ?>

    Controller

    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');
                        }
    
                }
        }
    

提交回复
热议问题