Welcome Guest, Not a member yet? Register   Sign In
file upload validation
#11

[eluser]Unknown[/eluser]
I notice that when using rules config file the validation is not workin.

To fix it just add the rules parameter in the constructor.

Change This

Code:
function __construct()
    {
        parent::CI_Form_validation();
    }

For this

Code:
function __construct($rules = array())
    {
        parent::CI_Form_validation($rules);
    }

Hope this help!
#12

[eluser]nomikos3[/eluser]
Fake $_POST is a bad practice... Like all fakes Sad

For validate files you only need to load the upload library and config your preferences (allowed_types, max_size, etc..., see preferences in http://ellislab.com/codeigniter/user-gui...ading.html). and show errors trough a simple message:

Code:
# in controller:
$config['upload_path'] = UPLOADS_TMP;
$config['allowed_types'] = 'xls';
$config['max_size']    = '2048';
// etc...

$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
    $data['file_error'] = $this->upload->display_errors();
    $this->load->view('file_form', $data);

# in view:
if (isset($file_error))
    echo $file_error;

If you need validate others fields add Form_validation and manage errors as usual.

Code:
# in view:
echo validation_errors();
if (isset($file_error))
    echo $file_error;
#13

[eluser]stondin[/eluser]
Its entirely understandable to think that you validate a form and its inputs with the form validation class and you display error from a file upload using file upload class however.... The situation that constantly arises, or at least in my experience, is that you usually have a file upload within a form that may fail validation.
If the file uploads correctly, great, but a user causes the validation to fail elsewhere in the form (eg, forgot to add a required title) what happens to the file?
The user is presented with an new file input! Is there an easy solution that I'm missing?
Currently I store the uploaded file ID somewhere and hang onto it until the form passes all validation. There just seems to be a real disconnect. Thoughts? Alternative solutions?
#14

[eluser]Unknown[/eluser]
Not sure if you found your solution here, but there is no way to re-populate the upload field unfortunately. It's a security measure with all browsers. Imagine what you could access if you "could" re-populate the upload field. You would essentially have access to the entire remote users computer, and could easily change the path during the validation, allowing you to steal any file, or the entire hardrive for that matter. You could try using javascript, jquery, or ajax to validate the field before the submit button would actually submit and refresh your page. That would at least let the person know ahead of time. Hope this helps. I sure spun my wheels on it for a few minutes. ;-) Now I'm just working on validating a combination of upload fields with if statements which is fun. If this one is blank, require the other one, so on and so on.
#15

[eluser]Syed Rakib Al Hasan[/eluser]
here is how I did my validation of the form entries and file entries.... it works pretty well
Code:
<?php
class Upload_controller extends CI_Controller {

    function __construct(){
        parent::__construct();
    }

    function index(){
        $form_validation_config = array(
             /* form_validation rules HERE */
        );
        $this->load->library('form_validation');
        $this->form_validation->set_rules($form_validation_config);
        if (isset($_POST['submit']) && $this->form_validation->run() == TRUE){
            /*
             * 'submit' is the name of the submit button in the form
             * if the SUBMIT button in the form was pressed
             * AND
             * if the form_validation_rules were satisfied,
             * then perform file upload validation
             */
            $this->perform_file_upload_validation();
        }
        else{
            /*
             * OTHERWISE, load the upload form view
             * eventually, this is the default landing page of this class
             */
            $this->load->view('upload_form');
        }
    }

    private function perform_file_upload_validation(){
        $this->load->library('upload');
        $config_img = array (
             /* file_upload_validation rules HERE */            
        )
        $this->upload->initialize($config_img);
        $img_uploaded = $this->upload->do_upload("image");
        $img_upload_data = $this->upload->data();
        $img_upload_error = $this->upload->display_errors('<font color="red">', '</font>');
        if ($img_uploaded){
            /*
             * if image upload is SUCCESSFUL,
             * load the upload successful view
             * and pass the uploaded file data to that view
             * if you want to show info of the uploaded data
             */
            $this->load->view('upload_success' , array ('upload_data'=>$img_upload_data));
        }
        else{
            /*
             * if image upload is a FAILURE,
             * load the upload form view again with the error logs
             * if you want to display the errors incurred
             */
            $error['IMAGE'] = $img_upload_error;
            $this->load->view('upload_form' , array ('error'=>$error));
        }
    }
}
?&gt;
hope it helps




Theme © iAndrew 2016 - Forum software by © MyBB