Asked  7 Months ago    Answers:  5   Viewed   34 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

36

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');
}
Wednesday, March 31, 2021
 
relyt
answered 7 Months ago
61

you could create a new library in application/libriries and name the file MY_Form_validation

What you are doing here is extending the form_validation class so that you will not need to mess with the core files.

The MY_ is what is set on your config, be sure to check it if you changed yours.

sample MY_Form_validation.php

class MY_Form_validation Extends CI_Form_validation
{
   //this is mandatory for this class
   //do not forget this or it will not work
   public function __construct($rules = array(){
        parent::__construct($rules);
        $this->CI->lang->load('MY_form_validation');
    }
   public function method1($str){
     return $str == '' ? FALSE : TRUE;
   }
   pulic function method2($str)
   {
       //if you want a validation from database
       //you can load it here
       // or check the `form_validation` file on `system/libraries/form_validation`

  }

  public function check_something_with_post($tr)
  {
      return $this->CI->input->post('some_post') == FALSE ? FALSE : TRUE;
  }
}

Basically, when you call a rule sample method1|method2 the value of your post field will be the parameter of the method. if you want to check other post you can do it by using $this->CI->input->post('name of the post');

when you want to pass a parameter just look at the form validation is_unique or unique code on system/libraries/form_validation you will have an idea.

To create a error message that goes with it go to application/language/english/MY_Form_validation_lang

Sample MY_form_validation_lang.php

$lang['method1'] = "error error error.";
$lang['method2'] = "this is an error message.";

if english does not exist on your application/language just create it.

check more atCreating libraries

NOTE:

On some linux or debian server you may want to change the file name from MY_Form_validation to MY_form_validation note the small f on the word form.

Wednesday, March 31, 2021
 
John_BSDthos
answered 7 Months ago
60

The validation rules you are using are correct. Just remove few rules those are not required

$this->form_validation->set_rules('email', '<b>email</b>', 'required|valid_email|callback_mail_check');

The callback automatically add the parameter of current validation. You don't need to read it from GET/POST method.

public function mail_check($email)
{
    if (!$this->users_model->get_user_by_email_address($email)) {

        $this->form_validation->set_message(__FUNCTION__, 'Your <b>email</b> could not be found.');
        return false;
    }
    return true;
}
Saturday, May 29, 2021
 
davidb
answered 5 Months ago
79

I got this issue solved.In config/mimes.php modified $mimes array

     $mimes = array('ini' => 'application/octet-stream')

If any one knows how to solve this issue without modifying the core files, *Please do comment.*

Saturday, May 29, 2021
 
etsous
answered 5 Months ago
82

CodeIgniter doesn't support multiple files. Using the do_upload() in a foreach won't be different than using it outside.

You will need to deal with it without the help of CodeIgniter. Here's an example https://github.com/woxxy/FoOlSlide/blob/master/application/controllers/admin/series.php#L331-370

Friday, August 20, 2021
 
Cam1989
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share