2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Password type form element with unmask option
21 * Contains HTML class for a password type element with unmask option
24 * @copyright 2009 Petr Skoda
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 if (!defined('MOODLE_INTERNAL')) {
29 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
33 require_once($CFG->libdir
.'/form/password.php');
36 * Password type form element with unmask option
38 * HTML class for a password type element with unmask option
42 * @copyright 2009 Petr Skoda {@link http://skodak.org}
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 class MoodleQuickForm_passwordunmask
extends MoodleQuickForm_password
{
49 * @param string $elementName (optional) name of the password element
50 * @param string $elementLabel (optional) label for password element
51 * @param mixed $attributes (optional) Either a typical HTML attribute string
52 * or an associative array
54 public function __construct($elementName=null, $elementLabel=null, $attributes=null) {
55 // no standard mform in moodle should allow autocomplete of passwords
56 if (empty($attributes)) {
57 $attributes = array('autocomplete'=>'off');
58 } else if (is_array($attributes)) {
59 $attributes['autocomplete'] = 'off';
61 if (strpos($attributes, 'autocomplete') === false) {
62 $attributes .= ' autocomplete="off" ';
65 $this->_persistantFreeze
= true;
67 parent
::__construct($elementName, $elementLabel, $attributes);
68 $this->setType('passwordunmask');
72 * Old syntax of class constructor. Deprecated in PHP7.
74 * @deprecated since Moodle 3.1
76 public function MoodleQuickForm_passwordunmask($elementName=null, $elementLabel=null, $attributes=null) {
77 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
78 self
::__construct($elementName, $elementLabel, $attributes);
82 * Function to export the renderer data in a format that is suitable for a mustache template.
84 * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
85 * @return stdClass|array
87 public function export_for_template(renderer_base
$output) {
88 $context = parent
::export_for_template($output);
89 $context['valuechars'] = array_fill(0, strlen($context['value']), 'x');
95 * Check that there is no whitespace at the beginning and end of the password.
97 * It turned out that wrapping whitespace can easily be pasted by accident when copying the text from elsewhere.
98 * Such a mistake is very hard to debug as the whitespace is not displayed.
100 * @param array $value Submitted value.
101 * @return string|null Validation error message or null.
103 public function validateSubmitValue($value) {
104 if ($value !== null && $value !== trim($value)) {
105 return get_string('err_wrappingwhitespace', 'core_form');