0

I'm trying to upload several files with their names in different field. example--- there are 3 form fields for inserting names and 3 fields for uploading files in there respective order. this is image attached, fr what i'm trying to achieve --

https://i.stack.imgur.com/QaZFn.jpg

Now what i want is, when a user enters name in "document name" label in first field and selects a file to upload in "document file" label first field, then data gets inserted. else shows an error. user should enter file name select file in there respective order, else it should show error. it should error when a user enters name in first field and selects a file in second field then it should display error. this is being done opencart admin section.

currently this is the simple code for simple validation --

foreach ($this->request->post['document_description'] as $language_id => $value) {
        if ((utf8_strlen($value['name']) < 3) || (utf8_strlen($value['name']) > 64)) {
            $this->error['name'][$language_id] = $this->language->get('error_name');
        }
    }

One thing is common between file name field and form upload field is "language id".

code for documents.php are ---

protected function getForm() {
    $this->data['heading_title'] = $this->language->get('heading_title');

    $this->data['entry_name'] = $this->language->get('entry_name');
    $this->data['entry_filename'] = $this->language->get('entry_filename');
    $this->data['entry_mask'] = $this->language->get('entry_mask');
    $this->data['entry_remaining'] = $this->language->get('entry_remaining');
    $this->data['entry_update'] = $this->language->get('entry_update');

    $this->data['button_save'] = $this->language->get('button_save');
    $this->data['button_cancel'] = $this->language->get('button_cancel');
    $this->data['button_upload'] = $this->language->get('button_upload');

    if (isset($this->error['warning'])) {
        $this->data['error_warning'] = $this->error['warning'];
    } else {
        $this->data['error_warning'] = '';
    }

    if (isset($this->error['name'])) {
        $this->data['error_name'] = $this->error['name'];
    } else {
        $this->data['error_name'] = array();
    }

    if (isset($this->error['filename'])) {
        $this->data['error_filename'] = $this->error['filename'];
    } else {
        $this->data['error_filename'] = '';
    }


    $this->data['breadcrumbs'] = array();

    $this->data['breadcrumbs'][] = array(
        'text'      => $this->language->get('text_home'),
        'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
        'separator' => false
    );

    $this->data['breadcrumbs'][] = array(
        'text'      => $this->language->get('heading_title'),
        'href'      => $this->url->link('catalog/documents', 'token=' . $this->session->data['token'] . $url, 'SSL'),           
        'separator' => ' :: '
    );

    if (!isset($this->request->get['document_id'])) {
        $this->data['action'] = $this->url->link('catalog/documents/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
    } else {
        $this->data['action'] = $this->url->link('catalog/documents/update', 'token=' . $this->session->data['token'] . '&document_id=' . $this->request->get['document_id'] . $url, 'SSL');
    }

    $this->data['cancel'] = $this->url->link('catalog/documents', 'token=' . $this->session->data['token'] . $url, 'SSL');

    $this->load->model('localisation/language');

    $this->data['languages'] = $this->model_localisation_language->getLanguages();

    if (isset($this->request->get['document_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
        $document_info = $this->model_catalog_documents->getDocument($this->request->get['document_id']);
    }

    $this->data['token'] = $this->session->data['token'];

    if (isset($this->request->get['document_id'])) {
        $this->data['document_id'] = $this->request->get['document_id'];
    } else {
        $this->data['document_id'] = 0;
    }

    if (isset($this->request->post['document_description'])) {
        $this->data['document_description'] = $this->request->post['document_description'];
    } elseif (isset($this->request->get['document_id'])) {
        $this->data['document_description'] = $this->model_catalog_documents->getDocumentDescriptions($this->request->get['document_id']);
    } else {
        $this->data['document_description'] = array();
    }   

    if (isset($this->request->post['filename'])) {
        $this->data['filename'] = $this->request->post['filename'];
    } elseif (!empty($document_info)) {
        $this->data['filename'] = $document_info['filename'];
    } else {
        $this->data['filename'] = '';
    }

    if (isset($this->request->post['mask'])) {
        $this->data['mask'] = $this->request->post['mask'];
    } elseif (!empty($document_info)) {
        $this->data['mask'] = $document_info['mask'];       
    } else {
        $this->data['mask'] = '';
    }

    if (isset($this->request->post['remaining'])) {
        $this->data['remaining'] = $this->request->post['remaining'];
    } elseif (!empty($document_info)) {
        $this->data['remaining'] = $document_info['remaining'];
    } else {
        $this->data['remaining'] = 1;
    }

    if (isset($this->request->post['update'])) {
        $this->data['update'] = $this->request->post['update'];
    } else {
        $this->data['update'] = false;
    }

    $this->template = 'catalog/documents_form.tpl';
    $this->children = array(
        'common/header',
        'common/footer'
    );

    $this->response->setOutput($this->render());    
}


protected function validateForm() { 
        if (!$this->user->hasPermission('modify', 'catalog/documents')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }
        print_r($this->request->post['document_description']); echo "<br>";
        print_r($this->request->files['document_files']); echo "<br>";
        foreach ($this->request->files['document_files'] as $files => $value)
        {
            print_r($files.'==>'.print_r($value)); echo "<br>"; 
        }die;

        foreach ($this->request->post['document_description'] as $language_id => $value) {
            foreach ($this->request->files['document_files'] as $selected => $filename)
            {
                if ((utf8_strlen($value['name']) < 3) || (utf8_strlen($value['name']) > 64)) 
                {   
                    $this->error['name'][$language_id] = $this->language->get('error_name');
                }
                if (empty($filename))
                {
                    $this->error['name'][$language_id] = $this->language->get('error_name');
                }
            }
        }   

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }


public function upload() {
        $this->language->load('sale/order');

        $json = array();

        if (!$this->user->hasPermission('modify', 'catalog/documents')) {
            $json['error'] = $this->language->get('error_permission');
        }   

        if (!isset($json['error'])) {   
            if (!empty($this->request->files['file']['name'])) {
                $filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'));

                if ((utf8_strlen($filename) < 3) || (utf8_strlen($filename) > 128)) {
                    $json['error'] = $this->language->get('error_filename');
                }       

                // Allowed file extension types
                $allowed = array();

                $filetypes = explode("\n", $this->config->get('config_file_extension_allowed'));

                foreach ($filetypes as $filetype) {
                    $allowed[] = trim($filetype);
                }

                if (!in_array(substr(strrchr($filename, '.'), 1), $allowed)) {
                    $json['error'] = $this->language->get('error_filetype');
                }   

                // Allowed file mime types      
                $allowed = array();

                $filetypes = explode("\n", $this->config->get('config_file_mime_allowed'));

                foreach ($filetypes as $filetype) {
                    $allowed[] = trim($filetype);
                }

                if (!in_array($this->request->files['file']['type'], $allowed)) {
                    $json['error'] = $this->language->get('error_filetype');
                }

                if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
                    $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
                }

                if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
                    $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
                }
            } else {
                $json['error'] = $this->language->get('error_upload');
            }
        }

        if (!isset($json['error'])) {
            if (is_uploaded_file($this->request->files['file']['tmp_name']) && file_exists($this->request->files['file']['tmp_name'])) {
                $ext = md5(mt_rand());

                $json['filename'] = $filename . '.' . $ext;
                $json['mask'] = $filename;

                move_uploaded_file($this->request->files['file']['tmp_name'], DIR_DOWNLOAD . $filename . '.' . $ext);
            }

            $json['success'] = $this->language->get('text_upload');
        }   

        $this->response->setOutput(json_encode($json));
    }
Pankaj Singh
  • 531
  • 6
  • 21
  • 1
    I think a better option is to show the `document name` field after selecting the file, which you can do as given in this link: http://stackoverflow.com/questions/5670769/jquery-detecting-if-a-file-has-been-selected-in-the-file-input. – Sankar V Nov 20 '13 at 07:42
  • @SankarV --- thanks fr ur reply. Actually what i'm trying to do is firstly, information will be inserted through this form, and secondly same form is used for updating information. One thing is common between file name field and form upload field is "language id". – Pankaj Singh Nov 20 '13 at 08:19
  • That I know. I meant hide the `document name` field initially and show it after selecting the file. – Sankar V Nov 20 '13 at 09:03
  • Usually this is done by displaying **only one row** with name text field and file input, and after both are filled either automatically add another row, or display a button *Add next file* after clicking on which a new row will be displayed... In this case it is better UX, though You still have to do the validation... – shadyyx Nov 20 '13 at 09:23
  • @shadyyx --- here, I'm uploading product description document in different languages. which will then be added to the product for each different language. so all these fields gets automatically added when a new language is installed in opencart. so that product description can be added in that language too. everything is working fine, but when i write a name in first form field and then upload a file for another langauge, then no error messages is being displayed. that's why i want to add a validation. – Pankaj Singh Nov 20 '13 at 09:38
  • Place the `document name` fields next to the `document file` field and hide it at first. Then display the `document name` fields when file is selected (use the code in my 1st comment). Validate in such a way that both name and filename are not empty for each language id. Add language based validate conditions in `validateForm` function the below `foreach`: `foreach ($this->request->post['product_description'] as $language_id => $value) {` – Sankar V Nov 20 '13 at 10:11
  • @SankarV --- that's the thing. that's what i need. I don't know how to validate them both at once. i need code for validation. And thanks once again fr ur reply – Pankaj Singh Nov 20 '13 at 10:15

1 Answers1

0

Place the document name fields next to the document file field and hide it at first. Then display the document name fields when file is selected (jQuery - Detecting if a file has been selected in the file input).

In admin/controller/catalog/product.php, there is a function: validateForm where all validations are done. Within that function a foreach is used for language based validations:

foreach ($this->request->post['product_description'] as $language_id => $value) {
...........
//add your validation code here like:
    if (trim($value['download_name']) == '' && $value['download_file'] == '' ) {
        $this->error['downloadfile'][$language_id] = $this->language->get('error_ownloadfile');
    }
...........

}// end of foreach
Community
  • 1
  • 1
Sankar V
  • 4,110
  • 5
  • 28
  • 52