0

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.

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 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 } ?>

as you can see the input name's and id's are then unique.

My controller syntax is below:

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


    }

How do I make use of the codeigniter validation class to ensure the users modify the input boxes with 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 the correct dynamic name of the input field? form currently has only customer name and postal code but need to add the rest of the fields.

Thanks in advance, as always.

2 Answers 2

1

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]");
}
4
  • Thanks yabol, tried as you suggested but fails as per update on question? my normal foreach statement is in my view, your advice is for the model and the foreach is failing there. Please advise. Thanks,
    – Smudger
    Commented Mar 3, 2013 at 15:54
  • 1
    Change it to: foreach($editcustomer as $row_id) as you do it below to update each row.
    – Jan.J
    Commented Mar 3, 2013 at 22:16
  • Thanks yabol. Trying with this now and getting an error Trying to get property of non object To me this is a result of the $row_id->id not having a value. I'll update my current syntax shortly and show you a sample of the output. Thanks again
    – Smudger
    Commented Mar 4, 2013 at 6:36
  • Thanks yabol, perfect. really appreciate your knowledge and time.
    – Smudger
    Commented Mar 4, 2013 at 7:52
0

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

View

<?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(); ?>

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

            }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.