tag * @param string $layout * @param bool $trackSubmit (optional) Whether to track if the form was * submitted by adding a special hidden field (default = true) */ public function __construct( $name, $method = 'post', $action = '', $target = '', $attributes = array(), $layout = self::LAYOUT_HORIZONTAL, $trackSubmit = true ) { // Default form class. if (is_array($attributes) && !isset($attributes['class']) || empty($attributes)) { $attributes['class'] = 'form-horizontal'; } if (isset($attributes['class']) && strpos($attributes['class'], 'form-search') !== false) { $layout = 'inline'; } $this->setLayout($layout); switch ($layout) { case self::LAYOUT_HORIZONTAL: $attributes['class'] = 'form-horizontal'; break; case self::LAYOUT_INLINE: case self::LAYOUT_BOX: $attributes['class'] = 'form-inline'; break; } parent::__construct($name, $method, $action, $target, $attributes, $trackSubmit); // Modify the default templates $renderer = & $this->defaultRenderer(); // Form template $formTemplate = $this->getFormTemplate(); $renderer->setFormTemplate($formTemplate); // Element template if (isset($attributes['class']) && $attributes['class'] == 'form-inline') { $elementTemplate = ' {label} {element} '; $renderer->setElementTemplate($elementTemplate); } elseif (isset($attributes['class']) && $attributes['class'] == 'form-search') { $elementTemplate = ' {label} {element} '; $renderer->setElementTemplate($elementTemplate); } else { $renderer->setElementTemplate($this->getDefaultElementTemplate()); // Display a gray div in the buttons $templateSimple = '
{label} {element}
'; $renderer->setElementTemplate($templateSimple, 'submit_in_actions'); //Display a gray div in the buttons + makes the button available when scrolling $templateBottom = '
{label} {element}
'; $renderer->setElementTemplate($templateBottom, 'submit_fixed_in_bottom'); //When you want to group buttons use something like this /* $group = array(); $group[] = $form->createElement('button', 'mark_all', get_lang('MarkAll')); $group[] = $form->createElement('button', 'unmark_all', get_lang('UnmarkAll')); $form->addGroup($group, 'buttons_in_action'); */ $renderer->setElementTemplate($templateSimple, 'buttons_in_action'); $templateSimpleRight = '
{label} {element}
'; $renderer->setElementTemplate($templateSimpleRight, 'buttons_in_action_right'); } //Set Header template $renderer->setHeaderTemplate('{header}'); //Set required field template $this->setRequiredNote('* '.get_lang('ThisFieldIsRequired').''); $noteTemplate = <<
{requiredNote}
EOT; $renderer->setRequiredNoteTemplate($noteTemplate); } /** * @return string */ public function getFormTemplate() { return '
{content}
{hidden} '; } /** * @todo this function should be added in the element class * @return string */ public function getDefaultElementTemplate() { return '
{icon} {element}

{label_2}

{error}
{label_3}
'; } /** * @return string */ public function getLayout() { return $this->layout; } /** * @param string $layout */ public function setLayout($layout) { $this->layout = $layout; } /** * Adds a text field 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 bool $required (optional) Is the form-element required (default=true) * @param array $attributes (optional) List of attributes for the form-element * @return HTML_QuickForm_text */ public function addText($name, $label, $required = true, $attributes = array()) { $element = $this->addElement('text', $name, $label, $attributes); $this->applyFilter($name, 'trim'); if ($required) { $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required'); } return $element; } /** * The "date_range_picker" element creates 2 hidden fields * "elementName" + "_start" and "elementName" + "_end" * For example if the name is "range", you will have 2 new fields * when executing $form->getSubmitValues() * "range_start" and "range_end" * * @param string $name * @param string $label * @param bool $required * @param array $attributes */ public function addDateRangePicker($name, $label, $required = true, $attributes = array()) { $this->addElement('date_range_picker', $name, $label, $attributes); $this->addElement('hidden', $name.'_start'); $this->addElement('hidden', $name.'_end'); if ($required) { $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required'); } } /** * @param string $name * @param string $label * @param array $attributes * * @return mixed */ public function addDatePicker($name, $label, $attributes = []) { return $this->addElement('DatePicker', $name, $label, $attributes); } /** * @param string $name * @param string $label * @param array $attributes * * @return mixed */ public function addSelectLanguage($name, $label, $options = [], $attributes = []) { return $this->addElement('SelectLanguage', $name, $label, $options, $attributes); } /** * @param string $name * @param string $label * @param array $options * @param array $attributes * @throws */ public function addSelectAjax($name, $label, $options = [], $attributes = []) { if (!isset($attributes['url'])) { throw new \Exception('select_ajax needs an URL'); } $this->addElement( 'select_ajax', $name, $label, $options, $attributes ); } /** * @param string $name * @param string $label * @param array $attributes * * @return mixed */ public function addDateTimePicker($name, $label, $attributes = []) { return $this->addElement('DateTimePicker', $name, $label, $attributes); } /** * @param string $name * @param string $value */ public function addHidden($name, $value) { $this->addElement('hidden', $name, $value); } /** * @param string $name * @param string $label * @param array $attributes * * @return HTML_QuickForm_textarea */ public function addTextarea($name, $label, $attributes = array()) { return $this->addElement('textarea', $name, $label, $attributes); } /** * @param string $name * @param string $label * @param string $icon font-awesome * @param string $style default|primary|success|info|warning|danger|link * @param string $size large|default|small|extra-small * @param string $class Example plus is transformed to icon fa fa-plus * @param array $attributes * * @return HTML_QuickForm_button */ public function addButton( $name, $label, $icon = 'check', $style = 'default', $size = 'default', $class = null, $attributes = array(), $createElement = false ) { if ($createElement) { return $this->createElement( 'button', $name, $label, $icon, $style, $size, $class, $attributes ); } return $this->addElement( 'button', $name, $label, $icon, $style, $size, $class, $attributes ); } /** * Returns a button with the primary color and a check mark * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * * @return HTML_QuickForm_button */ public function addButtonSave($label, $name = 'submit', $createElement = false) { return $this->addButton( $name, $label, 'check', 'primary', null, null, array(), $createElement ); } /** * Returns a cancel button * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * * @return HTML_QuickForm_button */ public function addButtonCancel($label, $name = 'submit', $createElement = false) { return $this->addButton( $name, $label, 'times', 'danger', null, null, array(), $createElement ); } /** * Returns a button with the primary color and a "plus" icon * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * @param array $attributes Additional attributes * * @return HTML_QuickForm_button */ public function addButtonCreate($label, $name = 'submit', $createElement = false, $attributes = array()) { return $this->addButton( $name, $label, 'plus', 'primary', null, null, $attributes, $createElement ); } /** * Returns a button with the primary color and a pencil icon * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * @return HTML_QuickForm_button */ public function addButtonUpdate($label, $name = 'submit', $createElement = false) { return $this->addButton( $name, $label, 'pencil', 'primary', null, null, array(), $createElement ); } /** * Returns a button with the danger color and a trash icon * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * * @return HTML_QuickForm_button */ public function addButtonDelete($label, $name = 'submit', $createElement = false) { return $this->addButton( $name, $label, 'trash', 'danger', null, null, array(), $createElement ); } /** * Returns a move style button * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * * @return HTML_QuickForm_button */ public function addButtonMove($label, $name = 'submit', $createElement = false) { return $this->addButton( $name, $label, 'arrow-circle-right', 'primary', null, null, array(), $createElement ); } /** * Returns a button with the primary color and a paper-plane icon * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * * @return HTML_QuickForm_button */ public function addButtonSend($label, $name = 'submit', $createElement = false, $attributes = array()) { return $this->addButton( $name, $label, 'paper-plane', 'primary', null, null, $attributes, $createElement ); } /** * Returns a button with the default (grey?) color and a magnifier icon * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * * @return HTML_QuickForm_button */ public function addButtonSearch($label = null, $name = 'submit', $attributes = array()) { if (empty($label)) { $label = get_lang('Search'); } return $this->addButton($name, $label, 'search', 'default', null, null, $attributes); } /** * Returns a button with the primary color and a right-pointing arrow icon * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param array $attributes Additional attributes * @return HTML_QuickForm_button */ public function addButtonNext($label, $name = 'submit', $attributes = array()) { return $this->addButton( $name, $label, 'arrow-right', 'primary', null, null, $attributes ); } /** * Returns a button with the primary color and a check mark icon * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * @return HTML_QuickForm_button */ public function addButtonImport($label, $name = 'submit', $createElement = false) { return $this->addButton( $name, $label, 'check', 'primary', null, null, array(), $createElement ); } /** * Returns a button with the primary color and a check-mark icon * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * @return HTML_QuickForm_button */ public function addButtonExport($label, $name = 'submit', $createElement = false) { return $this->addButton( $name, $label, 'check', 'primary', null, null, array(), $createElement ); } /** * Shortcut to filter button * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * @return HTML_QuickForm_button */ public function addButtonFilter($label, $name = 'submit', $createElement = false) { return $this->addButton( $name, $label, 'filter', 'primary', null, null, array(), $createElement ); } /** * Shortcut to reset button * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * @return HTML_QuickForm_button */ public function addButtonReset($label, $name = 'reset', $createElement = false) { $icon = 'eraser'; $style = 'default'; $size = 'default'; $class = null; $attributes = array(); if ($createElement) { return $this->createElement( 'reset', $name, $label, $icon, $style, $size, $class, $attributes ); } return $this->addElement( 'reset', $name, $label, $icon, $style, $size, $class, $attributes ); } /** * Returns a button with the primary color and an upload icon * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * * @return HTML_QuickForm_button */ public function addButtonUpload($label, $name = 'submit', $createElement = false) { return $this->addButton( $name, $label, 'upload', 'primary', null, null, array(), $createElement ); } /** * Returns a button with the primary color and a download icon * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * * @return HTML_QuickForm_button */ public function addButtonDownload($label, $name = 'submit', $createElement = false) { return $this->addButton( $name, $label, 'download', 'primary', null, null, array(), $createElement ); } /** * Returns a button with the primary color and a magnifier icon * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * * @return HTML_QuickForm_button */ public function addButtonPreview($label, $name = 'submit', $createElement = false) { return $this->addButton( $name, $label, 'search', 'primary', null, null, array(), $createElement ); } /** * Returns a button with the primary color and a copy (double sheet) icon * @param string $label Text appearing on the button * @param string $name Element name (for form treatment purposes) * @param bool $createElement Whether to use the create or add method * * @return HTML_QuickForm_button */ public function addButtonCopy($label, $name = 'submit', $createElement = false) { return $this->addButton( $name, $label, 'copy', 'primary', null, null, array(), $createElement ); } /** * @param string $name * @param string $label * @param string $text * @param array $attributes * * @return HTML_QuickForm_checkbox */ public function addCheckBox($name, $label, $text = '', $attributes = array()) { return $this->addElement('checkbox', $name, $label, $text, $attributes); } /** * @param string $name * @param string $label * @param array $options * @param array $attributes * * @return HTML_QuickForm_group */ public function addCheckBoxGroup($name, $label, $options = array(), $attributes = array()) { $group = array(); foreach ($options as $value => $text) { $attributes['value'] = $value; $group[] = $this->createElement( 'checkbox', $value, null, $text, $attributes ); } return $this->addGroup($group, $name, $label); } /** * @param string $name * @param string $label * @param array $options * @param array $attributes * * @return HTML_QuickForm_radio */ public function addRadio($name, $label, $options = array(), $attributes = array()) { $group = array(); foreach ($options as $key => $value) { $group[] = $this->createElement('radio', null, null, $value, $key, $attributes); } return $this->addGroup($group, $name, $label); } /** * @param string $name * @param string $label * @param array $options * @param array $attributes * * @return HTML_QuickForm_select */ public function addSelect($name, $label, $options = array(), $attributes = array()) { return $this->addElement('select', $name, $label, $options, $attributes); } /** * @param $name * @param $label * @param $collection * @param array $attributes * @param bool $addNoneOption * @param string $textCallable set a function getStringValue() by default __toString() * * @return HTML_QuickForm_element */ public function addSelectFromCollection( $name, $label, $collection, $attributes = array(), $addNoneOption = false, $textCallable = '' ) { $options = []; if ($addNoneOption) { $options[0] = get_lang('None'); } if (!empty($collection)) { foreach ($collection as $item) { $text = $item; if (!empty($textCallable)) { $text = $item->$textCallable(); } $options[$item->getId()] = $text; } } return $this->addElement('select', $name, $label, $options, $attributes); } /** * @param string $label * @param string $text * * @return HTML_QuickForm_label */ public function addLabel($label, $text) { return $this->addElement('label', $label, $text); } /** * @param string $text */ public function addHeader($text) { $this->addElement('header', $text); } /** * @param string $name * @param string $label * @param array $attributes * @throws Exception if the file doesn't have an id */ public function addFile($name, $label, $attributes = array()) { $element = $this->addElement('file', $name, $label, $attributes); if (isset($attributes['crop_image'])) { $id = $element->getAttribute('id'); if (empty($id)) { throw new Exception('If you use the crop functionality the element must have an id'); } $this->addHtml('
' ); $this->addHidden($id.'_crop_result', ''); $this->addHidden($id.'_crop_image_base_64', ''); } } /** * @param string $snippet */ public function addHtml($snippet) { $this->addElement('html', $snippet); } /** * Adds a HTML-editor to the form * @param string $name * @param string $label The label for the form-element * @param bool $required (optional) Is the form-element required (default=true) * @param bool $fullPage (optional) When it is true, the editor loads completed html code for a full page. * @param array $config (optional) Configuration settings for the online editor. * @param bool $style */ public function addHtmlEditor( $name, $label, $required = true, $fullPage = false, $config = [], $style = false ) { $attributes = []; $attributes['rows'] = isset($config['rows']) ? $config['rows'] : 15; $attributes['cols'] = isset($config['cols']) ? $config['cols'] : 80; $attributes['cols-size'] = isset($config['cols-size']) ? $config['cols-size'] : []; $attributes['class'] = isset($config['class']) ? $config['class'] : []; $this->addElement('html_editor', $name, $label, $attributes, $config); $this->applyFilter($name, 'trim'); if ($required) { $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required'); } /** @var HtmlEditor $element */ $element = $this->getElement($name); if ($style) { $config['style'] = true; } if ($fullPage) { $config['fullPage'] = true; } if ($element->editor) { $element->editor->processConfig($config); } } /** * Adds a Google Maps Geolocalization field to the form * * @param $name * @param $label */ public function addGeoLocationMapField($name, $label) { $gMapsPlugin = GoogleMapsPlugin::create(); $geolocalization = $gMapsPlugin->get('enable_api') === 'true'; if ($geolocalization) { $gmapsApiKey = $gMapsPlugin->get('api_key'); $this->addHtml(''); } $this->addElement( 'text', $name, $label, ['id' => $name] ); $this->applyFilter($name, 'stripslashes'); $this->applyFilter($name, 'trim'); $this->addHtml('
'); $this->addHtml('
'); $this->addHtml( ' '); } /** * @param string $name * @param string $label * * @return mixed */ public function addButtonAdvancedSettings($name, $label = '') { $label = !empty($label) ? $label : get_lang('AdvancedParameters'); return $this->addElement('advanced_settings', $name, $label); } /** * Adds a progress loading image to the form. * */ public function addProgress($delay = 2, $label = '') { if (empty($label)) { $label = get_lang('PleaseStandBy'); } $this->with_progress_bar = true; $id = $this->getAttribute('id'); $this->updateAttributes("onsubmit=\"javascript: addProgress('".$id."')\""); $this->addHtml(''); } /** * 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 $elements The array of elements * @param string $message The message displayed */ public function add_multiple_required_rule($elements, $message) { $this->_required[] = $elements[0]; $this->addRule($elements, $message, 'multiple_required'); } /** * Displays the form. * If an element in the form didn't validate, an error message is showed * asking the user to complete the form. */ public function display() { echo $this->returnForm(); } /** * Returns the HTML code of the form. * @return string $return_value HTML code of the form */ public function returnForm() { $returnValue = ''; /** @var HTML_QuickForm_element $element */ foreach ($this->_elements as $element) { $elementError = parent::getElementError($element->getName()); if (!is_null($elementError)) { $returnValue .= Display::return_message($elementError, 'warning').'
'; break; } } $returnValue .= parent::toHtml(); // Add div-element which is to hold the progress bar $id = $this->getAttribute('id'); if (isset($this->with_progress_bar) && $this->with_progress_bar) { $icon = Display::return_icon('progress_bar.gif'); // @todo improve UI $returnValue .= '
'; } return $returnValue; } /** * 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 , Ghent University, august 2006 * @author Julio Montoya * @deprecated use returnForm() */ public function return_form() { return $this->returnForm(); } /** * 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 * @deprecated use normal FormValidator construct * * @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; $reset = null; $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->addHtmlEditor($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; } /** * @return HTML_QuickForm_Renderer_Default */ public static function getDefaultRenderer() { return isset($GLOBALS['_HTML_QuickForm_default_renderer']) ? $GLOBALS['_HTML_QuickForm_default_renderer'] : null; } /** * Adds a input of type url to the form. * @param string $name The label for the form-element * @param string $label The element name * @param bool $required Optional. Is the form-element required (default=true) * @param array $attributes Optional. List of attributes for the form-element */ public function addUrl($name, $label, $required = true, $attributes = array()) { $this->addElement('url', $name, $label, $attributes); $this->applyFilter($name, 'trim'); $this->addRule($name, get_lang('InsertAValidUrl'), 'url'); if ($required) { $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required'); } } /** * Adds a text field for letters to the form. * A trim-filter is attached to the field. * @param string $name The element name * @param string $label The label for the form-element * @param bool $required Optional. Is the form-element required (default=true) * @param array $attributes Optional. List of attributes for the form-element */ public function addTextLettersOnly( $name, $label, $required = false, $attributes = [] ) { $attributes = array_merge( $attributes, [ 'pattern' => '[a-zA-ZñÑ]+', 'title' => get_lang('OnlyLetters') ] ); $this->addElement( 'text', $name, [ $label, get_lang('OnlyLetters') ], $attributes ); $this->applyFilter($name, 'trim'); if ($required) { $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required'); } $this->addRule( $name, get_lang('OnlyLetters'), 'regex', '/^[a-zA-ZñÑ]+$/' ); } /** * Adds a text field for alphanumeric characters to the form. * A trim-filter is attached to the field. * @param string $name The element name * @param string $label The label for the form-element * @param bool $required Optional. Is the form-element required (default=true) * @param array $attributes Optional. List of attributes for the form-element */ public function addTextAlphanumeric( $name, $label, $required = false, $attributes = [] ) { $attributes = array_merge( $attributes, [ 'pattern' => '[a-zA-Z0-9ñÑ]+', 'title' => get_lang('OnlyLettersAndNumbers') ] ); $this->addElement( 'text', $name, [ $label, get_lang('OnlyLettersAndNumbers') ], $attributes ); $this->applyFilter($name, 'trim'); if ($required) { $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required'); } $this->addRule( $name, get_lang('OnlyLettersAndNumbers'), 'regex', '/^[a-zA-Z0-9ÑÑ]+$/' ); } /** * @param string $name * @param $label * @param bool $required * @param array $attributes * @param bool $allowNegative * @param integer $minValue * @param null $maxValue */ public function addFloat( $name, $label, $required = false, $attributes = [], $allowNegative = false, $minValue = null, $maxValue = null ) { $this->addElement( 'FloatNumber', $name, $label, $attributes ); $this->applyFilter($name, 'trim'); if ($required) { $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required'); } // Rule allows "," and "." /*$this->addRule( $name, get_lang('OnlyNumbers'), 'regex', '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)|(^-?\d\d*\,\d*$)|(^-?\,\d\d*$)/' );*/ if ($allowNegative == false) { $this->addRule( $name, get_lang('NegativeValue'), 'compare', '>=', 'server', false, false, 0 ); } if (!is_null($minValue)) { $this->addRule( $name, get_lang('UnderMin'), 'compare', '>=', 'server', false, false, $minValue ); } if (!is_null($maxValue)) { $this->addRule( $name, get_lang('OverMax'), 'compare', '<=', 'server', false, false, $maxValue ); } } /** * Adds a text field for letters and spaces to the form. * A trim-filter is attached to the field. * @param string $name The element name * @param string $label The label for the form-element * @param bool $required Optional. Is the form-element required (default=true) * @param array $attributes Optional. List of attributes for the form-element */ public function addTextLettersAndSpaces( $name, $label, $required = false, $attributes = [] ) { $attributes = array_merge( $attributes, [ 'pattern' => '[a-zA-ZñÑ\s]+', 'title' => get_lang('OnlyLettersAndSpaces') ] ); $this->addElement( 'text', $name, [ $label, get_lang('OnlyLettersAndSpaces') ], $attributes ); $this->applyFilter($name, 'trim'); if ($required) { $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required'); } $this->addRule( $name, get_lang('OnlyLettersAndSpaces'), 'regex', '/^[a-zA-ZñÑ\s]+$/' ); } /** * Adds a text field for alphanumeric and spaces characters to the form. * A trim-filter is attached to the field. * @param string $name The element name * @param string $label The label for the form-element * @param bool $required Optional. Is the form-element required (default=true) * @param array $attributes Optional. List of attributes for the form-element */ public function addTextAlphanumericAndSpaces( $name, $label, $required = false, $attributes = [] ) { $attributes = array_merge( $attributes, [ 'pattern' => '[a-zA-Z0-9ñÑ\s]+', 'title' => get_lang('OnlyLettersAndNumbersAndSpaces') ] ); $this->addElement( 'text', $name, [ $label, get_lang('OnlyLettersAndNumbersAndSpaces') ], $attributes ); $this->applyFilter($name, 'trim'); if ($required) { $this->addRule($name, get_lang('ThisFieldIsRequired'), 'required'); } $this->addRule( $name, get_lang('OnlyLettersAndNumbersAndSpaces'), 'regex', '/^[a-zA-Z0-9ñÑ\s]+$/' ); } /** * @param string $url */ public function addMultipleUpload($url) { $inputName = 'input_file_upload'; $this->addMultipleUploadJavascript($url, $inputName); $this->addHtml('
'.get_lang('ClickToSelectOrDragAndDropMultipleFilesOnTheUploadField').'
'.get_lang('AddFiles').'
'.get_lang('UploadFiles').'

'); } /** * * @param string $url page that will handle the upload * @param string $inputName */ private function addMultipleUploadJavascript($url, $inputName) { $icon = Display::return_icon('file_txt.gif'); $this->addHtml(" "); } /** * @param string $elementName * @param string $groupName if element is inside a group * @throws Exception */ public function addPasswordRule($elementName, $groupName = '') { // Constant defined in old config/profile.conf.php if (CHECK_PASS_EASY_TO_FIND === true) { $message = get_lang('PassTooEasy').': '.api_generate_password(); if (!empty($groupName)) { $groupObj = $this->getElement($groupName); if ($groupObj instanceof HTML_QuickForm_group) { $elementName = $groupObj->getElementName($elementName); if ($elementName === false) { throw new Exception("The $groupName doesn't have the element $elementName"); } $this->_rules[$elementName][] = array( 'type' => 'callback', 'format' => 'api_check_password', 'message' => $message, 'validation' => '', 'reset' => false, 'group' => $groupName ); } } else { $this->addRule( $elementName, $message, 'callback', 'api_check_password' ); } } } } /** * Cleans HTML text filter * @param string $html HTML to clean * @param int $mode (optional) * @return string The cleaned HTML */ function html_filter($html, $mode = NO_HTML) { $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); } function html_filter_teacher_fullpage($html) { return html_filter($html, TEACHER_HTML_FULLPAGE); } function html_filter_student_fullpage($html) { return html_filter($html, STUDENT_HTML_FULLPAGE); } /** * Cleans mobile phone number text * @param string $mobilePhoneNumber Mobile phone number to clean * @return string The cleaned mobile phone number */ function mobile_phone_number_filter($mobilePhoneNumber) { $mobilePhoneNumber = str_replace(array('+', '(', ')'), '', $mobilePhoneNumber); return ltrim($mobilePhoneNumber, '0'); }