codeigniter form validation for dynamic form input names

前端 未结 2 1449

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:22

    You can loop through your $records in controller as you are doing it in view to achieve dynamic input validation rules.

    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]");
    }
    


    Edit:

    Think a little. I don't have ability to check what variables in your controller are. As far as I know basing on code you wrote here, this should be working:

    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]");
    }
    

提交回复
热议问题