Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-25937 Froms Library: Added server side validation for filepicker …

…and filemanager
  • Loading branch information...
commit f03a17bb09bed83a29cda253c2049c937852dd38 1 parent 9cfaebb
Rajesh Taneja authored
2  grade/import/xml/grade_import_form.php
@@ -32,7 +32,6 @@ function definition () {
32 32 $mform->setType('id', PARAM_INT);
33 33
34 34 $mform->addElement('header', 'general', get_string('importfile', 'grades'));
35   - $mform->disabledIf('url', 'userfile', 'noteq', '');
36 35
37 36 $mform->addElement('advcheckbox', 'feedback', get_string('importfeedback', 'grades'));
38 37 $mform->setDefault('feedback', 0);
@@ -42,6 +41,7 @@ function definition () {
42 41 $mform->disabledIf('userfile', 'url', 'noteq', '');
43 42
44 43 $mform->addElement('text', 'url', get_string('fileurl', 'gradeimport_xml'), 'size="80"');
  44 + $mform->disabledIf('url', 'userfile', 'noteq', '');
45 45
46 46 if (!empty($CFG->gradepublishing)) {
47 47 $mform->addElement('header', 'publishing', get_string('publishing', 'grades'));
1  lib/form/filemanager.php
@@ -45,6 +45,7 @@ function MoodleQuickForm_filemanager($elementName=null, $elementLabel=null, $att
45 45 if (!empty($options['maxbytes'])) {
46 46 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
47 47 }
  48 + $this->_type = 'filemanager';
48 49 parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
49 50 }
50 51
1  lib/form/filepicker.php
@@ -29,6 +29,7 @@ function MoodleQuickForm_filepicker($elementName=null, $elementLabel=null, $attr
29 29 if (!empty($options['maxbytes'])) {
30 30 $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']);
31 31 }
  32 + $this->_type = 'filepicker';
32 33 parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
33 34 }
34 35
47 lib/formslib.php
@@ -340,6 +340,43 @@ function _validate_files(&$files) {
340 340 }
341 341
342 342 /**
  343 + * Internal method. Validates filepicker and filemanager files if they are
  344 + * set as required fields. Also, sets the error message if encountered one.
  345 + *
  346 + * @return bool/array with errors
  347 + */
  348 + function _validate_draft_files() {
  349 + global $USER;
  350 + $mform =& $this->_form;
  351 +
  352 + $errors = array();
  353 + //Go through all the required elements and make sure you hit filepicker or
  354 + //filemanager element.
  355 + foreach ($mform->_rules as $elementname => $rules) {
  356 + $elementtype = $mform->getElementType($elementname);
  357 + //If element is of type filepicker then do validation
  358 + if (($elementtype == 'filepicker') || ($elementtype == 'filemanager')){
  359 + //Check if rule defined is required rule
  360 + foreach ($rules as $rule) {
  361 + if ($rule['type'] == 'required') {
  362 + $draftid = (int)$mform->getSubmitValue($elementname);
  363 + $fs = get_file_storage();
  364 + $context = get_context_instance(CONTEXT_USER, $USER->id);
  365 + if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) {
  366 + $errors[$elementname] = $rule['message'];
  367 + }
  368 + }
  369 + }
  370 + }
  371 + }
  372 + if (empty($errors)) {
  373 + return true;
  374 + } else {
  375 + return $errors;
  376 + }
  377 + }
  378 +
  379 + /**
343 380 * Load in existing data as form defaults. Usually new entry defaults are stored directly in
344 381 * form definition (new entry form); this function is used to load in data where values
345 382 * already exist and data is being edited (edit entry form).
@@ -439,6 +476,16 @@ function validate_defined_fields($validateonnosubmit=false) {
439 476
440 477 $files = array();
441 478 $file_val = $this->_validate_files($files);
  479 + //check draft files for validation and flag them if required files
  480 + //are not in draft area.
  481 + $draftfilevalue = $this->_validate_draft_files();
  482 +
  483 + if ($file_val !== true && $draftfilevalue !== true) {
  484 + $file_val = array_merge($file_val, $draftfilevalue);
  485 + } else if ($draftfilevalue !== true) {
  486 + $file_val = $draftfilevalue;
  487 + } //default is file_val, so no need to assign.
  488 +
442 489 if ($file_val !== true) {
443 490 if (!empty($file_val)) {
444 491 foreach ($file_val as $element=>$msg) {

0 comments on commit f03a17b

Please sign in to comment.
Something went wrong with that request. Please try again.