Cannot read property 'replace' of undefined when adding field type as file
Hello admin,
I just came across an issue where when I set the module field type as file I get 'Cannot read property 'replace' of undefined' in browser console.
The error is pointed on to
var replacePlaceholders = function(folder, context){
// now replace any placeholder values in the folder... required for new pages that may not have a value
var $inputs = $(context).closest('form').find('select, textarea')
.add('input').not('input[type="radio"], input[type="checkbox"], input[type="button"]')
.add('input[type="radio"]:checked, input[type="checkbox"]:checked');
var replaceValues = {};
$inputs.each(function(i){
var id = ($(this).is('input[type="radio"], input[type="checkbox"]')) ? $(this).attr('name'): $(this).attr('id');
if (id){
var idArr = id.split('--');
id = idArr[idArr.length -1];
if (id){
replaceValues[id] = $(this).val();
var regex = new RegExp('\{' + id + '\}', 'g');
folder = folder.replace(regex, replaceValues[id]);
}
}
})
return folder;
}
line - folder = folder.replace(regex, replaceValues[id]);
My settings for the module field is as follows:
$fields['pdf_file']['type'] = 'file';
$fields['pdf_file']['accept'] = 'pdf';
$fields['pdf_file']['class'] = 'asset_select pdf/financial_quaterly_returns';
$fields['pdf_file']['folder'] = 'pdf/financial_quaterly_returns/';
Also in the admin view while creating a record I can see select PDF button 2 times which should be upload PDF and Select PDF.
Comments
https://github.com/daylightstudio/FUEL-CMS/commit/3e29c849c6418ff8bc89e1ad49675cd78096bc06
Here is the screenshot for the same, where I am trying to upload a file but
firstly the upload fails (here I am getting no error in logs) and
secondly I can see 2 times select pdf button
Also the select pdf button which populates the path of the selected image is not populating the path as well and hence the select pdf functionality is also not working as expected.
Any idea's or suggestions!
https://github.com/daylightstudio/FUEL-CMS/commit/9db1855454e3ce047a1576ad72cbd04336954b98
Regarding the error, FUEL currently doesn't support required file upload fields since it only checks the $_POST request for validation. It does the uploading of files after the save. A way around this would be to check the $_FILES value in an on_before_validate model hook to see if it exists and then merge it into the $values array that gets returned by the on_before_validate hook:
public function on_before_validate($values) { if (isset($_FILES['pdf_file'])) { $values['pdf_file'] = $_FILES['pdf_file']['name']; } }
Multiple select PDF button issue is sorted, also the passing of filename in the values also worked and I get the full filename below the upload field after the save action.
But the file is not saved in the given location. File does not exist in the location, do I have to handle the upload myself?
Also I get 'The filetype you are attempting to upload is not allowed' for some of the pdf's that I am trying to upload. (I don't think this is related to the current discussion - any hints)
The mime type of both uploaded and not uploaded files are application/pdf.
I tried few of the online tools to check the mime-type, I am not sure how concrete is the methods and I got application/pdf
This issue is secondary - some files are giving positive response with a session flash set as 'Data has been saved' . I will still try and debug in _file_mime_type() method
Main issue which needs your attention - the file is not saved in the given location,
even after a positive response - 'Data has been saved'
The data is saved in the database, the folder is writable as well
I tired the following code to debug I get the following in logs. Any hint, what could be the problem here: ideally the upload data should be visible here as I have seen blog post for creating thumbnail based on the data being available in
CI->upload->data() (https://www.getfuelcms.com/blog/2010/12/19/learning-fuel-cms-part-3-creating-modules)
On debugging the issue, I reached fuel\modules\fuel\libraries\Fuel_assets.php upload function on line no 109 Thanks for the efforts.