It's better when it's simple

User Tools

Site Tools


Forms in DokuWiki

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

  • PHP code is cleaner
  • Plugins can easily modify these forms in form related events

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())


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->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->addCheckbox('checkboxName', '(this is another label)');
    return $form->toHTML();
devel/form.txt · Last modified: 2023-09-01 14:47 by Klap-in

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki