Viewed   83 times

I have 2 text fields and 1 file upload that are all required. Everything works when I require just the text fields, but when I require the file upload the validation error stays saying that a file is required, even though I selected one. Anyone know what I am doing wrong? Many thanks in advance.

//view

<?php echo form_open_multipart('add'); ?>

<fieldset>
    <input type="text" name="name" /><br>
    <input type="text" name="code" /><br>
    <input type="file" name="userfile" /><br><br>
    <input type="submit"value="Add" />
</fieldset>

<?php echo form_close(); ?>

//controller

public function add() {

    $this->form_validation->set_rules('name', 'Name', 'required');
    $this->form_validation->set_rules('code', 'Code', 'required');
    //$this->form_validation->set_rules('userfile', 'Document', 'required');
    //when the above line is active the upload does not go through

    if ($this->form_validation->run() == FALSE) {

        $data['page_view'] = $this->page_view;
        $data['page_title'] = $this->page_title;
        $this->load->view('template', $data);
    }
    else
    {
        $this->load->library('upload');

        if (!empty($_FILES['userfile']['name']))
        {
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';     

            $this->upload->initialize($config);

            if ($this->upload->do_upload('userfile'))
            {
                $img = $this->upload->data();
                $file_name = $img['file_name'];

                $name = $this->input->post('name');
                $code = $this->input->post('code');

                $this->load->model('create', 'create_model');
                $this->create_model->create_entry($name, $code, $file_name);

                $data['page_view'] = $this->page_view;
                $data['page_title'] = $this->page_title;
                $this->load->view('template', $data);
            }
            else
            {
                echo $this->upload->display_errors();

            }
        }
    }
}

 Answers

3

I found a solution that works exactly how I want.

I changed

$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('code', 'Code', 'trim|required');
$this->form_validation->set_rules('userfile', 'Document', 'required');

To

$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('code', 'Code', 'trim|required');
if (empty($_FILES['userfile']['name']))
{
    $this->form_validation->set_rules('userfile', 'Document', 'required');
}
Thursday, December 8, 2022
4

At your PHP change

$this->upload->do_upload()

to

$this->upload->do_upload("add_1")

where "add_1" is your input name attribute.

Codeigniter UPLOAD library docs

P.S. At your HTML the Label attribute for="" is targeting input ID so you have to add id to your input.

Thursday, September 15, 2022
3

You have to loop through the uploaded files like it is shown in your provided link.

function do_upload() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'jpeg|png|gif';
        $config['max_size'] = '0';
        $config['max_width']  = '0';
        $config['max_height']  = '0';

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

        foreach ($_FILES as $key => $value) {

            if (!empty($value['tmp_name'])) {

                if ( ! $this->upload->do_upload($key)) {
                    $error = array('error' => $this->upload->display_errors());
                    //failed display the errors
                } else {
                    //success
                }

            }
        }
}

And try using HTML like this:

<input type="file" name="file1" id="file_1" />
<input type="file" name="file2" id="file_2" />
<input type="file" name="file3" id="file_3" />

You can change the names of the input fields as you like.

Sunday, November 27, 2022
 
4

take a look at the following. regex for international numbers. You will need to check against multiple regex. If the number does not match the first regex(us phone numbers) then check it against the international regex. If neither match, fail the test

Wednesday, August 24, 2022
5

HTML:

<div id="ajaxResults"></div>

Javascript ajax:

 success: function(response) {
    $('#ajaxResults').text(response);
   }

this script you've wrote is only if the validation succeeds, right?

Wrong. The code in "success" gets executed any time you get a response back from the server (assuming the HTTP header is 200). Does your javascript knows if the server has any error for you? No.

You need your JavaScript to recognize if the validation failed or succeeded. You have many ways to do that. One of these could be sending the message to display followed by a 0 or 1.

So your PHP will looks like:

return "0 " . $errorMessage;

and

return "1 " . $successMessage;

and your javascript should then recognize, with if statement and substring, if the message starts with 0 or with 1.

Thursday, August 25, 2022
 
jchapa
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :