waHtmlControl

Generation of HTML controls

Contents...

In most cases, to obtain HTML code of a form element, it is sufficient to simply call method getControl.

Should you need to create an element of a custom type, first call method registerControl, which must return HTML of such a custom element. Then you may call method getControl by passing your custom element type ID as one of its parameters.

Methods

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

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

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

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
    }
}