Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-38885 form: Recursively clean parameters based on right type

  • Loading branch information...
commit eb5852672e5e45fd95b28aab58bad080b15a7b6d 1 parent a811e8a
Frédéric Massart authored May 09, 2013

Showing 1 changed file with 81 additions and 13 deletions. Show diff stats Hide diff stats

  1. 94  lib/formslib.php
94  lib/formslib.php
@@ -1492,8 +1492,84 @@ function setTypes($paramtypes) {
1492 1492
     }
1493 1493
 
1494 1494
     /**
1495  
-     * @param array $submission
1496  
-     * @param array $files
  1495
+     * Return the type(s) to use to clean an element.
  1496
+     *
  1497
+     * In the case where the element has an array as a value, we will try to obtain a
  1498
+     * type defined for that specific key, and recursively until done.
  1499
+     *
  1500
+     * This method does not work reverse, you cannot pass a nested element and hoping to
  1501
+     * fallback on the clean type of a parent. This method intends to be used with the
  1502
+     * main element, which will generate child types if needed, not the other way around.
  1503
+     *
  1504
+     * Example scenario:
  1505
+     *
  1506
+     * You have defined a new repeated element containing a text field called 'foo'.
  1507
+     * By default there will always be 2 occurence of 'foo' in the form. Even though
  1508
+     * you've set the type on 'foo' to be PARAM_INT, for some obscure reason, you want
  1509
+     * the first value of 'foo', to be PARAM_FLOAT, which you set using setType:
  1510
+     * $mform->setType('foo[0]', PARAM_FLOAT).
  1511
+     *
  1512
+     * Now if you call this method passing 'foo', along with the submitted values of 'foo':
  1513
+     * array(0 => '1.23', 1 => '10'), you will get an array telling you that the key 0 is a
  1514
+     * FLOAT and 1 is an INT. If you had passed 'foo[1]', along with its value '10', you would
  1515
+     * get the default value returned.
  1516
+     *
  1517
+     * @param string $elementname name of the element.
  1518
+     * @param mixed $value value that should be cleaned.
  1519
+     * @return string|array constant value or array of constant values (PARAM_...)
  1520
+     */
  1521
+    public function getCleanType($elementname, $value, $default = PARAM_RAW) {
  1522
+        $type = $default;
  1523
+        if (array_key_exists($elementname, $this->_types)) {
  1524
+            $type = $this->_types[$elementname];
  1525
+        }
  1526
+        if (is_array($value)) {
  1527
+            $default = $type;
  1528
+            $type = array();
  1529
+            foreach ($value as $subkey => $subvalue) {
  1530
+                $typekey = "$elementname" . "[$subkey]";
  1531
+                if (array_key_exists($typekey, $this->_types)) {
  1532
+                    $subtype = $this->_types[$typekey];
  1533
+                } else {
  1534
+                    $subtype = $default;
  1535
+                }
  1536
+                if (is_array($subvalue)) {
  1537
+                    $type[$subkey] = $this->getCleanType($typekey, $subvalue, $subtype);
  1538
+                } else {
  1539
+                    $type[$subkey] = $subtype;
  1540
+                }
  1541
+            }
  1542
+        }
  1543
+        return $type;
  1544
+    }
  1545
+
  1546
+    /**
  1547
+     * Return the cleaned value using the passed type(s).
  1548
+     *
  1549
+     * @param mixed $value value that has to be cleaned.
  1550
+     * @param int|array $type constant value to use to clean (PARAM_...), typically returned by {@link self::getCleanType()}.
  1551
+     * @return mixed cleaned up value.
  1552
+     */
  1553
+    public function getCleanedValue($value, $type) {
  1554
+        if (is_array($type) && is_array($value)) {
  1555
+            foreach ($type as $key => $param) {
  1556
+                $value[$key] = $this->getCleanedValue($value[$key], $param);
  1557
+            }
  1558
+        } else if (!is_array($type) && !is_array($value)) {
  1559
+            $value = clean_param($value, $type);
  1560
+        } else if (!is_array($type) && is_array($value)) {
  1561
+            $value = clean_param_array($value, $type, true);
  1562
+        } else {
  1563
+            throw new coding_exception('Unexpected type or value received in MoodleQuickForm::cleanValue()');
  1564
+        }
  1565
+        return $value;
  1566
+    }
  1567
+
  1568
+    /**
  1569
+     * Updates submitted values
  1570
+     *
  1571
+     * @param array $submission submitted values
  1572
+     * @param array $files list of files
1497 1573
      */
1498 1574
     function updateSubmission($submission, $files) {
1499 1575
         $this->_flagSubmitted = false;
@@ -1501,17 +1577,9 @@ function updateSubmission($submission, $files) {
1501 1577
         if (empty($submission)) {
1502 1578
             $this->_submitValues = array();
1503 1579
         } else {
1504  
-            foreach ($submission as $key=>$s) {
1505  
-                if (array_key_exists($key, $this->_types)) {
1506  
-                    $type = $this->_types[$key];
1507  
-                } else {
1508  
-                    $type = PARAM_RAW;
1509  
-                }
1510  
-                if (is_array($s)) {
1511  
-                    $submission[$key] = clean_param_array($s, $type, true);
1512  
-                } else {
1513  
-                    $submission[$key] = clean_param($s, $type);
1514  
-                }
  1580
+            foreach ($submission as $key => $s) {
  1581
+                $type = $this->getCleanType($key, $s);
  1582
+                $submission[$key] = $this->getCleanedValue($s, $type);
1515 1583
             }
1516 1584
             $this->_submitValues = $submission;
1517 1585
             $this->_flagSubmitted = true;

0 notes on commit eb58526

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