Types of Form Creation in Moodle

|
| By Webner

Introduction

A Document that contains different blank fields like(Name, DOB, Class, etc.) that the user can fill the data is called form. The form can be of many types like(Application Form, Contact Form, etc.). Forms are very helpful to store data in the database, with the help of forms we can add various functionalities to our systems. When we work in moodle, we have different types of form creation, here we will discuss two mainly used methods of form creation in moodle. There are two standard methods of form creation, these are explained as follows:
1) Moodle Forms Using Form API
2) Moodle Forms Using html_writer

Using Form API:

Moodle Quick Forms is a standard and most used method for form creation. As we know Moodle has its own Forms Library which includes plenty of accessibility code and error checking by default. In this method, Form API is used to create a form. This API supports all types of HTML form elements like checkbox, radio, textbox, select, etc. Moodle Forms is defined in moodle core file i.e formslib.php.

Steps to create a form:

  1. Firstly, we will include the file formslib.php as below:

    require_once("$CFG->libdir/formslib.php");

  2. After that, we need to create a class that extends the moodleform class.

    class edit_form extends moodleform {}

  3. Then we need to add html elements in the form. We can also set element type or default value in the form. Below is the full code of form creation:

    require_once("$CFG->libdir/formslib.php");
    class edit_form extends moodleform {
    //Add elements to form
    public function definition() {
    global $CFG;
    $mform = $this->_form; // Don't forget the underscore!
    $mform->addElement('text', 'email', get_string('email'));
    $mform->setType('email', PARAM_NOTAGS); //Set type of element
    $mform->setDefault('email', 'Please enter email'); //Default value
    $mform->addElement('select', 'lang', get_string('lang'));
    $mform->addElement('submit', 'submit', get_string(‘submit’));
    }
    }

Display the form:

After Form Creation, you need to represent it, so first you need to include the form then create an object of the above-created class where you want to show the form. Here is the syntax to show form.

//include edit_form.php
require_once('edit_form.php');
//create object
$mform = new edit_form();
//Form processing and displaying is done here
if ($mform->is_cancelled()) {
//Handle form cancel operation, if cancel button is present on form
} else if ($fromform = $mform->get_data()) {
// returns data posted in form.
} else {
//Set default data (if any)
$mform->set_data($toform);
//displays the form
$mform->display();
}

Using HTML writer:

Moodle has a class called html_writer which allows you to write HTML tags manually. This is typically used within renderer functions, for example, mod/facetofdace/renderer.php. In this method, we don’t need to add the formslib.php file. By using this method, first, we need to define a variable for a form then we will use this form variable to add HTML elements in the form and to display the form. For example, we created a search form. Here is the syntax to create a form by using this method:

$searchform = ' ';
$searchform .= html_writer::empty_tag( 'input', array('id' => 'searchtext', 'class' => 'searchtextreserve', 'type' => 'text', 'name' => 'searchtext', 'size' => '20', 'value' => ‘text’));
$searchform .= html_writer::empty_tag( 'input', array('name' => 'search', 'id' => 'search', 'type' => 'submit', 'value' => 'Search', 'title' => 'search'));
$searchform = html_writer::tag( 'form', $form, array( 'method' => 'post', 'id' => 'search_allocate'));
$searchform .= html_writer::empty_tag('br');

To display this form we need to use standard PHP display statement i.e.

echo $searchform;

Leave a Reply

Your email address will not be published. Required fields are marked *