Forms 动态表单输入名称的codeigniter表单验证

Forms 动态表单输入名称的codeigniter表单验证,forms,codeigniter,dynamic,input,validation,Forms,Codeigniter,Dynamic,Input,Validation,我有一个codeigniter应用程序。我的视图使用数据库行ID附加到输入名称以获得唯一ID。这允许我使用表单操作(即更新)中的所有输入 我的视图语法: <?php if(isset($records)) {?> <table id="hor-minimalist-a"> <tr> <th>&nbsp;</th><th>&nbsp;</th><th>Custom

我有一个codeigniter应用程序。我的视图使用数据库行ID附加到输入名称以获得唯一ID。这允许我使用表单操作(即更新)中的所有输入

我的视图语法:

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


    }
我如何使用codeigniter验证类来确保用户使用可靠的数据修改输入框

怎么可能

$this->form_validation->set_rules(“主要联系人告知”)、“
联系人告知
”、“必需的xss_clean |最小长度[10]|最大长度[14]”

是否引用输入字段的正确动态名称?表单当前只有客户名称和邮政编码,但需要添加其余字段


一如既往,请提前感谢。

您可以在控制器中循环使用$records,以实现动态输入验证规则

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

编辑:

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


    }
想一想。我无法检查控制器中的变量。据我所知,根据您在这里编写的代码,这应该是可行的:

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

工作解决方案,非常感谢@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(); ?>

谢谢yabol,按照您的建议尝试,但根据问题的更新失败?在我看来,我通常的foreach语句是,您的建议是针对模型的,而foreach在这方面是失败的。请告知。谢谢,将其更改为:foreach($editcustomer as$row\u id),如下所示,以更新每一行。谢谢yabol。现在尝试此操作并获得一个错误
尝试获取非对象的属性
,这是由于
$row\u id->id
没有值造成的。我将很快更新当前语法,并向您展示一个输出示例。再次感谢Hanks yabol,太好了。非常感谢你的知识和时间。