All web developers must already know how to create forms with html, namely by using the <form> tag. But moodle has provided a library to make the form not input html script but this uses php, by calling the formlib.php library.
require_once("$CFG->libdir/formslib.php");
I'm telling you this is a continuation of the previous chapter so we will still be coding in the same directory.1. Create a folder with the name "form" then create basicform.php in the form folder. Also create form.php in the myplugin folder then the directory structure will be like this.
myplugin/ ├─ templates/ ├─ lang/ ├─ db/ ├─ form/ │ ├─ basicform.php ├─ index.php ├─ lib.php ├─ version.php ├─ form.php ├─ template.php
<?php
//moodleform is defined in formslib.php
require_once("$CFG->libdir/formslib.php");
Class basicform extends moodleform {
//Add elements to form
public function definition() {
global $CFG,$COURSE;
$mform = $this->_form; // Don't forget the underscore!
/** name input **/
$mform->addElement('text', 'name', get_string('name')); // Add elements to your form.
$mform->setType('name', PARAM_NOTAGS); // Set type of element.
$mform->setDefault('name', ''); // Default value.
/** gender form input **/
$options = array(
'male' => 'Male',
'female' => 'Female'
);
$select = $mform->addElement('select', 'gender', 'Sex', $options);
// This will select the male.
$select->setSelected('male');
/** address form input **/
$mform->addElement('editor', 'address', get_string('address')); // Add elements to your form.
$mform->setType('address', PARAM_NOTAGS); // Set type of element.
$mform->setDefault('address', ''); // Default value.
/** file input **/
$mform->addElement('filepicker', 'userfile', get_string('file'), null,
array('maxbytes' => $maxbytes, 'accepted_types' => '*'));
$this->add_action_buttons();
}
//Custom validation should be added here
function validation($data, $files) {
return array();
}
}
Look at the code above, we have 4 input forms namely name with type text, gender with type option select, address with type texteditor, userfile with type file input. You can see other elements on the official website .3. Next we will call basicform.php in form.php
//CALL FORM FUNCTION
require_once('form/basicform.php');<?php
/**
* @package local_myplugin
* @author Deril Lab
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
//CALL CONFIG.PHP
require_once(__DIR__ . '/../../config.php');
//CALL FORM FUNCTION
require_once('form/basicform.php');
$PAGE->set_url(new moodle_url('/local/myplugin/index.php'));
//SET TITLE PAGE
$PAGE->set_title(get_string("pluginname", "local_myplugin"));
$PAGE->set_heading(get_string("pluginname", "local_myplugin"));
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 5, PARAM_INT);
GLOBAL $DB;
GLOBAL $CFG;
GLOBAL $USER;
//PRIVATE PAGE FOR PUBLIC
require_login();
//SET BREADCRHUM
$PAGE->navbar->add(get_string("pluginname", "local_myplugin"));
//GET HEADER
echo $OUTPUT->header();
/** FORM EXAMPLE **/
// Declare form function
$mform = new basicform();
//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()) {
//In this case you process validated data. $mform->get_data() returns data posted in form.
print_r($fromform);
} else {
// this branch is executed if the form is submitted but the data doesn't validate and the form should be redisplayed
// or on the first display of the form.
//Set default data (if any)
$toform = array(
'name'=>'Deril',
'gender'=>'male'
);
$mform->set_data($toform);
//displays the form
$mform->display();
}
/** END FORM **/
//GET FOOTER
echo $OUTPUT->footer();
?>
0 Comments