Difference between revisions of "lib/formslib.php Validation"

Jump to: navigation, search
(moodleform::validation())
(MoodleQuickForm::addRule())
Line 15: Line 15:
 
</pre>
 
</pre>
  
There is good documentation for this on the [http://www.go-pear.org/manual/en/package.html.html-quickform.intro-validation.php PEAR web site]
+
There is good documentation for this on the [http://pear.php.net/manual/en/package.html.html-quickform.intro-validation.php PEAR web site]
  
 
In Moodle formslib you can set certain rules' error message to null and a default error message will automatically be used. The default messages are defined in lang/{language}/form.php. Here are the English definitions :
 
In Moodle formslib you can set certain rules' error message to null and a default error message will automatically be used. The default messages are defined in lang/{language}/form.php. Here are the English definitions :

Revision as of 19:38, 10 December 2007


There are two ways to add validation in the forms lib.

MoodleQuickForm::addRule()

You use this typically inside your moodleform::definition method. For example to set a field as required.

        $mform->addElement('text','shortname', get_string('shortname'),'maxlength="15" size="10"');
        $mform->setHelpButton('shortname', array('courseshortname', get_string('shortname')), true);
        $mform->setDefault('shortname', get_string('defaultcourseshortname'));
        $mform->addRule('shortname', null, 'required', null, 'client');
        $mform->setType('shortname', PARAM_MULTILANG);

There is good documentation for this on the PEAR web site

In Moodle formslib you can set certain rules' error message to null and a default error message will automatically be used. The default messages are defined in lang/{language}/form.php. Here are the English definitions :

$string['err_alphanumeric']='You must enter only letters or numbers here.';
$string['err_email']='You must enter a valid email address here.';
$string['err_lettersonly']='You must enter only letters here.';
$string['err_maxlength']='You must enter not more than $a->format characters here.';
$string['err_minlength']='You must enter at least $a->format characters here.';
$string['err_nopunctuation']='You must enter no punctuation characters here.';
$string['err_nonzero']='You must enter a number not starting with a 0 here.';
$string['err_numeric']='You must enter a number here.';
$string['err_rangelength']='You must enter between {$a->format[0]} and {$a->format[1]} characters here.';
$string['err_required']='You must supply a value here.';

moodleform::validation()

Define a method validation on your moodleform child to make your own custom validation for the form. This is done on the client side. And data_submitted will return null until the function returns no errors. You return an array of errors if there is any error or an empty array if there are no errors.

class course_edit_form extends moodleform {

    function definition() {
        blah blah;
    }


/// perform some extra moodle validation
    function validation($data) {
        $errors= array();
        if ($foundcourses = get_records('course', 'shortname', $data['shortname'])) {
            if (!empty($data['id'])) {
                unset($foundcourses[$data['id']]);
            }
            if (!empty($foundcourses)) {
                foreach ($foundcourses as $foundcourse) {
                    $foundcoursenames[] = $foundcourse->fullname;
                }
                $foundcoursenamestring = implode(',', $foundcoursenames);
                $errors['shortname']= get_string('shortnametaken', '', $foundcoursenamestring);
            }
        }

        if (empty($data['enrolenddisabled'])){
            if ($data['enrolenddate'] <= $data['enrolstartdate']){
                $errors['enroldateendgrp'] = get_string('enrolenddaterror');
            }
        }
        return $errors;
    }
}
?>

Moodle1.9 There is a new parameter $files in Moodle 1.9, it allows validation of uploaded files too.

    function validation($data, $files) {
        $errors = parrent::validate($data, $files);
        if (empty($files['userfile'])) {
            if (array_key_exists('url', $data)) {
                $errors['url'] = get_string('required');
            }
        }
        return $errors;
    }