codeigniter validation_errors()过早填充

时间:2014-05-22 21:35:03

标签: php forms codeigniter validation

我有一个视图,其中我使用带有单选按钮的表单来显示公司中的所有员工。 (我使用一个简单的java脚本,允许在不使用提交按钮的情况下提交表单,并通过单选按钮将员工ID传递给控制器​​)​​

<script language="JavaScript" type="text/javascript"><!--
function submit ()
{
        document.select_employee.submit() ;
}

--></script>

<title>Employee Master</title>

<a href="<?php echo (base_url('index.php/employee_master/add_employee'))?>">Add New     Employee </a>
<a href="javascript:submit()">Edit Employee</a>
<a href="">Delete Employee </a>
<br />
<?php $id_people = "0"; ?>
<form name ="select_employee" id="select_employee" action="<?php echo    (base_url('index.php/employee_master/edit_employee/'.$id_people.''))?>" method="post">
<br />
<?php foreach($rows as $person) { ?>
<?php $id_people=$person['id_people']; ?>
<input type = "radio" class='rad_button' name = "employee_id" value="<?php echo $id_people ; ?>"/>

<?php
    foreach($person as $object){

        echo $object; ?>_______

    <?php } ?> <br> 

<?php } ?>

</form>
</body>
</html>


<script>
$(".rad_button").click(function() {
$("#select_employee").attr('action', "<?php echo     base_url('index.php/employee_master/edit_employee')?>" + "/" + $(this).attr('value'));
console.log($("#select_employee").attr('action'));
})
</script>

之前,我从post发送了$ id_people但是我添加了通过url传递它的脚本。这是传递给它的控制器。

public function edit_employee($id_people){

    if(empty($id_people)){

        $this->session->set_flashdata('message', "please select an employee");
        redirect(base_url('index.php/employee_master'), 'refresh');
    } else {
            $this->load->model('employee_master_model');
            $info=$this->employee_master_model->get_info($id_people);//this will query the db every time form validation fails, may need to streamline

            $this->load->library('form_validation');
            $this->load->config('form_validation');

            if ($this->form_validation->run('employee_master/edit_employee') == FALSE)
            {
                $this->load->view("forms/edit_employee", $info);
            }

            else
            { . . . (unrelated part of the controller)...

当这个控制器第一次加载forms_ edit_employee时,每个错误都填充了validation_errors()!这是我的配置文件,我从

调用验证规则
<?php //contains validation rules for all forms in application. name of array corrisponds to controller name and function name

$config = array(
             'employee_master/add_employee' => array(
                                array(
                                        'field' => 'first_name',
                                        'label' => '"First Name"',
                                        'rules' => 'required|max_length[44]'
                                     ),
                                array(
                                        'field' => 'email',
                                        'label' => '"Email"',
                                        'rules' => 'required|max_length[44]|is_unique[people.email]|valid_email'
                                     ),
                                array(
                                        'field' => 'last_name',
                                        'label' => '"Last Name"',
                                        'rules' => 'required|max_length[44]'
                                     ),
                                array(
                                        'field' => 'address',
                                        'label' => '"Address"',
                                        'rules' => 'required|max_length[44]'
                                     ),
                                array(
                                        'field' => 'city',
                                        'label' => '"City"',
                                        'rules' => 'required|max_length[44]'
                                     ),
                                array(
                                        'field' => 'state',
                                        'label' => '"State"',
                                        'rules' => 'required|max_length[2]'
                                     ),
                                array(
                                        'field' => 'marital_status',
                                        'label' => '"Marital Status"',
                                        'rules' => 'required'
                                     ),
                                array(
                                        'field' => 'ssn',
                                        'label' => '"Social Security"',
                                        'rules' => 'required|max_length[9]|numeric'
                                     ),
                                array(
                                        'field' => 'acct_number',
                                        'label' => '"Account Number"',
                                        'rules' => 'required|max_length[15]|numberic'
                                     ),
                                array(
                                        'field' => 'pay_rate',
                                        'label' => '"Pay Rate"',
                                        'rules' => 'required|numeric'
                                     ),
                                array(
                                        'field' => 'direct_deposit',
                                        'label' => '"Direct Deposit"',
                                        'rules' => 'required'
                                     )

                                ),
             'employee_master/edit_employee' => array(
                                array(
                                        'field' => 'first_name',
                                        'label' => '"First Name"',
                                        'rules' => 'required|max_length[44]'
                                     ),
                                array(
                                        'field' => 'email',
                                        'label' => '"Email"',
                                        'rules' => 'required|max_length[44]valid_email'
                                     ),
                                array(
                                        'field' => 'last_name',
                                        'label' => '"Last Name"',
                                        'rules' => 'required|max_length[44]'
                                     ),
                                array(
                                        'field' => 'address',
                                        'label' => '"Address"',
                                        'rules' => 'required|max_length[44]'
                                     ),
                                array(
                                        'field' => 'city',
                                        'label' => '"City"',
                                        'rules' => 'required|max_length[44]'
                                     ),
                                array(
                                        'field' => 'state',
                                        'label' => '"State"',
                                        'rules' => 'required|max_length[2]'
                                     ),
                                array(
                                        'field' => 'marital_status',
                                        'label' => '"Marital Status"',
                                        'rules' => 'required'
                                     ),
                                array(
                                        'field' => 'ssn',
                                        'label' => '"Social Security"',
                                        'rules' => 'required|max_length[9]|numeric'
                                     ),
                                array(
                                        'field' => 'acct_number',
                                        'label' => '"Account Number"',
                                        'rules' => 'required|max_length[15]|numberic'
                                     ),
                                array(
                                        'field' => 'pay_rate',
                                        'label' => '"Pay Rate"',
                                        'rules' => 'required|numeric'
                                     ),
                                array(
                                        'field' => 'direct_deposit',
                                        'label' => '"Direct Deposit"',
                                        'rules' => 'required'
                                     )


                                )                         
           );

2 个答案:

答案 0 :(得分:0)

如果我理解正确,请将表单验证包装到:

if($this->input->post('submit_button_name_or_input_name'))
{
    // run validation
}

现在正在进行的是您的验证运行,第二次显示您的表单。

答案 1 :(得分:0)

将验证分配给变量

&#39; $ data [&#39; form2_errors&#39;] = validation_errors();&#39;

请参阅此链接