1
<?php // $Id: formslib.php,v 1.129.2.19 2008/12/01 07:09:44 tjhunt Exp $
1
<?php // $Id: formslib.php,v 1.129.2.24 2010/05/13 01:40:36 moodler Exp $
3
3
* formslib.php - library of classes for creating forms in Moodle, based on PEAR QuickForms.
20
20
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
23
if (!defined('MOODLE_INTERNAL')) {
24
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
23
28
//setup.php icludes our hacked pear libs first
24
29
require_once 'HTML/QuickForm.php';
25
30
require_once 'HTML/QuickForm/DHTMLRulesTableless.php';
116
121
$this->definition();
118
123
$this->_form->addElement('hidden', 'sesskey', null); // automatic sesskey protection
124
$this->_form->setType('sesskey', PARAM_RAW);
119
125
$this->_form->setDefault('sesskey', sesskey());
120
126
$this->_form->addElement('hidden', '_qf__'.$this->_formname, null); // form submission marker
127
$this->_form->setType('_qf__'.$this->_formname, PARAM_RAW);
121
128
$this->_form->setDefault('_qf__'.$this->_formname, 1);
122
129
$this->_form->_setDefaultRuleMessages();
302
309
* Check that form data is valid.
310
* You should almost always use this, rather than {@see validate_defined_fields}
304
312
* @return bool true if form data valid
306
314
function is_validated() {
315
//finalize the form definition before any processing
316
if (!$this->_definition_finalized) {
317
$this->_definition_finalized = true;
318
$this->definition_after_data();
320
return $this->validate_defined_fields();
326
* You almost always want to call {@see is_validated} instead of this
327
* because it calls {@see definition_after_data} first, before validating the form,
328
* which is what you want in 99% of cases.
330
* This is provided as a separate function for those special cases where
331
* you want the form validated before definition_after_data is called
332
* for example, to selectively add new elements depending on a no_submit_button press,
333
* but only when the form is valid when the no_submit_button is pressed,
335
* @param boolean $validateonnosubmit optional, defaults to false. The default behaviour
336
* is NOT to validate the form when a no submit button has been pressed.
337
* pass true here to override this behaviour
339
* @return bool true if form data valid
341
function validate_defined_fields($validateonnosubmit=false) {
307
342
static $validated = null; // one validation is enough
308
343
$mform =& $this->_form;
310
//finalize the form definition before any processing
311
if (!$this->_definition_finalized) {
312
$this->_definition_finalized = true;
313
$this->definition_after_data();
316
if ($this->no_submit_button_pressed()){
345
if ($this->no_submit_button_pressed() && empty($validateonnosubmit)){
318
347
} elseif ($validated === null) {
319
348
$internal_val = $mform->validate();
550
579
$mform =& $this->_form;
551
580
$mform->registerNoSubmitButton($addfieldsname);
552
581
$mform->addElement('hidden', $repeathiddenname, $repeats);
582
$mform->setType($repeathiddenname, PARAM_INT);
553
583
//value not to be overridden by submitted value
554
584
$mform->setConstants(array($repeathiddenname=>$repeats));
555
for ($i=0; $i<$repeats; $i++) {
585
$namecloned = array();
586
for ($i = 0; $i < $repeats; $i++) {
556
587
foreach ($elementobjs as $elementobj){
557
588
$elementclone = fullclone($elementobj);
558
589
$name = $elementclone->getName();
590
$namecloned[] = $name;
560
592
$elementclone->setName($name."[$i]");
562
if (is_a($elementclone, 'HTML_QuickForm_header')){
563
$value=$elementclone->_text;
594
if (is_a($elementclone, 'HTML_QuickForm_header')) {
595
$value = $elementclone->_text;
564
596
$elementclone->setValue(str_replace('{no}', ($i+1), $value));
591
623
$mform->setHelpButton($realelementname, $params);
593
625
case 'disabledif' :
626
foreach ($namecloned as $num => $name){
627
if ($params[0] == $name){
628
$params[0] = $params[0]."[$i]";
594
632
$params = array_merge(array($realelementname), $params);
595
633
call_user_func_array(array(&$mform, 'disabledIf'), $params);
640
678
$mform->addElement('hidden', "checkbox_controller$groupid");
679
$mform->setType("checkbox_controller$groupid", PARAM_INT);
641
680
$mform->setConstants(array("checkbox_controller$groupid" => $new_select_value));
643
682
// Locate all checkboxes for this group and set their value, IF the optional param was given
794
833
$this->_pageparams = '';
796
835
//no 'name' atttribute for form in xhtml strict :
797
$attributes = array('action'=>$action, 'method'=>$method, 'id'=>'mform'.$formcounter) + $target;
836
$attributes = array('action'=>$action, 'method'=>$method,
837
'accept-charset'=>'utf-8', 'id'=>'mform'.$formcounter) + $target;
799
839
$this->updateAttributes($attributes);