DokuWiki

It's better when it's simple

ユーザ用ツール

サイト用ツール


ja:devel:form

DokuWiki のフォーム

伝統的にほとんど(すべて?)の DokuWiki 内の HTML フォームは、inc/form.php にあるフォーム構築クラスで構築されています。 これには二つの利点があります:

  • PHP コードがキレイです。
  • フォーム関連のイベントでプラグインが簡単にこれらのフォームを変更できます。

しかし、このフォームシステムの構文は扱いにくく柔軟性がありません。 この理由で、Elenor 版以降、新しいシステムに次第に置換えられていきます。

この新しいフォームシステムは厳密なオブジェクト指向で inc/Form/Form.php にあります。 今はまだすべてのイベントがまだ古いシステム上で動作しますが、自分のプラグインでフォームを作成する場合は新しいシステムを使用することをお勧めします。

このページでは、新しいシステムを説明します。

基本的な使用法

From クラスをインスタント化し、要素を追加し、その上で toHTML() メッソドを呼び出すことでフォームを作成します。

紹介メソッド(findPositionBy*())を通して既存の要素を探すことできます。 addElement() メソッドを使うかより基本的な要素のための(addTextInput() のような)簡便性メソッドを使って新しい要素を追加できます。

attr()addClass() のような)独自のメソッドによって各要素は変更できます。

簡単な例

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

少し高度な例

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();
}
ja/devel/form.txt · 最終更新: 2016-06-27 22:41 by sawachan

特に明示されていない限り、本Wikiの内容は次のライセンスに従います: 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