It's better when it's simple

User Tools

Site Tools


Forms in DokuWiki

Traditionally most (all?) HTML forms in DokuWiki are built with a form builder class located in inc/form.php. It has two advantages:

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

However the syntax of this form system is cumbersome and unflexible. This is why it will slowly be replaced with a new system included since the Elenor release.

This new form system is strictly OOP and can be found in inc/Form/Form.php. For now all events still work on the old system, but we recommend to use the 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()) 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: 2016-05-27 11:16 by andi

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