In most cases, to obtain HTML code of a form element, it is sufficient to simply call method getControl
.
In order to create an element of a custom type, first call method registerControl
, which must return HTML of such a custom element. Then call method getControl
by passing your custom element type ID as its parameter.
Methods
-
addNamespace
Adds namespace to the value of
name
attribute of a control. -
getControl
Returns control's HTML code.
-
registerControl
Adds a custom control type to the list of available ones.
public static final function addNamespace (&$params, $namespace = '')
Adds namespace to the value of name
attribute of a control. The namespace is added through generation of an array variable name where the single array key is the original control name
value.
Parameters
-
&$params
Array of control parameters as described for getControl method.
-
$namespace
Namespace value in the form of a string or an array of strings. If simple string value is specified, then the name of the variable sent to the server becomes the specified namespace value, and the single key of the sent array variable becomes the original
name
value. If an array is specified, then the first array item becomes the name of the variable sent to the server, each subsequent array item becomes the sinle key of a nested sub-array, and the originalname
value becomes the single key of the most deeply nested sub-array.
Example
$params = array(); waHtmlControl::addNamespace($params, 'some'); $control = waHtmlControl::getControl(waHtmlControl::INPUT, 'test', $params);
Результат
<input ... name="some[test]" ...>
Example
$params = array(); waHtmlControl::addNamespace($params, array('some', 'other')); $control = waHtmlControl::getControl(waHtmlControl::INPUT, 'test', $params);
Результат
<input ... name="some[other][test]" ...>
public static function getControl ($type, $name, $params = array())
Returns control's HTML code.
Parameters
-
$type
Control type: one of default values or a custom control type, which must be registered in previous code using registerControl method.
Default control types:
waHtmlControl::CHECKBOX
: tag<input type="checkbox">
waHtmlControl::CONTACTFIELD
: drop-down list of contact fields available in Contacts app.waHtmlControl::FILE
: tag<input type="file">
waHtmlControl::GROUPBOX
: group of tags<input type="checkbox>
waHtmlControl::HELP
: tag<p>
with textwaHtmlControl::HIDDEN
: tag<input type="hidden>
waHtmlControl::INPUT
: tag<input type="text">
waHtmlControl::PASSWORD
: tag<input type="password">
waHtmlControl::RADIOGROUP
: group of tags<input type="radio">
waHtmlControl::SELECT
: tag<select>
waHtmlControl::TEXTAREA
: tag<textarea>
waHtmlControl::TITLE
: tag<h3>
as a header
-
$name
String to be used as the value of the
name
attribute, with the'namespace'
value specified in control parameters taken into account. -
$params
Parameters used for generating controls of different types.
-
waHtmlControl::CHECKBOX
value
: value ofvalue
attribute; defaults to 1.class
: value ofclass
attribute.style
: value ofstyle
attribute.title
: value oftitle
attribute.checked
: optional flag requiring to add attributechecked
; defaults tofalse
.
-
waHtmlControl::CONTACTFIELD
value
: ID of contact field which must be auto-selected in the drop-down list.
-
waHtmlControl::FILE
class
: value ofclass
attribute.style
: value ofstyle
attribute.
-
waHtmlControl::GROUPBOX
options
: array ofcheckbox
type elements data. Two modes of specifying the data array are supported:
Mode 1: the key of each array item is the value for thename
attribute of the correspondingcheckbox
element, and its value is the text caption to be displayed next to thecheckbox
element; e.g.:
'options' => array( 1 => 'first', 2 => 'second', 3 => 'third', ),
Mode 2: each array item is a sub-array with keys'value'
and'title'
containing the value for thename
attribute and the text caption respectively; e.g.:
'options' => array( array( 'value' => 1, 'title' => 'first', ), array( 'value' => 2, 'title' => 'second', ), array( 'value' => 3, 'title' => 'third', ), ),
Instead of specifying a fixed array of options using the
'options'
element, you can assign a dynamically generated set of options using a'options_callback'
element with acallable
type value. Example:'options_callback' => array('appMyClass', 'myCallbackMethod'),
An array of options must be generated by the method specified within a
callable
type value.value
: an array of values forname
attribute specified in'options'
array, corresponding to thecheckbox
elements which must be checked by default.options_wrapper
: array of parameters used to apply custom design to individualCHECKBOX
within commonGROUPBOX
element:title_wrapper
: wrapper around title next toCHECKBOX
element; default value:' %s'
,description_wrapper
: wrapper around description next toCHECKBOX
element; default value:'<span class="hint">%s</span>'
,control_separator
: HTML code added between individualCHECKBOX
elements; default value:'<br>'
,custom_control_separator
: HTML code to override the value ofcontrol_separator
.
-
waHtmlControl::HELP
value
: text to be added inside tag<p>
.class
: value ofclass
attribute.
-
waHtmlControl::HIDDEN
value
: value ofvalue
attribute.class
: value ofclass
attribute.
-
waHtmlControl::INPUT
andwaHtmlControl::PASSWORD
value
: value ofvalue
attribute.class
: value ofclass
attribute.style
: value ofstyle
attribute.size
: value ofsize
attribute.maxlength
: value ofmaxlength
attribute.placeholder
: value ofplaceholder
attribute.readonly
: flag requiring to add attributereadonly
; defaults tofalse
.autocomplete
: flag requiring to add attributeautocomplete="on|off"
; by default, the attribute is not added.autofocus
: flag requiring to add attributeautofocus
; defaults tofalse
.
-
waHtmlControl::RADIOGROUP
options
: array ofradio
type elements data. Two modes of specifying the data array are supported:
Mode 1: the key of each array item is the value of thevalue
attribute of the correspondingradio
element, and its value is the text caption to be displayed next to theradio
element; e.g.:
'options' => array( 1 => 'first', 2 => 'second', 3 => 'third', ),
Mode 2: each array item is a sub-array with keys'value'
and'title'
containing the value of thevalue
attribute and the text caption respectively; e.g.:
'options' => array( array( 'value' => 1, 'title' => 'first', ), array( 'value' => 2, 'title' => 'second', ), array( 'value' => 3, 'title' => 'third', ), ),
Instead of specifying a fixed array of options using the
'options'
element, you can assign a dynamically generated set of options using a'options_callback'
element with acallable
type value. Example:'options_callback' => array('appMyClass', 'myCallbackMethod'),
An array of options must be generated by the method specified within a
callable
type value.value
: the value of avalue
attribute from the'options'
array corresponding to theradio
element which must be selected by default.control_separator
: optional portion of HTML code to be added between each pair ofradio
elements.custom_control_separator
: HTML code to override the value ofcontrol_separator
.
-
waHtmlControl::SELECT
options
: array ofoption
elements data. Two modes of specifying the data array are supported:
Mode 1: the key of each array item is the value of thevalue
attribute of the correspondingoption
element, and its value is the text caption to be displayed in theselect
drop-down list; e.g.:
'options' => array( 1 => 'first', 2 => 'second', 3 => 'third', ),
Array keys must be either integers or strings. Mixing up integer and string keys within one array may lead to unpredictable results.
Mode 2: each array item is a sub-array with keys'value'
and'title'
containing the value of thevalue
attribute and the text caption respectively; e.g.:
'options' => array( array( 'value' => 1, 'title' => 'first', ), array( 'value' => 2, 'title' => 'second', ), array( 'value' => 3, 'title' => 'third', ), ),
Instead of specifying a fixed array of options using the
'options'
element, you can assign a dynamically generated set of options using a'options_callback'
element with acallable
type value. Example:'options_callback' => array('appMyClass', 'myCallbackMethod'),
An array of options must be generated by the method specified within a
callable
type value.value
: the value of avalue
attribute from the'options'
array corresponding to theoption
element which must be selected by default.
-
waHtmlControl::TITLE
value
: text to be added inside tag<h3>
.description
: text to be added inside tag<h4>
directly following tag<h3>
as a sub-header.class
: value ofclass
attribute.style
: value ofstyle
attribute.
-
waHtmlControl::TEXTAREA
value
: contents of thetextarea
element.class
: value ofclass
attribute.style
: value ofstyle
attribute.cols
: value ofcols
attribute.rows
: value ofrows
attribute.wrap
: value ofwrap
attribute.placeholder
: value ofplaceholder
attribute.readonly
: flag requiring to add attributereadonly
; defaults tofalse
.autofocus
: flag requiring to add attributeautofocus
; defaults tofalse
.wysiwyg
: non-empty array of parameters for callingfromTextArea
method of JavaScript library CodeMirror. When such an array is provided, which can also be replaced with a simpletrue
value, atextarea
element is transformed into a custom source code editing element with code highlighting feature. For this option to work, CSS and JavaScript files from directorywa-content/js/codemirror/
must be included on a page. When thewysiwyg
parameter is specified, the values of parametersclass
,style
,cols
,rows
,wrap
,placeholder
,readonly
,autofocus
are ignored.
-
Optional parameters common for all control types
namespace
: namespace value.id
: additional value to be added with an underscore character _ at the beginning of the automatically generated value of theid
attribute.description
: value of'description'
parameter, usually displayed under a control.control_wrapper
: template of the HTML wrapper inside which a control's HTML code must be displayed; the template must contain 3%s
snippets, each corresponding, in the specified order, to 1) the value of'title'
parameter, 2) a control's HTML code, 3) the value of'description'
parameter.title_wrapper
: template of HTML wrapper for'title'
parameter; default value is%s:
, i.e. text followed by a colon.description_wrapper
: template of HTML wrapper for value of'description'
parameter.custom_title_wrapper
: template to override the value oftitle_wrapper
.custom_description_wrapper
: template to override the value ofdescription_wrapper
.custom_control_wrapper
: template to override the value ofcontrol_wrapper
.
-
Example
$control_params = array( 'namespace' => 'settings', 'control_wrapper' => '<div class="field"><div class="name">%s</div><div class="value">%s%s</div></div>', 'title_wrapper' => '%s', 'description_wrapper' => '<br><span class="hint">%s</span>' ); $controls = array( waHtmlControl::getControl(waHtmlControl::CHECKBOX, 'setting1', array( 'checked' => true, 'value' => 1, 'title' => 'Checkbox', 'description' => 'Enable or disable it', ) + $control_params), waHtmlControl::getControl(waHtmlControl::CONTACTFIELD, 'setting2', array( 'title' => 'Contact field', 'description' => 'Seletc one from the list', ) + $control_params), waHtmlControl::getControl(waHtmlControl::FILE, 'setting3', array( 'title' => 'File', 'description' => 'Browse for a file on your computer', ) + $control_params), waHtmlControl::getControl(waHtmlControl::GROUPBOX, 'setting4', array( 'options' => array( 1 => 'first', 2 => 'second', 3 => 'third', ), 'value' => array(1, 3), 'title' => 'Checkboxes', 'description' => 'Select any options', ) + $control_params), waHtmlControl::getControl(waHtmlControl::HIDDEN, 'setting5', array( 'value' => ifset($setting5), )), waHtmlControl::getControl(waHtmlControl::INPUT, 'setting6', array( 'title' => 'Text string', 'placeholder' => 'enter short text', ) + $control_params), waHtmlControl::getControl(waHtmlControl::PASSWORD, 'setting7', array( 'title' => 'Password', 'placeholder' => 'enter a password', ) + $control_params), waHtmlControl::getControl(waHtmlControl::RADIOGROUP, 'setting8', array( 'options' => array( 1 => 'first', 2 => 'second', 3 => 'third', ), 'value' => 2, 'title' => 'Radio buttons', 'description' => 'Select one of the options', ) + $control_params), waHtmlControl::getControl(waHtmlControl::SELECT, 'setting9', array( 'options' => array( 1 => 'first', 2 => 'second', 3 => 'third', ), 'value' => 3, 'title' => 'Drop-down list', 'description' => 'Select one of the items', ) + $control_params), waHtmlControl::getControl(waHtmlControl::TEXTAREA, 'setting10', array( 'value' => ifset($setting9), 'title' => 'Multi-line field', 'placeholder' => 'enter long text', ) + $control_params), );
public static function registerControl ($type, $callback)
Adds a custom control type to the list of available ones, which can be used with getControl method.
Parameters
-
$type
String value of a custom control type, written with the first letter in the upper case and the rest of the string written in the lower case.
-
$callback
A value which can be called as a function (see documentation for PHP function is_callable). Such a function, which is usually a static method of a PHP class, must accept the value of control's
name
attribute and an array of parameters for control generation similar to those described for default controls mentioned in the description of getControl method, and must return the custom control's HTML code.
Example
class someClass { public function execute() { //... waHtmlControl::registerControl('Mycontrol', array(__CLASS__, 'getControl')); wa()->getView()->assign('mycontrol', waHtmlControl::getControl('Mycontrol', $params)); //... } public static function getControl($name, $params = array()) { //... implementation of a custom control return '...'; //return custom control's HTML code } }