Skip to content

Commit

Permalink
Add Formvalidator element of type URL - refs BT#9889 #TMI
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Jun 16, 2015
1 parent 05a764c commit aa3d79d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions main/inc/lib/extra_field.lib.php
Expand Up @@ -60,6 +60,7 @@ class ExtraField extends Model
const FIELD_TYPE_FILE_IMAGE = 16;
const FIELD_TYPE_FLOAT = 17;
const FIELD_TYPE_FILE = 18;
const FIELD_TYPE_VIDEO_URL = 19;

public $type = 'user';
public $pageName;
Expand Down Expand Up @@ -333,6 +334,7 @@ public static function get_extra_fields_by_handler($handler)
$types[self::FIELD_TYPE_FILE_IMAGE] = get_lang('FieldTypeFileImage');
$types[self::FIELD_TYPE_FLOAT] = get_lang('FieldTypeFloat');
$types[self::FIELD_TYPE_FILE] = get_lang('FieldTypeFile');
$types[self::FIELD_TYPE_VIDEO_URL] = get_lang('FieldTypeVideoUrl');

switch ($handler) {
case 'course':
Expand Down Expand Up @@ -1437,6 +1439,14 @@ public function set_extra_fields_in_form(
}
}
break;
case ExtraField::FIELD_TYPE_VIDEO_URL:
$form->addUrl(
"extra_{$field_details['variable']}",
$field_details['display_text'],
false,
['placeholder' => 'https://']
);
break;
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions main/inc/lib/formvalidator/Element/Url.php
@@ -0,0 +1,31 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Url element
*
* Class Url
*/
class Url extends HTML_QuickForm_text
{

/**
* Constructor of Url class
* @param type $elementName
* @param type $elementLabel
* @param type $attributes
*/
public function Url($elementName = null, $elementLabel = null, $attributes = null)
{
if (!isset($attributes['id'])) {
$attributes['id'] = $elementName;
}

$attributes['type'] = 'url';
$attributes['class'] = 'form-control';

parent::__construct($elementName, $elementLabel, $attributes);

$this->setType('url');
}

}
19 changes: 19 additions & 0 deletions main/inc/lib/formvalidator/FormValidator.class.php
Expand Up @@ -973,6 +973,25 @@ public static function getDefaultRenderer()
isset($GLOBALS['_HTML_QuickForm_default_renderer']) ?
$GLOBALS['_HTML_QuickForm_default_renderer'] : null;
}

/**
* Adds a input of type url to the form.
* @param type $name The label for the form-element
* @param type $label The element name
* @param type $required Optional. Is the form-element required (default=true)
* @param type $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');
}
}

}

/**
Expand Down

0 comments on commit aa3d79d

Please sign in to comment.