Source for file FormValidator.class.php
Documentation is available at FormValidator.class.php
==============================================================================
Dokeos - elearning and course management software
Copyright (c) 2004-2005 Dokeos S.A.
Copyright (c) Bart Mollet, Hogeschool Gent
For a full list of contributors, see "credits.txt".
The full license can be read in "license.txt".
This program 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 2
of the License, or (at your option) any later version.
See the GNU General Public License for more details.
Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
==============================================================================
require_once ('HTML/QuickForm.php');
require_once ('HTML/QuickForm/advmultiselect.php');
define('STUDENT_HTML_FULLPAGE',4);
define('TEACHER_HTML_FULLPAGE',5);
* Objects of this class can be used to create/manipulate/validate user input.
* @param string $form_name Name of the form
* @param string $method Method ('post' (default) or 'get')
* @param string $action Action (default is $PHP_SELF)
* @param string $target Form's target defaults to '_self'
* @param mixed $attributes (optional)Extra attributes for <form> tag
* @param bool $trackSubmit (optional)Whether to track if the form was
* submitted by adding a special hidden field (default = true)
function FormValidator($form_name, $method = 'post', $action = '', $target = '', $attributes = null, $trackSubmit = true)
$this->HTML_QuickForm($form_name, $method,$action, $target, $attributes, $trackSubmit);
// Load some custom elements and rules
$this->registerElementType('html_editor', $dir. 'Element/html_editor.php', 'HTML_QuickForm_html_editor');
$this->registerElementType('datepicker', $dir. 'Element/datepicker.php', 'HTML_QuickForm_datepicker');
$this->registerElementType('datepickerdate', $dir. 'Element/datepickerdate.php', 'HTML_QuickForm_datepickerdate');
$this->registerElementType('receivers', $dir. 'Element/receivers.php', 'HTML_QuickForm_receivers');
$this->registerElementType('select_language', $dir. 'Element/select_language.php', 'HTML_QuickForm_Select_Language');
$this->registerElementType('select_theme', $dir. 'Element/select_theme.php', 'HTML_QuickForm_Select_Theme');
$this->registerRule('date', null, 'HTML_QuickForm_Rule_Date', $dir. 'Rule/Date.php');
$this->registerRule('date_compare', null, 'HTML_QuickForm_Rule_DateCompare', $dir. 'Rule/DateCompare.php');
$this->registerRule('html',null,'HTML_QuickForm_Rule_HTML',$dir. 'Rule/HTML.php');
$this->registerRule('username_available',null,'HTML_QuickForm_Rule_UsernameAvailable',$dir. 'Rule/UsernameAvailable.php');
$this->registerRule('username',null,'HTML_QuickForm_Rule_Username',$dir. 'Rule/Username.php');
$this->registerRule('filetype',null,'HTML_QuickForm_Rule_Filetype',$dir. 'Rule/Filetype.php');
$this->registerRule('multiple_required','required','HTML_QuickForm_Rule_MultipleRequired',$dir. 'Rule/MultipleRequired.php');
// Modify the default templates
$renderer->setFormTemplate($form_template);
$element_template = <<<EOT
<!-- BEGIN required --><span class="form_required">*</span> <!-- END required -->{label}
<!-- BEGIN error --><span class="form_error">{error}</span><br /><!-- END error --> {element}
$renderer->setElementTemplate($element_template);
$header_template = <<<EOT
<div class="form_header">{header}</div>
$renderer->setHeaderTemplate($header_template);
$required_note_template = <<<EOT
<div class="label"></div>
<div class="formw">{requiredNote}</div>
$renderer->setRequiredNoteTemplate($required_note_template);
* Add a textfield to the form.
* A trim-filter is attached to the field.
* @param string $label The label for the form-element
* @param string $name The element name
* @param boolean $required Is the form-element required (default=true)
* @param array $attributes Optional list of attributes for the form-element
function add_textfield( $name, $label,$required = true, $attributes = array())
$this->addElement('text',$name,$label,$attributes);
* Add a HTML-editor to the form to fill in a title.
* A trim-filter is attached to the field.
* A HTML-filter is attached to the field (cleans HTML)
* A rule is attached to check for unwanted HTML
* @param string $label The label for the form-element
* @param string $name The element name
* @param boolean $required Is the form-element required (default=true)
function add_html_editor($name, $label, $required = true, $full_page = false)
$this->addElement('html_editor',$name,$label,'rows="15" cols="80"');
if(!empty($_SESSION['status']))
//First *filter* the HTML (markup, indenting, ...)
//$this->applyFilter($name,'html_filter_teacher_fullpage');
//First *filter* the HTML (markup, indenting, ...)
//$this->applyFilter($name,'html_filter_teacher');
//Add rule to check not-allowed HTML
//$this->addRule($name,get_lang('SomeHTMLNotAllowed'),'html',$html_type);
* Add a datepicker element to the form
* A rule is added to check if the date is a valid one
* @param string $label The label for the form-element
* @param string $name The element name
* Add a datepickerdate element to the form
* A rule is added to check if the date is a valid one
* @param string $label The label for the form-element
* @param string $name The element name
* Add a timewindow element to the form.
* 2 datepicker elements are added and a rule to check if the first date is
* @param string $label The label for the form-element
* @param string $name The element name
$this->addRule(array ($name_1, $name_2), get_lang('StartDateShouldBeBeforeEndDate'), 'date_compare', 'lte');
* Add a button to the form to add resources.
* Adds a progress bar to the form.
* Once the user submits the form, a progress bar (animated gif) is
* displayed. The progress bar will disappear once the page has been
* @param int $delay The number of seconds between the moment the user
* submits the form and the start of the progress bar.
$this->addElement('html','<script type="text/javascript">var myUpload = new upload('. (abs(intval($delay))* 1000). ');</script>');
* Use the new functions (php 5.2) allowing to display a real upload progress.
* @param upload_id the value of the field UPLOAD_IDENTIFIER
* @param elementAfter the first element of the form (to place at first UPLOAD_IDENTIFIER
* @param delay the frequency of the xajax call
$xajax_upload -> registerFunction ('updateProgress');
// IMPORTANT : must be the first element of the form
$el = $this->insertElementBefore(FormValidator::createElement('html','<input type="hidden" name="UPLOAD_IDENTIFIER" value="'. $upload_id. '" />'), $elementAfter);
// add the div where the progress bar will be displayed
<div id="dynamic_div_container" style="display:none">
<div id="dynamic_div_label">'. get_lang('UploadFile'). '</div>
<div id="dynamic_div_frame" style="width:214px; height:12px; border:1px solid grey; background-image:url('. api_get_path(REL_PATH). 'main/img/real_upload_frame.gif);">
<div id="dynamic_div_filled" style="width:0%;height:100%;background-image:url('. api_get_path(REL_PATH). 'main/img/real_upload_step.gif);background-repeat:repeat-x;background-position:center;"></div>
<div id="dynamic_div_waiter_container" style="display:none">
<div id="dynamic_div_waiter_label">
<div id="dynamic_div_waiter_frame">
$this->addElement('html','<script type="text/javascript">var myUpload = new upload('. (abs(intval($delay))* 1000). ');</script>');
$this->updateAttributes("onsubmit=\"myUpload.startRealUpload('dynamic_div','". $upload_id. "','". $this->getAttribute('id'). "',". $waitAfterUpload. ")\"");
* This function avoid to change directly QuickForm class.
* When we use it, the element is threated as 'required' to be dealt during validation
* @param array $element the array of elements
* @param string $message the message displayed
$this -> addRule ($elements , $message , 'multiple_required');
* If an element in the form didn't validate, an error message is showed
* asking the user to complete the form.
* Returns the HTML code of the form.
* If an element in the form didn't validate, an error message is showed
* asking the user to complete the form.
* @return string $return_value HTML code of the form
* @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
* @version Dokeos 1.8, august 2006
foreach($this->_elements as $index => $element)
$return_value = parent::toHtml();
// Add the div which will hold the progress bar
$return_value .= '<div id="dynamic_div" style="display:block;margin-left:40%;margin-top:10px;height:50px;"></div>';
* @param string HTML to clean
* @return string The cleaned HTML
require_once(dirname(__FILE__ ). '/Rule/HTML.php');
$cleaned_html = kses($html,$allowed_tags);
|