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.

  • Sorted!! Thanks.
  • That's good
  • Ohhk, I am getting back here because I guess the fix was not handled or something breaks while/after uploading the file.
    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.

    image

    Any idea's or suggestions!
  • I've pushed a modified fix for the PDF button:
    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'];
    }
    }

  • Sorry, If you read the previous posted comment.
    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.
  • If it's for some of the PDFs and not all of them, it maybe an issue with how the server is sending the mime types or the browser. Are you certain that the mimes being returned are "application/pdf"? I would debug in the CI_Upload::_file_mime_type() method.
  • Are you certain that the mimes being returned 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
  • CI - debug in _file_mime_type() -> application/octet-stream; charset=binary for the file which is not uploading.
  • Ohhk coming to the main issue of the image not been uploaded in the directory and the data being saved in the database:

    I tired the following code to debug

    function on_after_post($values){
    $CI =& get_instance();
    if (!empty($CI->upload)){
    $data = $CI->upload->data();
    log_message('error',print_r($data, TRUE));
    }else{
    log_message('error','No data in CI upload');
    }
    }


    I get the following in logs.

    Array
    (
    [file_name] =>
    [file_type] =>
    [file_path] =>
    [full_path] =>
    [raw_name] =>
    [orig_name] =>
    [client_name] =>
    [file_ext] =>
    [file_size] =>
    [is_image] =>
    [image_width] =>
    [image_height] =>
    [image_type] =>
    [image_size_str] =>
    )

    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)
  • I guess the issue here was the pdf files I was trying!! I wasted the entire day looking for the solution. These pdf's where not getting uploaded on the very first place. Tried with some other pdf's and the pdf's were visible in the directory

    On debugging the issue, I reached fuel\modules\fuel\libraries\Fuel_assets.php upload function on line no 109


    // upload the file
    145 foreach($_FILES as $key => $file)
    {
    147 if ($file['error'] == 0){
    ...
    }else{
    //The upload was resulting to $file['error'] == 1
    //thhis condition does not exist in the code : I would request the admin to look into this and add a mechanism for pushing some sort of error here for the end user
    }


    Thanks for the efforts.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!