当错误来自上传文件字段时,如何使codeigniter粘贴表单

时间:2013-04-29 10:12:55

标签: php codeigniter validation

我有一个允许用户上传多个文件的表单。 Additionaly用户必须填写其他输入文本字段,如地址,电子邮件等。

如果错误来自文件上传以及如何访问form_validation库设置的错误消息,我不知道如何使表单变粘。因为,例如,如果不允许其中一个文件类型,我使用重定向跳出负责上传每个文件的循环。

要保留上传错误消息,我使用$ this-> session-> set_flashdata('errors',$ errors),但之后我无法使用form_validation库中的错误消息(echo form_error('field_name))并在表单字段中显示用户输入(echo set_value('field_name'))

以下是负责文件上传和数据插入的控制器方法:

function do_upload()
{
    // load form validation library
    $this->load->library('form_validation');

    // set validation rules        
    $this->form_validation->set_rules('email','Email','trim|required|valid_email|matches[confirm_email]');
    $this->form_validation->set_rules('confirm_email','Email','trim|required|valid_email');
    $this->form_validation->set_rules('delivery_name','Nazwa','trim|required');
    $this->form_validation->set_rules('delivery_street','Ulica','trim|required');
    $this->form_validation->set_rules('delivery_city','Miasto','trim|required');
    $this->form_validation->set_rules('delivery_postcode','Kod pocztowy','trim|required');
    $this->form_validation->set_rules('delivery_country','Kraj','trim|required');
    $this->form_validation->set_rules('captcha','Captcha','callback_check_captcha');

    if ( $this->form_validation->run() )
    {
        // if validation passes I insert form data into database and I start to upload
        // all the files using foreach loop


        $config['upload_path'] = './uploads/upload_directory';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '0';

        $upload_data = array();
        $task_address_id_array = $this->MTasks->insertNewTask($task_directory);

        $task_id = $task_address_id_array['task_id'];
        $address_id = $task_address_id_array['address_id'];

        // iterate over files to be uploaded
        foreach ($_FILES as $key => $value)
        {

            if (!empty($value['name']))
            {
                $this->upload->initialize($config);
                $result = $this->upload->do_upload($key);
                if (!$result)
                {
                    // if upload error occurs I need to redirect to break the loop
                    $errors = $this->upload->display_errors();
                    $this->session->set_flashdata('errors', $errors);

                    redirect();

                }
                else
                {
                    $current_upload_data = $this->upload->data();
                    // insert upload data to database
                    $this->MTasks->insertFileInfo($current_upload_data,$task_id);
                }

            }
        }
        redirect('upload/success_message');
    }
    else
    {
        $this->load->view('include/header');
        $this->load->view('upload_form',$this->data);
        $this->load->view('include/footer');
    }


}

1 个答案:

答案 0 :(得分:0)

主要问题是您不能将验证规则文件上传,因为该库逐个执行。

一个简单的解决方案是验证您的字段,然后验证文件上传。例如:

<?php

function save()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('name', 'Name', 'trim|required');

    if($this->form_validation->run() == FALSE)
    {
        $this->form_validation->set_error_delimiters('', '<br />');
        echo validation_errors();
        exit();
    }
    else
    {
        if($_FILES['imagen']['error'] != 4)
        {
            $image $this->_manage_image($this->input->post('name'));
        }
        else
        {
            exit('No empty field!');
        }

        # save in your bd o success message...  
    }
}

function _manage_image($name) 
{
    # config
    $config['upload_path'] = './images/products/';
    $config['allowed_types'] = 'gif|jpg|png';
    // ...
    $config['file_name'] = $name;

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload('image'))
    {
        # errors...
        echo $this->upload->display_errors();
        exit();
    }
    else
    {
        # upload
        return $img_data['file_name'];  
    }

}