CodeIgniter Forums
How do I validate that a file has been selected. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How do I validate that a file has been selected. (/showthread.php?tid=40968)

Pages: 1 2


How do I validate that a file has been selected. - El Forum - 04-24-2011

[eluser]DaTran[/eluser]
I have this in my controller; as you can see i am using the form validation config file to call all validations.

Code:
if($this->form_validation->run('anime/add') == TRUE){
        
        if($this->anime_model->add($this->input->post())){
          if($this->upload->do_upload('image')){
            $template['result'] = TRUE;
          }
        }
}

How do I use the form validation to "require" the
Code:
<input type="file" name="image" size="20" />
field. (views page)
It keeps saying that there is no image selected.

I am also using MY_Form_validation.php to call in custom validations.


How do I validate that a file has been selected. - El Forum - 04-25-2011

[eluser]toopay[/eluser]
Code:
$this->form_validation->set_rules('image', 'Image', 'required');



How do I validate that a file has been selected. - El Forum - 04-27-2011

[eluser]DaTran[/eluser]
That only works for normal input values. It doesn't work if the input type="file"


How do I validate that a file has been selected. - El Forum - 04-27-2011

[eluser]toopay[/eluser]
Oh crap, sorry about that. Not really follow this thread carefully.

Can you show your full view file?


How do I validate that a file has been selected. - El Forum - 04-27-2011

[eluser]InsiteFX[/eluser]
Check the $_FILE array to see if contains your file!

InsiteFX


How do I validate that a file has been selected. - El Forum - 04-27-2011

[eluser]toopay[/eluser]
InsiteFX, looks like what he want to apply is 'client-side' validation, since he mention...
[quote author="DaTran" date="1303690398"]It keeps saying that there is no image selected.[/quote]
which mean, the server-side did inspect the input (i'm not aware this too at my first response). I bet, what he need is simple Javascript function on his view files, that prevent submiting the form before user select a file!


How do I validate that a file has been selected. - El Forum - 04-27-2011

[eluser]InsiteFX[/eluser]
@toopay,

He is using the upload class, so the only way he check a file is through the $_FILE array.

Or try the helper function:
Code:
$this->upload->data()
This is a helper function that returns an array containing all of the data related to the file you uploaded. Here is the array prototype:

File Uploading Class

InsiteFX


How do I validate that a file has been selected. - El Forum - 04-27-2011

[eluser]DaTran[/eluser]
I think that is just what I needed however, I have an issue with form_validation when it is empty the MY_Form_validation callback I made will never be called to check if the file is selected or not. So no error message is displayed when the input type="file" is empty.

Here is my callback in my MY_Form_validaiton

Code:
function image($str){
    $this->CI->form_validation->set_message('image', 'No %s has been selected');
    if($this->upload->data()){
      return TRUE;
    }else{
      return FALSE;
    }
  }

here is more information if it helps (the controller):
Code:
if($this->input->post())
    {
      if($this->form_validation->run('anime/add') == TRUE){
        
        if($this->anime_model->add($this->input->post())){
          if($this->upload->do_upload('image')){
            $template['result'] = TRUE;
          }
        }
      }
    }

the config form_validation.php
Code:
$config = array(
  // ADD ANIME
  'anime/add' => array(
    array(
     'field'   => 'image',
     'label'   => 'Image',
     'rules'   => 'image'
    ),
    array(
     'field'   => 'title',
     'label'   => 'Title',
     'rules'   => 'trim|url_title|required|xss_clean|unique[anime.title]'
    ),
    array(
     'field'   => 'aka',
     'label'   => 'Aka',
     'rules'   => ''
    ),
    array(
     'field'   => 'slug',
     'label'   => 'User friendly URL',
     'rules'   => 'trim|required|xss_clean|url_title|unique[anime.slug]'
    ),  
    array(
     'field'   => 'yp',
     'label'   => 'Year Published',
     'rules'   => 'trim|required|integer|min_length[4]'
    ),
    array(
     'field'   => 'noe',
     'label'   => 'Number of episodes',
     'rules'   => 'trim|integer'
    ),
    array(
     'field'   => 'summary',
     'label'   => 'Summary',
     'rules'   => 'trim|required|xss_clean'
    )
  ),



How do I validate that a file has been selected. - El Forum - 04-27-2011

[eluser]InsiteFX[/eluser]
Try this:
Code:
'anime/add' => array(
    array(
     'field'   => 'image',
     'label'   => 'Image',
     'rules'   => 'image|callback_image'
    ),

InsiteFX


How do I validate that a file has been selected. - El Forum - 04-27-2011

[eluser]DaTran[/eluser]
It didn't work sorry.