Table of Contents

Forms in DokuWiki

DokuWiki has a form system which is strictly OOP and can be found in dokuwiki\Form\Form. It has two advantages:

This system was introduced since the “Elenor” 2016 release. Before, HTML forms in DokuWiki are built with a form builder class located in Doku_Form (deprecated). It was replaced because its syntax was cumbersome and unflexible. For now events still work on the old system, but we recommend to use only new system when creating forms in your plugins yourself.

This page only describes the new system.

Basic usage

A form is created by instantiating a Form class, adding Elements to it and call the toHTML() method on it.

Existing elements can be found via querying methods (findPositionBy*()). New elements can be added by using the addElement() method or by using the convenience methods (like addTextInput(), setHiddenField()) for the most common elements.

Each element can be modified through its own methods (like attr() or addClass())

Examples

simple Example

function createSimpleForm() {
    // create an empty Form object with default attributes
    $form = new dokuwiki\Form\Form();
 
    // add an <input> field with the 'name'-Attribute 'inputName' and the label 'Label'
    $form->addTextInput('inputName', 'Label');
 
    // Generate the HTML-Representation of the form
    return $form->toHTML();
}

slightly advanced example

function createAdvancedForm() {
 
    // you can set the attributes of the form during construction
    $form = new dokuwiki\Form\Form(array('id' => 'my_special_form'));
 
    $form->addClass('visible');
 
    /*
     * $form->addTextInput etc. not only adds the element to the form,
     * but also returns a reference to it
     */
    $input = $form->addTextInput('inputName', 'Label');
 
    /*
     * Further HTML attributes can be set with the Form::attr(attribute, value) 
     * and Form::attrs($associativeArray) functions.
     */
    $input->attr('placeholder', 'sample Input Text');
 
    // There are specialized functions for many Form elements
    $form->addDropdown('DropdownName', $optionsArray);
    $form->addPasswordInput('passname');
    $form->addCheckbox('checkboxName', '(this is another label)');
 
    return $form->toHTML();
}