Welcome Guest, Not a member yet? Register   Sign In
Form Validation Class
#1

[eluser]omed habib[/eluser]
Does this class also work with file uploads?
#2

[eluser]xwero[/eluser]
No the file upload class has it's own validation.
#3

[eluser]omed habib[/eluser]
Yes, but I'm trying to integrate the two. I'm trying to get the validation class to also validate a file.

For the file field, I'm trying to do a callback to another function that does that validation for the file... however, it doesn't seem to recognize the 'file' field. Any ideas?
#4

[eluser]xwero[/eluser]
The validation class handles strings, I think that is why the people at Ellislab seperated the two. If you mix the two, the validation class also has to take care of the upload itself.

I wondering how much of the file upload class you are going to use in the validation class? And how are you going to work out if people want to rename the file, resize it, ... ?

It would be nice to have one validation class for everything so i'm looking forward to your results.
#5

[eluser]xwero[/eluser]
The upload is not in the $_POST global but in the $_FILES global. So you need some way to address that global in a rule.

I think the simplest way to do this is to add your own rule in an extended CI_Validation class
Code:
class MY_Validation extends CI_Validation
{
     function MY_Validation()
     {
         parent::CI_Validation();
     }

     function upload($params)
     {
        if(!isset($_FILES) || count($_FILES) == 0)
        {
          return false;
        }
        
        $params = explode(',',$params);
        // $_FILES checking
        if(!isset($_FILES[$params[0]]))
        {
          return false;
        }
     }
}
The first parameter of the rule has to be the fieldname to check if it's the right field you are checking. It's a bit redundant but this is a validation class hack so it's not perfect.
#6

[eluser]omed habib[/eluser]
The validation class does not recognize the <input type='file'> type... therefore creating a callback function wont' even work because the function wont even be called.

The best I could do was the following in the controller:


Code:
if ( $this->validation->run() == FALSE)    {
            $data['error'] = array('error' => $this->upload->display_errors());
            // Load content
            $data['bodycontent'] = $this->load->view('manage/clients/addnewclient', $data, true);
    
        // If the form STRING validation passed correctly.
        } else {
            // Check for the file upload validation
            if (!$this->upload->do_upload()) {
                $data['error'] = array('error' => $this->upload->display_errors());
                // Load content
                $data['bodycontent'] = $this->load->view('manage/clients/addnewclient', $data, true);
            } else {
                // If everything passed correctly, great!
                $data = array('upload_data' => $this->upload->data());
                // Load content
                $data['bodycontent'] = $this->load->view('manage/clients/addnewclient_success', $data, true);
            }


Thanks for your help!!
#7

[eluser]xwero[/eluser]
[quote author="omed" date="1195570566"]
Code:
if ( $this->validation->run() == FALSE)    {
            $data['error'] = array('error' => $this->upload->display_errors());
            // Load content
            $data['bodycontent'] = $this->load->view('manage/clients/addnewclient', $data, true);
    
        // If the form STRING validation passed correctly.
        } else {
            // Check for the file upload validation
            if (!$this->upload->do_upload()) {
                $data['error'] = array('error' => $this->upload->display_errors());
                // Load content
                $data['bodycontent'] = $this->load->view('manage/clients/addnewclient', $data, true);
            } else {
                // If everything passed correctly, great!
                $data = array('upload_data' => $this->upload->data());
                // Load content
                $data['bodycontent'] = $this->load->view('manage/clients/addnewclient_success', $data, true);
            }
[/quote]
shouldn't it be
Code:
if ( $this->validation->run() == FALSE)    {
            $data['error'] = array('error' => $this->validation->error_string); // changed
            // Load content
            $data['bodycontent'] = $this->load->view('manage/clients/addnewclient', $data, true);
    
        // If the form STRING validation passed correctly.
        } else {
            // Check for the file upload validation
            if (!$this->upload->do_upload()) {
                $data['error'] = array('error' => $this->upload->display_errors());
                // Load content
                $data['bodycontent'] = $this->load->view('manage/clients/addnewclient', $data, true);
            } else {
                // If everything passed correctly, great!
                $data = array('upload_data' => $this->upload->data());
                // Load content
                $data['bodycontent'] = $this->load->view('manage/clients/addnewclient_success', $data, true);
            }
but i think you can better do something like
Code:
$data['error'] = array();
if ( $this->validation->run() == FALSE)    {
            $data['error'] = array('error' => $this->validation->error_string);
}
if (!$this->upload->do_upload()) {
                $data['error'] = array('error' => $this->upload->display_errors());
}
if(count($data['error']) == 0)
{
$data = array('upload_data' => $this->upload->data());
                // Load content
                $data['bodycontent'] = $this->load->view('manage/clients/addnewclient_success', $data, true);
}
else
{
$data['bodycontent'] = $this->load->view('manage/clients/addnewclient', $data, true);
}
#8

[eluser]omed habib[/eluser]
That was awesome, much cleaner and more efficient w/ no redundancies. Thanks!!




Theme © iAndrew 2016 - Forum software by © MyBB