phpformscodeignitervalidationfield

form_validation dynamically added form dropdown field codeigniter


I have a form that dynamically adds dropdown fields depending on the number for results returned a db query that also populates the dropdown.

I need to validate if these dropdown fields. This is what I have at the moment

My view

<table>    
<tbody>
<?php

            if(!empty($result))
            {
                foreach($result as $row)
                { 
                    echo "<tr>";
                    echo "<td>" . $row->project_no . "</td>";
                    echo "<td>" . $row->sws_dwg_no . "</td>";
                    echo "<td>" . $row->client_dwg_no . "</td>";
                    echo "<td>" . $row->dwg_title . "</td>";
                    echo "<td>" . $row->dwg_by . "</td>";
                    $rev = array('' => $row->dwg_rev);
                    $rev_change = array(
                        'A' => 'A',
                        'B' => 'B',
                        'C' => 'C',
                        'D' => 'D',
                        'E' => 'E'                  
                        );
                    $dropdown = array_merge($rev,$rev_change);


                    echo "<td>" . form_dropdown('result['.$row->dwg_id.'][temp_dwg_rev]',$dropdown,'', 'id="theSelect' . $row->dwg_id .'"') . "</td>";
                    echo "<td>" . date('Y/m/d', strtotime($row->dwg_date)) . "</td>"; 
                    echo "<td>" . form_checkbox('result['.$row->dwg_id.'][temp_dwg_id]',$row->dwg_id,'', 'id="activate' . $row->dwg_id . '"') . "</td>"; 
                    echo "</tr>";
?></tbody></table>

$row->dwg_id returns the table id of the results, i.e 1,2,3 or 4, to form the associative array.

My controller

public function issue()
{

    $this->form_validation->set_rules('result[][temp_dwg_rev]', 'Rev  ', 'required|callback_rev_change');
    $this->form_validation->set_rules('result[][temp_dwg_rev]', 'Rev  ', 'required|callback_rev_change');
    $this->form_validation->set_rules('result[][temp_dwg_rev]', 'Rev  ', 'required|callback_rev_change');
    $this->form_validation->set_rules('result[][temp_dwg_rev]', 'Rev  ', 'required|callback_rev_change');

    if($this->form_validation->run())
    {
      echo "All changed";
    }
    else echo validation_errors();
}

Currently only the first dropdown is validated and an error returned. I have tried adding the id number to the field name in the form validation rule and the same thing happens - only the first dropdown is validated.

I want it to work for all the dropdown fields in the form.


Solution

  • I solve the problem that I had. Thank you for your interest Saty

    Here is what I did to solve the problem that I had. I used a foreach loop that takes the posted $row->dwg_id values to create the name of the form validation rule. The rule then matches the name of the input.

    My controller

    public function issue()
    {
        $rows = array();
        $rows = $_POST['result'];
        $temp_array = array_column($rows, 'temp_dwg_id');
        foreach($temp_array as $key => $temp_dwg_id)
        {
            $this->form_validation->set_rules('result['.$temp_dwg_id.'][temp_dwg_rev]', 'Rev'.$temp_dwg_id , 'required');
        }
    
    
        if($this->form_validation->run())
        {   
          //Do stuff.
        }
        else{
          //Do other stuff.
        }