Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-69187

Filepicker: file type validation behaves inconsistently depending on how the list of accepted types is passed

XMLWordPrintable

    • MOODLE_39_STABLE
    • MOODLE_401_STABLE, MOODLE_402_STABLE
    • MDL-69187-401
    • Hide
      1. Log in as admin
      2. Make the following change (string accepted types):

        index c7ee781ff20..a1d3d58b857 100644
        --- a/admin/tool/admin_presets/classes/form/import_form.php
        +++ b/admin/tool/admin_presets/classes/form/import_form.php
        @@ -43,7 +43,7 @@ class import_form extends moodleform {
         
                 // File upload.
                 $mform->addElement('filepicker', 'xmlfile',
        -            get_string('selectfile', 'tool_admin_presets'));
        +            get_string('selectfile', 'tool_admin_presets'), null, ['accepted_types' => '.txt, .xml']);
                 $mform->addRule('xmlfile', null, 'required');
         
                 $this->add_action_buttons(true, get_string('import', 'tool_admin_presets'));
        

      3. Navigate to Site admin presets from site administration
      4. Press Import preset
      5. Confirm you see the following:

        Accepted file types:
         
            application/xml .xml
            Text file .txt
        

      6. Upload Frog.jpg
      7. Confirm you see the error: "Image (JPEG) filetype cannot be accepted."
      8. Upload Demo.xml and press Import
      9. Confirm file was imported successfully
      10. Make the following change (array accepted types):

        --- a/admin/tool/admin_presets/classes/form/import_form.php
        +++ b/admin/tool/admin_presets/classes/form/import_form.php
        @@ -43,7 +43,7 @@ class import_form extends moodleform {
         
                 // File upload.
                 $mform->addElement('filepicker', 'xmlfile',
        -            get_string('selectfile', 'tool_admin_presets'));
        +            get_string('selectfile', 'tool_admin_presets'), null, ['accepted_types' => ['.txt', '.xml']]);
                 $mform->addRule('xmlfile', null, 'required');
         
                 $this->add_action_buttons(true, get_string('import', 'tool_admin_presets'));
        

      11. Repeat steps 3. to 9. above, ensuring identical behaviour
      Show
      Log in as admin Make the following change ( string accepted types): index c7ee781ff20..a1d3d58b857 100644 --- a/admin/tool/admin_presets/classes/form/import_form.php +++ b/admin/tool/admin_presets/classes/form/import_form.php @@ -43,7 +43,7 @@ class import_form extends moodleform { // File upload. $mform->addElement('filepicker', 'xmlfile', - get_string('selectfile', 'tool_admin_presets')); + get_string('selectfile', 'tool_admin_presets'), null, ['accepted_types' => '.txt, .xml']); $mform->addRule('xmlfile', null, 'required'); $this->add_action_buttons(true, get_string('import', 'tool_admin_presets')); Navigate to Site admin presets from site administration Press Import preset Confirm you see the following: Accepted file types:   application/xml .xml Text file .txt Upload Frog.jpg Confirm you see the error: "Image (JPEG) filetype cannot be accepted." Upload Demo.xml and press Import Confirm file was imported successfully Make the following change ( array accepted types): --- a/admin/tool/admin_presets/classes/form/import_form.php +++ b/admin/tool/admin_presets/classes/form/import_form.php @@ -43,7 +43,7 @@ class import_form extends moodleform { // File upload. $mform->addElement('filepicker', 'xmlfile', - get_string('selectfile', 'tool_admin_presets')); + get_string('selectfile', 'tool_admin_presets'), null, ['accepted_types' => ['.txt', '.xml']]); $mform->addRule('xmlfile', null, 'required'); $this->add_action_buttons(true, get_string('import', 'tool_admin_presets')); Repeat steps 3. to 9. above, ensuring identical behaviour

      This issue is found while trying to correct MDL-69141.

      If a form specifies the valid extensions in the file picker as: 

      $options['accepted_types'] = '.html, .txt';
      

       The file picker does not show the valid extensions to the user and the form can be submitted

      but, if the user tries to upload (and submit the form) an invalid file gets an inline red error:

      On the other hand, if the same form specifies the extensions like this:

      $options['accepted_types'] = ['.html', '.txt'];
      

       The file picker shows the valid extensions correctly

      but, when the user uploads an invalid file, get a modal error instantly:

      Possible solutions

      1. Unify the error messages format to have a coherent UX
      2. In both string or array accepted_type, the file picker should show the valid extensions under the file picker.

       

       

        1. 0001-MDL-69187-core_file-Unit-test-for-file_get_typegroup.patch
          2 kB
        2. Demo.xml
          0.5 kB
        3. Frog.jpg
          Frog.jpg
          70 kB
        4. MDL-69187.png
          MDL-69187.png
          959 kB
        5. photo_2020-07-01_11-25-29.jpg
          photo_2020-07-01_11-25-29.jpg
          20 kB
        6. photo_2020-07-01_11-27-05.jpg
          photo_2020-07-01_11-27-05.jpg
          52 kB
        7. screenshot_084.png
          screenshot_084.png
          17 kB
        8. screenshot_085.png
          screenshot_085.png
          19 kB
        9. screenshot_086.png
          screenshot_086.png
          21 kB
        10. screenshot_087.png
          screenshot_087.png
          20 kB

            pholden Paul Holden
            tusefomal Ferran Recio
            Andrew Lyons Andrew Lyons
            Huong Nguyen Huong Nguyen
            Ron Carl Alfon Yu Ron Carl Alfon Yu
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 2 hours, 18 minutes
                2h 18m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.