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'); } } ?>
This is a very simple form definition including one text box, mytext1
, and a submit button, submitbutton
.
Next, prepare the form logic in this example validation.php
, with the following content:
<?php require_once...