Skip to content
Browse files

Merge branch 'wip-MDL-19907-MOODLE_21_STABLE' of git://github.com/mar…

…inaglancy/moodle into MOODLE_21_STABLE
  • Loading branch information...
2 parents 5be31c5 + f9694db commit 9a1f42da6db9c6bbce07996d9814c3ad5aa2e054 @samhemelryk samhemelryk committed Aug 2, 2011
View
1 admin/settings/security.php
@@ -71,6 +71,7 @@
$temp->add(new admin_setting_configcheckbox('groupenrolmentkeypolicy', get_string('groupenrolmentkeypolicy', 'admin'), get_string('groupenrolmentkeypolicy_desc', 'admin'), 1));
$temp->add(new admin_setting_configcheckbox('disableuserimages', get_string('disableuserimages', 'admin'), get_string('configdisableuserimages', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('emailchangeconfirmation', get_string('emailchangeconfirmation', 'admin'), get_string('configemailchangeconfirmation', 'admin'), 1));
+ $temp->add(new admin_setting_configcheckbox('strictformsrequired', get_string('strictformsrequired', 'admin'), get_string('configstrictformsrequired', 'admin'), 0));
$ADMIN->add('security', $temp);
View
2 lang/en/admin.php
@@ -333,6 +333,7 @@
$string['configstatsruntimedays'] = 'Specify the maximum number of days processed in each stats execution. When stats are up-to-date, only one day will be processed, so adjust this value depending of your server load, reducing it if shorter cron executions are needed.';
$string['configstatsruntimestart'] = 'What time should the cronjob that does the stats processing <b>start</b>? Please specify different times if there are multiple Moodles on one physical server.';
$string['configstatsuserthreshold'] = 'This setting specifies the minimum number of enrolled users for a course to be included in statistics calculations.';
+$string['configstrictformsrequired'] = 'If enabled, users are prevented from entering a space or line break only in required fields in forms.';
$string['configstripalltitletags'] = 'Uncheck this setting to allow HTML tags in activity and resource names.';
$string['configsupportemail'] = 'This email address will be published to users of this site as the one to email when they need general help (for example, when new users create their own accounts). If this email is left blank then no such helpful email address is supplied.';
$string['configsupportname'] = 'This is the name of a person or other entity offering general help via the support email or web address.';
@@ -961,6 +962,7 @@
$string['stickyblocksduplicatenotice'] = 'If any block you add here is already present in a particular page, it will result in a duplicate.<br />Only the pinned block will be non-editable, the duplicate will still be editable.';
$string['stickyblocksmymoodle'] = 'My Moodle';
$string['stickyblockspagetype'] = 'Page type to configure';
+$string['strictformsrequired'] = 'Strict validation of required fields';
$string['stripalltitletags'] = 'Remove HTML tags from all activity names';
$string['supportcontact'] = 'Support contact';
$string['supportemail'] = 'Support email';
View
54 lib/formslib.php
@@ -43,6 +43,7 @@
require_once 'HTML/QuickForm.php';
require_once 'HTML/QuickForm/DHTMLRulesTableless.php';
require_once 'HTML/QuickForm/Renderer/Tableless.php';
+require_once 'HTML/QuickForm/Rule.php';
require_once $CFG->libdir.'/filelib.php';
@@ -2343,6 +2344,57 @@ function getStopFieldsetElements(){
}
/**
+ * Required elements validation
+ * This class overrides QuickForm validation since it allowed space or empty tag as a value
+ */
+class MoodleQuickForm_Rule_Required extends HTML_QuickForm_Rule {
+ /**
+ * Checks if an element is not empty.
+ * This is a server-side validation, it works for both text fields and editor fields
+ *
+ * @param string $value Value to check
+ * @param mixed $options Not used yet
+ * @return boolean true if value is not empty
+ */
+ function validate($value, $options = null) {
+ global $CFG;
+ if (is_array($value) && array_key_exists('text', $value)) {
+ $value = $value['text'];
+ }
+ $stripvalues = array(
+ '#</?(?!img|canvas|hr).*?>#im', // all tags except img, canvas and hr
+ '#(\xc2|\xa0|\s|&nbsp;)#', //any whitespaces actually
+ );
+ if (!empty($CFG->strictformsrequired)) {
+ $value = preg_replace($stripvalues, '', (string)$value);
+ }
+ if ((string)$value == '') {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * This function returns Javascript code used to build client-side validation.
+ * It checks if an element is not empty.
+ * Note, that QuickForm does not know how to work with editor text field and builds not correct
+ * JS code for validation. If client check is enabled for editor field it will not be validated
+ * on client side no matter what this function returns.
+ *
+ * @param mixed $options Not used yet
+ * @return array
+ */
+ function getValidationScript($options = null) {
+ global $CFG;
+ if (!empty($CFG->strictformsrequired)) {
+ return array('', "{jsVar}.replace(/^\s+$/g, '') == ''");
+ } else {
+ return array('', "{jsVar} == ''");
+ }
+ }
+}
+
+/**
* @global object $GLOBALS['_HTML_QuickForm_default_renderer']
* @name $_HTML_QuickForm_default_renderer
*/
@@ -2385,3 +2437,5 @@ function getStopFieldsetElements(){
MoodleQuickForm::registerElementType('textarea', "$CFG->libdir/form/textarea.php", 'MoodleQuickForm_textarea');
MoodleQuickForm::registerElementType('url', "$CFG->libdir/form/url.php", 'MoodleQuickForm_url');
MoodleQuickForm::registerElementType('warning', "$CFG->libdir/form/warning.php", 'MoodleQuickForm_warning');
+
+MoodleQuickForm::registerRule('required', null, 'MoodleQuickForm_Rule_Required', "$CFG->libdir/formslib.php");
View
7 lib/pear/HTML/QuickForm.php
@@ -1502,13 +1502,6 @@ function validate()
$values[] = $this->getSubmitValue($elName);
}
$result = $registry->validate($rule['type'], $values, $rule['format'], true);
- } else if ($rule['type'] === 'required' and $this->getElement($target)->_type === 'editor') {
- //horrible horrible hack
- if (!isset($submitValue['text']) or $submitValue['text'] === '') {
- $result = false;
- } else {
- $result = true;
- }
} elseif (is_array($submitValue) && !isset($rule['howmany'])) {
$result = $registry->validate($rule['type'], $submitValue, $rule['format'], true);
} else {
View
2 lib/pear/README_MOODLE.txt
@@ -22,8 +22,6 @@ and documented for Moodle at:
4/ MDL-20876 - replaced deprecated split() with explode() or str_split() where appropriate
-5/ hardcoded editor element required rule validation
-
Such modifications should be carefully each time the Excel PEAR package is updated
to a new release within Moodle.
View
119 lib/simpletest/testformslib.php
@@ -0,0 +1,119 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+
+/**
+ * Unit tests for /lib/formslib.php.
+ *
+ * @package file
+ * @copyright 2011 Sam Hemelryk
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+if (!defined('MOODLE_INTERNAL')) {
+ die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
+}
+require_once($CFG->libdir . '/formslib.php');
+
+class formslib_test extends UnitTestCase {
+
+ public function test_require_rule() {
+ global $CFG;
+
+ $strictformsrequired = false;
+ if (!empty($CFG->strictformsrequired)) {
+ $strictformsrequired = $CFG->strictformsrequired;
+ }
+
+ $rule = new MoodleQuickForm_Rule_Required();
+
+ // First run the tests with strictformsrequired off
+ $CFG->strictformsrequired = false;
+ // Passes
+ $this->assertTrue($rule->validate('Something'));
+ $this->assertTrue($rule->validate("Something\nmore"));
+ $this->assertTrue($rule->validate("\nmore"));
+ $this->assertTrue($rule->validate(" more "));
+ $this->assertTrue($rule->validate("0"));
+ $this->assertTrue($rule->validate(0));
+ $this->assertTrue($rule->validate(true));
+ $this->assertTrue($rule->validate(' '));
+ $this->assertTrue($rule->validate(' '));
+ $this->assertTrue($rule->validate("\t"));
+ $this->assertTrue($rule->validate("\n"));
+ $this->assertTrue($rule->validate("\r"));
+ $this->assertTrue($rule->validate("\r\n"));
+ $this->assertTrue($rule->validate(" \t \n \r "));
+ $this->assertTrue($rule->validate('<p></p>'));
+ $this->assertTrue($rule->validate('<p> </p>'));
+ $this->assertTrue($rule->validate('<p>x</p>'));
+ $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />'));
+ $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>'));
+ $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>'));
+ $this->assertTrue($rule->validate('<hr />'));
+ $this->assertTrue($rule->validate('<hr/>'));
+ $this->assertTrue($rule->validate('<hr>'));
+ $this->assertTrue($rule->validate('<hr></hr>'));
+ $this->assertTrue($rule->validate('<br />'));
+ $this->assertTrue($rule->validate('<br/>'));
+ $this->assertTrue($rule->validate('<br>'));
+ $this->assertTrue($rule->validate('&nbsp;'));
+ // Fails
+ $this->assertFalse($rule->validate(''));
+ $this->assertFalse($rule->validate(false));
+ $this->assertFalse($rule->validate(null));
+
+ // Now run the same tests with it on to make sure things work as expected
+ $CFG->strictformsrequired = true;
+ // Passes
+ $this->assertTrue($rule->validate('Something'));
+ $this->assertTrue($rule->validate("Something\nmore"));
+ $this->assertTrue($rule->validate("\nmore"));
+ $this->assertTrue($rule->validate(" more "));
+ $this->assertTrue($rule->validate("0"));
+ $this->assertTrue($rule->validate(0));
+ $this->assertTrue($rule->validate(true));
+ $this->assertTrue($rule->validate('<p>x</p>'));
+ $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />'));
+ $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>'));
+ $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>'));
+ $this->assertTrue($rule->validate('<hr />'));
+ $this->assertTrue($rule->validate('<hr/>'));
+ $this->assertTrue($rule->validate('<hr>'));
+ $this->assertTrue($rule->validate('<hr></hr>'));
+ // Fails
+ $this->assertFalse($rule->validate(' '));
+ $this->assertFalse($rule->validate(' '));
+ $this->assertFalse($rule->validate("\t"));
+ $this->assertFalse($rule->validate("\n"));
+ $this->assertFalse($rule->validate("\r"));
+ $this->assertFalse($rule->validate("\r\n"));
+ $this->assertFalse($rule->validate(" \t \n \r "));
+ $this->assertFalse($rule->validate('<p></p>'));
+ $this->assertFalse($rule->validate('<p> </p>'));
+ $this->assertFalse($rule->validate('<br />'));
+ $this->assertFalse($rule->validate('<br/>'));
+ $this->assertFalse($rule->validate('<br>'));
+ $this->assertFalse($rule->validate('&nbsp;'));
+ $this->assertFalse($rule->validate(''));
+ $this->assertFalse($rule->validate(false));
+ $this->assertFalse($rule->validate(null));
+
+ $CFG->strictformsrequired = $strictformsrequired;
+ }
+
+}

0 comments on commit 9a1f42d

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