3

civix generate:form creates a basic form with a buildQuickForm() and postProcess() demonstrating a select option.

I want the form to upload a file and save it to a custom field.

If I add: $this->add('file', 'file_id', "My document") then I get a normal file upload element on the form, but the problem is what to do with it in the postProcess.

$this->controller->exportValues() does not include my file_id although it is listed in $this->getSubmitValues(TRUE) with the the original file name and tmp_name.

I understand QuickForm does some filtering of submitted values but how do I stop the file details being filtered out and get it saved as a custom field?

1 Answer 1

4

I just created a custom extension that uploads a CSV file and reads it into a table.

I added the file element in buildQuickForm as follows

  $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);

And the processing of the file was is done with

public function preProcess()
{
    if (isset($this->_submitFiles['uploadFile'])) {
        $uploadFile = $this->_submitFiles['uploadFile'];
        $importer = new CRM_Importer();
        $importer ->truncate();
        $importer -> importCVStoTable($uploadFile['tmp_name']);
    }
}

I think the key element is $this->_submitFiles and you already found about the tmp_name

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.