O'Reilly logo

Moodle JavaScript Cookbook by Alastair Hole

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding a required field

In this recipe, we will build a new web form using the QuickForm library, comprised of one text box field with "required field" validation applied.

This type of form validation is the simplest and most frequently used validation. It prevents the user from leaving the field blank, but imposes no further restrictions on what is actually entered.

Getting ready

First, we will prepare the form definition. Create a new file validation_form.php in the cook directory, with the following content:

<?php require_once($CFG->libdir.'/formslib.php'); class validation_form extends moodleform { function definition() { $mform =& $this->_form; $mform->addElement('text', 'mytext1', 'Required'); $mform->addElement('submit', 'submitbutton', 'Submit'); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required