Ashraf Hefny Ashraf Hefny - 2 months ago 15
PHP Question

Form element filepicker and file manager are not displaying by $mform->addElement('filepicker' .... )

I am trying to add upload file input in signup form in moodle v3

By using

$mform->addElement('filepicker' .... )

But I am getting this error:

Error code: noguest

Stack trace:

line 488 of /lib/setuplib.php: moodle_exception thrown
line 348 of /lib/filelib.php: call to print_error()
line 131 of /lib/form/filepicker.php: call to file_get_unused_draft_itemid()
line 189 of /lib/pear/HTML/QuickForm/Renderer/Tableless.php: call to MoodleQuickForm_filepicker->toHtml()
line 2806 of /lib/formslib.php: call to HTML_QuickForm_Renderer_Tableless->renderElement()
line 408 of /lib/pear/HTML/QuickForm/element.php: call to MoodleQuickForm_Renderer->renderElement()
line 1639 of /lib/pear/HTML/QuickForm.php: call to HTML_QuickForm_element->accept()
line 1714 of /lib/formslib.php: call to HTML_QuickForm->accept()
line 1682 of /lib/pear/HTML/QuickForm.php: call to MoodleQuickForm->accept()
line 442 of /lib/pear/HTML/Common.php: call to HTML_QuickForm->toHtml()
line 204 of /lib/pear/HTML/QuickForm/DHTMLRulesTableless.php: call to HTML_Common->display()
line 933 of /lib/formslib.php: call to HTML_QuickForm_DHTMLRulesTableless->display()
line 117 of /login/signup.php: call to moodleform->display()

So I think this mean that guest user does't allowed to use filepicker

So how can fix this ?


In case of someone need the answer, I finally find a solution.

But first the reason why guest user can't use filepaicker/filemanager is that the draft files for a filearea are stored based on the userid and guest users shouldn't be storing data in the system (otherwise different people logged in as 'guest' could potentially access each other's draft files).

The solution is: using $mform->addElement('file' .... ) and use RegEx with $mform->addRule() for validation as the following:

$mform->addRule('document_1', 'Error (allowed extensions are .jpg, .png and .pdf)', 'filename', 'myregex');