diff --git a/main/inc/lib/formvalidator/FormValidator.class.php b/main/inc/lib/formvalidator/FormValidator.class.php index 1f16c24ed8..569b4a480e 100644 --- a/main/inc/lib/formvalidator/FormValidator.class.php +++ b/main/inc/lib/formvalidator/FormValidator.class.php @@ -6,89 +6,6 @@ */ class FormValidator extends HTML_QuickForm { - - /** - * Create a form validator based on an array of form data: - * - * array( - * 'name' => 'zombie_report_parameters', //optional - * 'method' => 'GET', //optional - * 'items' => array( - * array( - * 'name' => 'ceiling', - * 'label' => 'Ceiling', //optional - * 'type' => 'date', - * 'default' => date() //optional - * ), - * array( - * 'name' => 'active_only', - * 'label' => 'ActiveOnly', - * 'type' => 'checkbox', - * 'default' => true - * ), - * array( - * 'name' => 'submit_button', - * 'type' => 'style_submit_button', - * 'value' => get_lang('Search'), - * 'attributes' => array('class' => 'search') - * ) - * ) - * ); - * - * @param array form_data - * @return FormValidator - */ - public static function create($form_data) - { - if (empty($form_data)) { - return null; - } - $form_name = isset($form_data['name']) ? $form_data['name'] : 'form'; - $form_method = isset($form_data['method']) ? $form_data['method'] : 'POST'; - $form_action = isset($form_data['action']) ? $form_data['action'] : ''; - $form_target = isset($form_data['target']) ? $form_data['target'] : ''; - $form_attributes = isset($form_data['attributes']) ? $form_data['attributes'] : null; - $form_track_submit = isset($form_data['track_submit']) ? $form_data['track_submit'] : true; - - $result = new FormValidator($form_name, $form_method, $form_action, $form_target, $form_attributes, $form_track_submit); - - $defaults = array(); - foreach ($form_data['items'] as $item) { - $name = $item['name']; - $type = isset($item['type']) ? $item['type'] : 'text'; - $label = isset($item['label']) ? $item['label'] : ''; - if ($type == 'wysiwyg') { - $element = $result->add_html_editor($name, $label); - } else { - $element = $result->addElement($type, $name, $label); - } - if (isset($item['attributes'])) { - $attributes = $item['attributes']; - $element->setAttributes($attributes); - } - if (isset($item['value'])) { - $value = $item['value']; - $element->setValue($value); - } - if (isset($item['default'])) { - $defaults[$name] = $item['default']; - } - if (isset($item['rules'])) { - $rules = $item['rules']; - foreach ($rules as $rule) { - $message = $rule['message']; - $type = $rule['type']; - $format = isset($rule['format']) ? $rule['format'] : null; - $validation = isset($rule['validation']) ? $rule['validation'] : 'server'; - $force = isset($rule['force']) ? $rule['force'] : false; - $result->addRule($name, $message, $type, $format, $validation, $reset, $force); - } - } - } - $result->setDefaults($defaults); - return $result; - } - public $with_progress_bar = false; /** @@ -102,7 +19,7 @@ class FormValidator extends HTML_QuickForm * submitted by adding a special hidden field (default = true) */ public function __construct( - $form_name, + $form_name = null, $method = 'post', $action = '', $target = '', @@ -113,17 +30,21 @@ class FormValidator extends HTML_QuickForm if (is_array($attributes) && !isset($attributes['class']) || empty($attributes)) { $attributes['class'] = 'form-horizontal'; } - + // Fixing form search if (is_array($attributes) && isset($attributes['class'])) { if ($attributes['class'] == 'form-search') { $attributes['class'] = 'form-inline'; } } + // Allow form with no names + if (empty($form_name)) { + $form_name = uniqid(); + } parent::__construct($form_name, $method, $action, $target, $attributes, $track_submit); // Load some custom elements and rules - $dir = api_get_path(LIBRARY_PATH) . 'formvalidator/'; + $dir = api_get_path(LIBRARY_PATH).'formvalidator/'; $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'); @@ -168,11 +89,10 @@ class FormValidator extends HTML_QuickForm } else { $element_template = $this->getDefaultElementTemplate(); } - //$renderer->setGroupElementTemplate($this->getGroupTemplate()); $renderer->setElementTemplate($element_template); - //Display a gray div in the buttons + // Display a gray div in the buttons $button_element_template_simple = '
');
- $group[] = $this->createElement('submit', 'add_resource', get_lang('Attachment'), 'class="link_alike"');
- $this->addGroup($group);
- }
-
/**
* Adds a progress bar to the form.
*
@@ -484,10 +381,8 @@ EOT;
}
$xajax_upload = new xajax(api_get_path(WEB_LIBRARY_PATH) . 'upload.xajax.php');
-
$xajax_upload->registerFunction('updateProgress');
-
// IMPORTANT : must be the first element of the form
$el = $this->insertElementBefore(FormValidator::createElement('html', ''), $element_after);
@@ -495,12 +390,12 @@ EOT;
// Add div-element where the progress bar is to be displayed
$this->addElement('html', '
- ');
+ ');
if ($wait_after_upload) {
$this->addElement('html', '
@@ -533,7 +428,7 @@ EOT;
/**
* This function has been created for avoiding changes directly within 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 array $elements The array of elements
* @param string $message The message displayed
*/
public function add_multiple_required_rule($elements, $message)
@@ -604,40 +499,86 @@ EOT;
';
}
-}
-
-// @todo remove this!
-
-/**
- * Cleans HTML text
- * @param string $html HTML to clean
- * @param int $mode (optional)
- * @return string The cleaned HTML
- */
-function html_filter($html, $mode = NO_HTML)
-{
- require_once api_get_path(LIBRARY_PATH) . 'formvalidator/Rule/HTML.php';
- $allowed_tags = HTML_QuickForm_Rule_HTML::get_allowed_tags($mode);
- $cleaned_html = kses($html, $allowed_tags);
- return $cleaned_html;
-}
-
-function html_filter_teacher($html)
-{
- return html_filter($html, TEACHER_HTML);
-}
-function html_filter_student($html)
-{
- return html_filter($html, STUDENT_HTML);
-}
+ /**
+ * Create a form validator based on an array of form data:
+ *
+ * array(
+ * 'name' => 'zombie_report_parameters', //optional
+ * 'method' => 'GET', //optional
+ * 'items' => array(
+ * array(
+ * 'name' => 'ceiling',
+ * 'label' => 'Ceiling', //optional
+ * 'type' => 'date',
+ * 'default' => date() //optional
+ * ),
+ * array(
+ * 'name' => 'active_only',
+ * 'label' => 'ActiveOnly',
+ * 'type' => 'checkbox',
+ * 'default' => true
+ * ),
+ * array(
+ * 'name' => 'submit_button',
+ * 'type' => 'style_submit_button',
+ * 'value' => get_lang('Search'),
+ * 'attributes' => array('class' => 'search')
+ * )
+ * )
+ * );
+ *
+ * @param array form_data
+ * @return FormValidator
+ */
+ public static function create($form_data)
+ {
+ if (empty($form_data)) {
+ return null;
+ }
+ $form_name = isset($form_data['name']) ? $form_data['name'] : 'form';
+ $form_method = isset($form_data['method']) ? $form_data['method'] : 'POST';
+ $form_action = isset($form_data['action']) ? $form_data['action'] : '';
+ $form_target = isset($form_data['target']) ? $form_data['target'] : '';
+ $form_attributes = isset($form_data['attributes']) ? $form_data['attributes'] : null;
+ $form_track_submit = isset($form_data['track_submit']) ? $form_data['track_submit'] : true;
-function html_filter_teacher_fullpage($html)
-{
- return html_filter($html, TEACHER_HTML_FULLPAGE);
-}
+ $result = new FormValidator($form_name, $form_method, $form_action, $form_target, $form_attributes, $form_track_submit);
-function html_filter_student_fullpage($html)
-{
- return html_filter($html, STUDENT_HTML_FULLPAGE);
+ $defaults = array();
+ foreach ($form_data['items'] as $item) {
+ $name = $item['name'];
+ $type = isset($item['type']) ? $item['type'] : 'text';
+ $label = isset($item['label']) ? $item['label'] : '';
+ if ($type == 'wysiwyg') {
+ $element = $result->add_html_editor($name, $label);
+ } else {
+ $element = $result->addElement($type, $name, $label);
+ }
+ if (isset($item['attributes'])) {
+ $attributes = $item['attributes'];
+ $element->setAttributes($attributes);
+ }
+ if (isset($item['value'])) {
+ $value = $item['value'];
+ $element->setValue($value);
+ }
+ if (isset($item['default'])) {
+ $defaults[$name] = $item['default'];
+ }
+ if (isset($item['rules'])) {
+ $rules = $item['rules'];
+ foreach ($rules as $rule) {
+ $message = $rule['message'];
+ $type = $rule['type'];
+ $format = isset($rule['format']) ? $rule['format'] : null;
+ $validation = isset($rule['validation']) ? $rule['validation'] : 'server';
+ $force = isset($rule['force']) ? $rule['force'] : false;
+ $result->addRule($name, $message, $type, $format, $validation, $reset, $force);
+ }
+ }
+ }
+ $result->setDefaults($defaults);
+ return $result;
+ }
}