Moodle Quick Forms Moodle - lib/formslib
Contents <ul><li>Introduction to Moodle_QuickForm </li></ul><ul><li>Usage </li></ul><ul><li>Definition </li></ul><ul><li>V...
Introduction to Moodle_QuickForm <ul><li>Accessibility improvements :  </li></ul><ul><ul><li>Input fields labeled  </li></...
Cont… <ul><li>Form validation with custom validation() or  addRule  QuickForms validation  </li></ul><ul><li>Feedback to t...
Cont… <ul><li>Custom Moodle specific and non specific form elements </li></ul><ul><ul><li>Specific elements  modgrade, mod...
Usage
Definition <ul><ul><li>addElement()  </li></ul></ul><ul><ul><li>closeHeaderBefore() </li></ul></ul><ul><ul><li>setHelpButt...
definition()
addElement() Syntax : $form->addElement  ( type, name, label, …) Example : <ul><li>$form->addElement  ( ' header ', ' Gene...
closeHeaderBefore() Syntax : $form->closeHeaderBefore ( element_name )
setHelpButton() Syntax : $form->setHelpButton ( element_name,  array( help_filename, title, module), supress_check ) $mfor...
addGroup() <ul><ul><li>$buttonarray  = array();  </li></ul></ul><ul><ul><li>$buttonarray[]  </li></ul></ul><ul><ul><li>=& ...
setType() <ul><li>Some elements other than checkbox, radio, select clean themselves but other requires cleaning </li></ul>...
Most Commonly Used PARAM_* Types : alias of PARAM_ALPHA and is used for hidden fields specifying form actions  PARAM_ACTIO...
setDefault() <ul><li>We set the defaults for the form in definition(). </li></ul><ul><li>This default is what is used if n...
addRule() $mform->addRule (  ‘elementname’,  ‘ error_message’,  ‘ ruletype’,  ‘ external_ruledata’,  ‘ server/client’,  ‘ ...
Cont…
add_action_buttons $this->add_action_buttons ( $cancel  = true,  $ submitlabel =null) Syntax : <ul><li>param:   boolean $c...
Other <ul><ul><li>setMultiple(bool)  - used with select element   </li></ul></ul><ul><ul><li>save_files() </li></ul></ul>A...
Cont… <ul><ul><li>registerRule() </li></ul></ul>$mform->registerRule ($name , $type , $data1 , $data2 ) $name =  name of f...
Cont… <ul><ul><li>disabledIf() </li></ul></ul>$mform->disabledIf ( $elementName,  $dependentOn,    $condition = 'notchecke...
disabledif() Cont… <ul><li>$condition will be  'notchecked', 'checked', 'noitemselected', 'eq'  or, if it is anything else...
Validation <ul><li>Two ways :   </li></ul><ul><li>MoodleQuickForm::addRule()  </li></ul><ul><li>moodleform::validation()  ...
validation() <ul><li>Define a method validation on your moodleform child to make your own custom validation for the form. ...
MoodleQuickForm::addRule()  Default error messages are defined in lang/{language}/form.php
moodleform::validation()  $data = contains all the  values set on submitting the value.  $data is array which  contains va...
Cont… $files in validation function is extra parameter for validating  the files which are to be uploaded to system
References http://docs.moodle.org/en/Category:Formslib http://www.moodle.org/ http://pear.php.net/
Upcoming SlideShare
Loading in …5
×

Moodle Quick Forms

21,221 views

Published on

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

Published in: Technology

Moodle Quick Forms

  1. 1. Moodle Quick Forms Moodle - lib/formslib
  2. 2. Contents <ul><li>Introduction to Moodle_QuickForm </li></ul><ul><li>Usage </li></ul><ul><li>Definition </li></ul><ul><li>Validation </li></ul>
  3. 3. Introduction to Moodle_QuickForm <ul><li>Accessibility improvements : </li></ul><ul><ul><li>Input fields labeled </li></ul></ul><ul><ul><li>Tableless layout. </li></ul></ul><ul><li>Process form data securely done through required_param, optional_param, etc </li></ul><ul><li>Process submitted data themselves and </li></ul><ul><li>Check the submitted data </li></ul>
  4. 4. Cont… <ul><li>Form validation with custom validation() or addRule QuickForms validation </li></ul><ul><li>Feedback to the user about required input and errors in user input. </li></ul><ul><li>Facility to add Moodle help buttons to forms. </li></ul><ul><li>upload_manager processes uploaded files securely. </li></ul>
  5. 5. Cont… <ul><li>Custom Moodle specific and non specific form elements </li></ul><ul><ul><li>Specific elements modgrade, modvisible, modgroupmode and choosecoursefile, format </li></ul></ul><ul><ul><li>Non specific elements htmleditor, dateselector, datetimeselector, selectyesno </li></ul></ul><ul><ul><li>Lots of modification to regular elements. </li></ul></ul><ul><ul><li>Easy to define your own custom elements. </li></ul></ul>
  6. 6. Usage
  7. 7. Definition <ul><ul><li>addElement() </li></ul></ul><ul><ul><li>closeHeaderBefore() </li></ul></ul><ul><ul><li>setHelpButton() </li></ul></ul><ul><ul><li>addGroup() </li></ul></ul><ul><ul><li>setType() </li></ul></ul><ul><ul><li>setDefault() </li></ul></ul><ul><ul><li>addRule() </li></ul></ul><ul><ul><li>add_action_buttons() </li></ul></ul><ul><ul><li>Other </li></ul></ul>
  8. 8. definition()
  9. 9. addElement() Syntax : $form->addElement ( type, name, label, …) Example : <ul><li>$form->addElement ( ' header ', ' General ', get_string(' generalheader ')); </li></ul><ul><li>$form->addElement ( ' checkbox ', ' mycheck ', get_string(' mycheckbutton '), array( 'group' => 1 ), array( 0,1 )); </li></ul><ul><li>$form->addElement ( ' date_time_selector ', ' d_t ', get_string(' d_t ')); </li></ul><ul><li>$mform->addElement (' choosecoursefile ', ' mediafile ', 'Course file upload ', array(' courseid '=> null , ' height '=> 500 , ' width '=> 750 , ' options '=>' none ') </li></ul>
  10. 10. closeHeaderBefore() Syntax : $form->closeHeaderBefore ( element_name )
  11. 11. setHelpButton() Syntax : $form->setHelpButton ( element_name, array( help_filename, title, module), supress_check ) $mform->setHelpButton(' questionsperpage ', array( ' questionsperpage ', get_string( ' questionsperpage ', ' quiz ' ), ' quiz ' ) ) Example :
  12. 12. addGroup() <ul><ul><li>$buttonarray = array(); </li></ul></ul><ul><ul><li>$buttonarray[] </li></ul></ul><ul><ul><li>=& $mform->createElement(‘ submit ’, ‘ save ', get_string(' save ')); </li></ul></ul><ul><ul><li>$buttonarray[] </li></ul></ul><ul><ul><li>=& $mform->createElement(‘ submit ’, ' cancel ', get_string(' cancel ')); </li></ul></ul><ul><ul><li>$mform->addGroup( $buttonarray , ' buttonar ', ‘’, array(' '), false ); </li></ul></ul>A 'group' in formslib is just a group of elements that will have a label and will be included on one line. Def :
  13. 13. setType() <ul><li>Some elements other than checkbox, radio, select clean themselves but other requires cleaning </li></ul><ul><li>PARAM_* types are used to specify how a submitted variable should be cleaned. </li></ul>Syntax : $form->setType ( element_name, type) Example :
  14. 14. Most Commonly Used PARAM_* Types : alias of PARAM_ALPHA and is used for hidden fields specifying form actions PARAM_ACTION Used for Integers PARAM_INT <ul><li>no cleaning whatsoever (Data coming from HTML editor) </li></ul><ul><li>Data from the editor is later cleaned before display using format_text() function </li></ul>PARAM_RAW <ul><li>cleaning data that is expected to be plain text. </li></ul><ul><li>It will strip *all* html type tags. It will still *not* let tags for multilang support through. </li></ul><ul><li>This should be used for instance for email addresses where no multilang support is appropriate. </li></ul>PARAM_NOTAGS Cleaning the plain text data. Strips all html tags. PARAM_TEXT Deprecated PARAM_CLEAN
  15. 15. setDefault() <ul><li>We set the defaults for the form in definition(). </li></ul><ul><li>This default is what is used if no data is loaded into the form with set_data(); </li></ul>Syntax : $form->setDefault ( element_name, default value) Example :
  16. 16. addRule() $mform->addRule ( ‘elementname’, ‘ error_message’, ‘ ruletype’, ‘ external_ruledata’, ‘ server/client’, ‘ reset(bool)’ , ‘ force(bool)’ ) Syntax : Common Rule Types nonzero callback compare alphanumeric numeric nopunctuation email regex lettersonly required maxlength minlength rangelength
  17. 17. Cont…
  18. 18. add_action_buttons $this->add_action_buttons ( $cancel = true, $ submitlabel =null) Syntax : <ul><li>param: boolean $cancel whether to show cancel button, default true </li></ul><ul><li>param: string $submitlabel label for submit button, defaults to get_string('savechanges') </li></ul><ul><li>Note: Should be always present at the end of all the form elements. </li></ul>
  19. 19. Other <ul><ul><li>setMultiple(bool) - used with select element </li></ul></ul><ul><ul><li>save_files() </li></ul></ul>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.
  20. 20. Cont… <ul><ul><li>registerRule() </li></ul></ul>$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,
  21. 21. Cont… <ul><ul><li>disabledIf() </li></ul></ul>$mform->disabledIf ( $elementName, $dependentOn, $condition = 'notchecked', $value=null) <ul><ul><li>elementname – Name of the group or name of the element. </li></ul></ul><ul><ul><li>dependentOn – </li></ul></ul><ul><ul><ul><li>is the actual name of the element as it will appear in html. </li></ul></ul></ul><ul><ul><ul><li>Can be different for addGroup elements. </li></ul></ul></ul><ul><ul><ul><li>You typically make the depedentOn a checkbox or select box. </li></ul></ul></ul>
  22. 22. disabledif() Cont… <ul><li>$condition will be 'notchecked', 'checked', 'noitemselected', 'eq' or, if it is anything else, we test for 'neq'. </li></ul><ul><ul><li>If $condition is 'eq' or 'neq' then we check the value of the dependentOn field and check for equality (==) or nonequality (!=) in js </li></ul></ul><ul><ul><li>If $condition is 'checked' or 'notchecked' then we check to see if a checkbox is checked or not. </li></ul></ul><ul><ul><li>If $condition is 'noitemselected' then we check to see whether nothing is selected in a dropdown list. </li></ul></ul>
  23. 23. Validation <ul><li>Two ways : </li></ul><ul><li>MoodleQuickForm::addRule() </li></ul><ul><li>moodleform::validation() </li></ul>
  24. 24. validation() <ul><li>Define a method validation on your moodleform child to make your own custom validation for the form. </li></ul><ul><li>This is done on the server side. And data_submitted will return null until the function returns no errors. </li></ul><ul><li>You return an array of errors if there is any error or an empty array if there are no errors. </li></ul>
  25. 25. MoodleQuickForm::addRule() Default error messages are defined in lang/{language}/form.php
  26. 26. 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.
  27. 27. Cont… $files in validation function is extra parameter for validating the files which are to be uploaded to system
  28. 28. References http://docs.moodle.org/en/Category:Formslib http://www.moodle.org/ http://pear.php.net/

×