28
Moodle Quick Forms Moodle - lib/formslib

Moodle Quick Form

Embed Size (px)

DESCRIPTION

Slides describe the basic Moodle_quickform library, how to use and create and validate the form in Moodle

Citation preview

Page 1: Moodle Quick Form

Moodle Quick Forms

Moodle - lib/formslib

Page 2: Moodle Quick Form

Contents

Introduction to Moodle_QuickForm

Usage

Definition

Validation

Page 3: Moodle Quick Form

Introduction to Moodle_QuickForm

Accessibility improvements : Input fields labeled Tableless layout.

Process form data securely done through required_param, optional_param, etc

Process submitted data themselves and

Check the submitted data

Page 4: Moodle Quick Form

Cont…

Form validation with custom validation() or addRule QuickForms validation

Feedback to the user about required input and errors in user input.

Facility to add Moodle help buttons to forms.

upload_manager processes uploaded files securely.

Page 5: Moodle Quick Form

Cont…

Custom Moodle specific and non specific form elements

Specific elements modgrade, modvisible, modgroupmode and choosecoursefile, format

Non specific elements htmleditor, dateselector, datetimeselector, selectyesno

Lots of modification to regular elements.

Easy to define your own custom elements.

Page 6: Moodle Quick Form

Usage

Page 7: Moodle Quick Form

Definition addElement() closeHeaderBefore() setHelpButton() addGroup() setType() setDefault() addRule() add_action_buttons() Other

Page 8: Moodle Quick Form

definition()

Page 9: Moodle Quick Form

addElement()

Syntax : $form->addElement ( type, name, label, …)

Example :

$form->addElement ( 'header', 'General', get_string('generalheader'));

$form->addElement ( 'checkbox', 'mycheck', get_string('mycheckbutton'), array('group' => 1), array(0,1));

$form->addElement ( 'date_time_selector', 'd_t', get_string('d_t'));

$mform->addElement ('choosecoursefile', 'mediafile', 'Course file upload', array('courseid'=>null, 'height'=>500, 'width'=>750, 'options'=>'none')

Page 10: Moodle Quick Form

closeHeaderBefore()

Syntax : $form->closeHeaderBefore ( element_name )

Page 11: Moodle Quick Form

setHelpButton()

Syntax : $form->setHelpButton ( element_name,

array( help_filename, title, module), supress_check )

$mform->setHelpButton('questionsperpage',

array( 'questionsperpage', get_string( 'questionsperpage', 'quiz' ), 'quiz' ) )

Example :

Page 12: Moodle Quick Form

addGroup()

$buttonarray = array();

$buttonarray[] =& $mform->createElement(‘submit’, ‘save', get_string('save'));

$buttonarray[] =& $mform->createElement(‘submit’, 'cancel', get_string('cancel'));

$mform->addGroup($buttonarray, 'buttonar', ‘’, array(' '), false);

$buttonarray = array();

$buttonarray[] =& $mform->createElement(‘submit’, ‘save', get_string('save'));

$buttonarray[] =& $mform->createElement(‘submit’, 'cancel', get_string('cancel'));

$mform->addGroup($buttonarray, 'buttonar', ‘’, array(' '), false);

A 'group' in formslib is just a group of elements that will have a

label and will be included on one line.

Def :

Page 13: Moodle Quick Form

setType()

Some elements other than checkbox, radio, select clean themselves but other requires cleaning

PARAM_* types are used to specify how a submitted variable should be cleaned.

Syntax : $form->setType ( element_name, type)

Example :

Page 14: Moodle Quick Form

Most Commonly Used PARAM_* Types :

PARAM_CLEAN Deprecated

PARAM_TEXT Cleaning the plain text data. Strips all html tags.

PARAM_NOTAGS

1. cleaning data that is expected to be plain text.

2. It will strip *all* html type tags. It will still *not* let tags for

multilang support through.

3. This should be used for instance for email addresses where no

multilang support is appropriate.

PARAM_RAW

1. no cleaning whatsoever (Data coming from HTML editor)

2. Data from the editor is later cleaned before display using

format_text() function

PARAM_INT Used for Integers

PARAM_ACTIONalias of PARAM_ALPHA and is used for hidden fields specifying form

actions

Page 15: Moodle Quick Form

setDefault()

Syntax : $form->setDefault ( element_name, default value)

Example :

We set the defaults for the form in definition().

This default is what is used if no data is loaded into the form with set_data();

Page 16: Moodle Quick Form

addRule()

$mform->addRule ( ‘elementname’, ‘error_message’, ‘ruletype’, ‘external_ruledata’, ‘server/client’, ‘reset(bool)’ , ‘force(bool)’ )

Syntax :

Common Rule Types

required maxlength minlength rangelength

email regex lettersonly

alphanumeric numeric nopunctuation

nonzero callback compare

Page 17: Moodle Quick Form

Cont…

Page 18: Moodle Quick Form

add_action_buttons

$this->add_action_buttons ($cancel = true, $submitlabel=null)

Syntax :

• param: boolean $cancel whether to show cancel button, default true

• param: string $submitlabel label for submit button, defaults to get_string('savechanges')

• Note: Should be always present at the end of all the form elements.

Page 19: Moodle Quick Form

Other

setMultiple(bool) - used with select element

save_files() After submitting a form, all the form information is get through get_data() function.

save_files() function used for that FORM saves all the files uploaded to specified directory.

Page 20: Moodle Quick Form

Cont… registerRule()

$mform->registerRule ($name , $type , $data1 , $data2 )

$name = name of function / name of validation rule

$type = 'regex' or 'callback' ('function' is also kept for backward compatibility)

$data1 = Name of function, regular expression, classname , callback function needs to return true or false

$data2 = Object parent of above function, name of the file

Where,

Page 21: Moodle Quick Form

Cont…

disabledIf()$mform->disabledIf ( $elementName, $dependentOn,

$condition = 'notchecked', $value=null)

elementname – Name of the group or name of the element.

dependentOn –

is the actual name of the element as it will appear in html. Can be different for addGroup elements. You typically make the depedentOn a checkbox or select box.

Page 22: Moodle Quick Form

disabledif() Cont…

$condition will be 'notchecked', 'checked', 'noitemselected', 'eq' or, if it is anything else, we test for 'neq'.

If $condition is 'eq' or 'neq' then we check the value of the dependentOn field and check for equality (==) or nonequality (!=) in js

If $condition is 'checked' or 'notchecked' then we check to see if a checkbox is checked or not.

If $condition is 'noitemselected' then we check to see whether nothing is selected in a dropdown list.

Page 23: Moodle Quick Form

Validation

Two ways : MoodleQuickForm::addRule() moodleform::validation()

Page 24: Moodle Quick Form

validation()

Define a method validation on your moodleform child to make your own custom validation for the form.

This is done on the server 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.

Page 25: Moodle Quick Form

MoodleQuickForm::addRule()

Default error messages are defined in lang/{language}/form.php

Page 26: Moodle Quick Form

moodleform::validation() $data = contains all the

values set on submitting

the value.

$data is array which

contains value in following

form:

$data[‘elementname’]

Example:

$s = $data[‘shortname’];

$error = is an array which stores error, while validating the submitted data.

Syntax: $error[‘elementname’]

Where, elementname should be same as name given while creating form.

Page 27: Moodle Quick Form

Cont…

$files in validation function is extra parameter for validating

the files which are to be uploaded to system

Page 28: Moodle Quick Form

References

http://docs.moodle.org/en/Category:Formslibhttp://www.moodle.org/http://pear.php.net/