Author - StudySection Post Views - 1,197 views
moodle

Moodle Form Validation

Introduction

Forms are a great way to gather information from visitors and allow them to submit information to your website, for example, Submit feedback to the website. Mostly, we want to ensure that customers don’t leave specific form fields blank, or we want to limit a field to a certain type of input such as characters, numbers, email addresses, etc. At this time we need to validate form fields. If we validate form input fields before the data is sent to the database or website, then the visitor gets a chance to correct the data. The goal of Form Validation is to ensure that the user provides necessary and properly formatted information needed to complete the form submission.
As we all know, Moodle has its own Forms library which has lots of accessibility code and error checking by default. Form API is used to create a form. When we create the form by using this Form API we need to include formslib.php. So we have two ways to add Form Validation in the forms lib. These are explained as follows:

  1. MoodleQuickForm::addRule()
  2. moodleform::validation()

MoodleQuickForm::addRule()

Moodle Quick Forms are standard and most used method for form creation. This is the easiest method to apply validation on moodle form. We use this inside the moodleform::definition method. First, we add an element in form after that we can apply addRule() for validation. There are some common rule types that we use via this method like required, maxlength, minlength, email, regex, numeric, etc. Below is the syntax to use addRule().


$mform->addRule('elementname', get_string('error'), 'rule type', 'extraruledata', 'client’);

Now we are going to describe the syntax:

  1. The first param ‘elementname’ is an element name.
  2. The second ‘get_string(‘error’)’ is the error message that will be displayed to the user.
  3. The third parameter ‘rule type’ is a type of rule like (required, minlength, etc).
  4. The fourth param ‘extraruledata’ is used for extra data needed with some rules such as minlength(7), regex(a-z), etc.
  5. The fifth parameter ‘client’ validates input data on the server or client-side, if validation is done on the client-side then it will be checked on the server-side as well. This is an optional parameter.

Now here is the example of fullname field with required and maxlength rules :

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','fullname', get_string('fullnamecourse'), ['size' => 50]);
$mform->addHelpButton('fullname', 'fullnamecourse');
$mform->addRule('fullname', get_string('maximumchars', '', 1333), 'maxlength', 1333); //maxlength rule applied
$mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client'); //required rule applied
$mform->setType('fullname', PARAM_TEXT); //Set type of element

}
}

moodleform::validation()

This is another method to apply validation on moodle form. This is commonly used for custom validation. When we create a form by extending moodleform class, we need to define a method validation() to make our own custom validation for the form. After that, we need to call the moodleform parent validation() method. Then we will apply our own rules. As a result, this will return an array of ‘errors’ or ‘null’. Here is the syntax:


class edit_form extends moodleform {
// Perform some extra moodle validation
function validation($data, $files) {
$errors = parent::validation($data, $files); // call parent validation function
// then apply your own rules
return $errors;
}
}

Now here is the example of full name field already existing rule :


class edit_form extends moodleform {
function definition() {
// form creation code go here;
}

// Perform some extra moodle validation
function validation($data, $files) {
$errors = parent::validation($data, $files);
global $DB;
// Add field validation check for duplicate fullname.
if ($course = $DB->get_record('course', array('fullname'=> $data['fullname'), '*', IGNORE_MULTIPLE)) {
if (empty($data['id']) || $course->id != $data['id']) {
$errors['fullname'] = get_string('fullname'taken, '', $course->fullname);
}
}
return $errors;
}
}

If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.

Leave a Reply

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

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri