Does anybody know if it is possible to create an addRule to the filemanager inside of a Moodle form? the problem i am having is, i don't want people to submit a form if no file exists. The addRule appears to work for all other elements apart from on the filemanager.
Currently when i print out the value of "attachments" it still shows the ID of the stored file, even if the user never actually uploaded a file:
$mform = $this->_form; // Don't forget the underscore!
$filemanageropts = $this->_customdata['filemanageropts'];
// FILE MANAGER
$mform->addElement('filemanager', 'attachments', 'Upload evidence', null, $filemanageropts);
$mform->addRule('filemanager', 'You must upload file(s) into the box below before you can save', 'required', null, 'client');
Obviously the above code isn't all of my form code but the relevant part for what i am trying to achieve. If somebody knows where i am going wrong or an alternative way of checking if no file has been supplied before submission then that would be great.
I may be wrong, but I'm sure I've seen this work. Have you tried ditching the 'client' part?
If that doesn't work, then you could always use the form's validation function to check the files on the server, before the form is processed.
Note the "attachments" value is just the id of the temporary storage area where the files are stored whilst you are editing the form, it doesn't tell you anything about the content of that temporary area.
Hi there,
Yeah i have tried removing the client to see if server validation works and i get the same. My code is very similar to the problem identified here:
http://stackoverflow.com/questions/31872233/validation-for-filepicker-is-not-working-in-moodle
I also tried adding on at the bottom of the form:
function validation($data, $files) {
return array();
}
also my addRule i have tried removing the 'client' and also the final null and neither seems to pick up validation:
$mform->addRule('filemanager', 'You must upload file(s) into the box below before you can save', 'required', null);
The only thing i can think of doing is checking once the form is submitted to check if the file exists in the file area but that seems a bit of a long way of doing it:
// Just to show they are all there - output a list of submitted files
$fs = get_file_storage();
/** @var stored_file[] $files */
$files = $fs->get_area_files($context->id, 'local_filemanager', 'attachment', $itemid);
echo '<p>List of files:</p>';
echo '<ul>';
foreach ($files as $file) {
$out = $file->get_filename(); //<--i could get the filename and check if it is legitimate or not
}
I don't even think the above would work as no file is actually written
Obviously the file does not actually save into the system due to non being submitted but just trying to think of a way to avoid people from submitting. If you can think of anything which may work then would be a great help
Take a look at how mod_resource does this - https://github.com/moodle/moodle/blob/master/mod/resource/mod_form.php#L198
An empty validation function will not do anything useful - you need to look through the 'draft' file area and count how many files are in it.
Hello Mark,
I used file validation rule with filepicker and it works for me.
Pass pageurl as a parameter, where you have instantiated form object. For ex - if your class is 'class testing_form extends moodleform' use $mform = new testing_form($pageurl) intead of $mform = new testing_form(); and see if this work for you. pageurl is the location where form data submitted.
I have a working demo of filepicker if you required let me know.
Pinky - note he is using a 'filemanager' element, not a 'filepicker' element (there are very few cases in Moodle where you actually want to use filepicker elements). Also, you should almost never need to pass the $pageurl to a moodle form - if it is posting back to the current page, then this is done automatically, if it is not posting back to the current page, then much of the workflow for the form will be broken anyway.
Davo - I know he is using 'filemanage' and this is also working with filemanger (now I checked see screenshot). Since I used it with filepicker so gave reference of it. You are right it is not mandatory to pass action Url every time in Moodle form and it works fine in every cases except file validation. Without this parameter it doesn't seems to check for empty file and this should not be happen. May be it is a Moodle bug, not sure. Maybe, you could enlighten me with a better way.
Hi Pinky,
Do you have any source there i can see off to see how you managed to achieve that? I have tried the following in my validation after looking at the link in Davo's previous post to a github and it seems to just allow the submission anyways:
function validation($data, $files) {
global $USER;
$errors = parent::validation($data, $files);
$usercontext = context_user::instance($USER->id);
$fs = get_file_storage();
if (!$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data['files'], 'sortorder, id', false)) {
$errors['files'] = get_string('required');
return $errors;
}
return $errors;
}
Ya sure Mark,
Here I am attaching test module which I have created for testing. It is just for testing, so not properly managed and you may found some error. To check file validation, create an activity and then click to view it , here you will find a form with filemanager where you can test required file rule.
I have tried your code and it doesn't seem to work with the file validation. I tried to copy the validation code into a form i am creating and it still doesn't run the validation.
public function validation($data, $files) {
$errors = parent::validation($data, $files);
return $errors;
}
I added that within the form but outside of the form definition. I am using Moodle 2.6
Out of curiosity - did you actually put any validation code inside the validation function, or was it literally as posted here.
The version posted here will do nothing (other than pass on any validation done by the parent function), if you have some specific server-side validation you have to put the code for it in the function and then add it to the list of errors (indexed by the name of the field the error should appear beside).
Mark,
Did you run my demo module without any modifications? Did it work?
What validation you want to implement?
You said "i don't want people to submit a form if no file exists". That means you want to put mandatory filed validation, so that user can not submit form without file upload. This validation already done by parent function as shown in your code you just need to call it or if you have some other validation then you have to put the code for it in the function.
You added the validation function but problem is not in validation code. It will work if you pass url to Moodle form. In my code I used $mform = new add_form($submiturl); in view.php to make validation work, Have you used it in your code?
This will work for Moodle 2.6 also.
Can you show the code snippet, where you are using your form?