• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using the file upload class and validation class together.

#1
[eluser]eedfwChris[/eluser]
I'm a little confused as to how to get the two to work seemlessly together. I have a form with a simple upload field for a CSV file. The callback would be analized to be sure it has the right columns and all that jazz.

My question is, should you use the file upload in a callback? If so, am I doing anything right below?

I'm using the alternative view class.
Code:
/**
* ----------------------------------------------------
* Validate Form
* ----------------------------------------------------
* Validates the RTD search form.
*
*/
function _validate_form()
{
    $this->load->library('validation');

    /* Set field names */
    $fields['name']         = "Site/Community Name";
    $fields['state']         = "State";
    $fields['file_upload']    = "CSV File";

    $this->validation->set_fields($fields);

    /* Set validation parameters */
    $rules['name']             = "required|max_length[50]";
    $rules['state']            = "required|max_length[2]|min_length[2]";
    $rules['file_upload']     = "callback__validate_csv";

    $this->validation->set_rules($rules);

    /* Run validation */
    if ($this->validation->run() == FALSE)
    {
        $this->view->set('validation_error', $this->validation->error_string);
        $this->view->part('body', 'rtd/rtd_search_form_view');
    }
    else
    {
        echo 'true';
    }
}

/**
* ----------------------------------------------------
* Validate CSV
* ----------------------------------------------------
* Validates the uploaded CSV file to ensure that the proper type of CSV is uploaded (or it is a CSV at all)
*
*/

function _validate_csv()
{
    $config['upload_path'] = './rtd_tmp/';
    $config['allowed_types'] = 'csv';

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

    if ($this->upload->do_upload() == FALSE)
    {
        $this->view->append('validation_error', 'CSV is invalid');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

#2
[eluser]eedfwChris[/eluser]
Ok I have done a bit of tinkering and this is what I have come up with... It seems logical but I am still curious if you can instead use a callback function...
Code:
/**
* ----------------------------------------------------
* Validate Form
* ----------------------------------------------------
* Validates the RTD search form.
*
*/
function _validate_form()
{
    $this->load->library('validation');

    /* Set field names */
    $fields['name']         = "Site/Community Name";
    $fields['state']         = "State";
    //$fields['userfile']    = "CSV File";

    $this->validation->set_fields($fields);

    /* Set validation parameters */
    $rules['name']             = "required|max_length[50]";
    $rules['state']            = "required|max_length[2]|min_length[2]";
    //$rules['userfile']     = "required";

    $this->validation->set_rules($rules);

    /* Run validation */
    if ($this->validation->run() == FALSE)
    {
        $this->view->set('validation_error', $this->validation->error_string);
        $this->view->part('body', 'rtd/rtd_search_form_view');
    }
    else
    {
        $this->_validate_csv();
    }
}

/**
* ----------------------------------------------------
* Validate CSV
* ----------------------------------------------------
* Validates the uploaded CSV file to ensure that the proper type of CSV is uploaded (or it is a CSV at all)
*
*/

function _validate_csv()
{
    $config['upload_path'] = './rtd_tmp/';
    $config['allowed_types'] = 'csv';

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

    if ($this->upload->do_upload() == FALSE)
    {
        $this->view->set('validation_error', $this->upload->display_errors());
        $this->view->part('body', 'rtd/rtd_search_form_view');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

But I am getting a "The upload path does not appear to be valid." error...

Where exactly does codeigniter want the upload directory? I have my file path is like this

-System
---application
------controller
---rtd_tmp
-Webroot


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.