[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/lib/ -> formslib.php (summary)

formslib.php - library of classes for creating forms in Moodle, based on PEAR QuickForms. To use formslib then you will want to create a new file purpose_form.php eg. edit_form.php and you want to name your class something like {modulename}_{purpose}_form. Your class will extend moodleform overriding abstract classes definition and optionally defintion_after_data and validation.

Author: Jamie Pratt
License: http://www.gnu.org/copyleft/gpl.html GNU Public License
Version: $Id: formslib.php,v 1.129.2.17 2008/10/13 19:38:49 skodak Exp $
File Size: 1891 lines (78 kb)
Included or required:0 times
Referenced: 13 times
Includes or requires: 0 files

Defines 3 classes

moodleform:: (23 methods):
  moodleform()
  focus()
  _process_submission()
  _validate_files()
  set_data()
  set_upload_manager()
  is_submitted()
  no_submit_button_pressed()
  is_validated()
  is_cancelled()
  get_data()
  get_submitted_data()
  save_files()
  get_new_filename()
  get_file_content()
  display()
  definition()
  definition_after_data()
  validation()
  repeat_elements()
  add_checkbox_controller()
  html_quickform_toggle_checkboxes()
  add_action_buttons()

MoodleQuickForm:: (32 methods):
  MoodleQuickForm()
  setAdvanced()
  setShowAdvanced()
  getShowAdvanced()
  accept()
  closeHeaderBefore()
  setType()
  setTypes()
  updateSubmission()
  getReqHTML()
  getAdvancedHTML()
  setDefault()
  setHelpButtons()
  setHelpButton()
  setConstant()
  exportValues()
  addRule()
  addGroupRule()
  getValidationScript()
  qf_errorHandler()
  validate_()
  validate_()
  _setDefaultRuleMessages()
  getLockOptionEndScript()
  _getElNamesRecursive()
  disabledIf()
  registerNoSubmitButton()
  isNoSubmitButton()
  _registerCancelButton()
  hardFreeze()
  hardFreezeAllVisibleExcept()
  isSubmitted()

MoodleQuickForm_Renderer:: (8 methods):
  MoodleQuickForm_Renderer()
  setAdvancedElements()
  startForm()
  startGroup()
  renderElement()
  finishForm()
  renderHeader()
  getStopFieldsetElements()

Defines 8 functions

  pear_handle_error()

Class: moodleform  - X-Ref

Moodle specific wrapper that separates quickforms syntax from moodle code. You won't directly
use this class you should write a class definition which extends this class or a more specific
subclass such a moodleform_mod for each form you want to display and/or process with formslib.

You will write your own definition() method which performs the form set up.
moodleform($action=null, $customdata=null, $method='post', $target='', $attributes=null, $editable=true)   X-Ref
The constructor function calls the abstract function definition() and it will then
process and clean and attempt to validate incoming data.

It will call your custom validate method to validate data and will also check any rules
you have specified in definition using addRule

The name of the form (id attribute of the form) is automatically generated depending on
the name you gave the class extending moodleform. You should call your class something
like

param: mixed $action the action attribute for the form. If empty defaults to auto detect the
param: array $customdata if your form defintion method needs access to data such as $course
param: string $method if you set this to anything other than 'post' then _GET and _POST will
param: string $target target frame for form submission. You will rarely use this. Don't use
param: mixed $attributes you can pass a string of html attributes here or an array.
return: moodleform

focus($name=NULL)   X-Ref
To autofocus on first form element or first element with error.

param: string $name if this is set then the focus is forced to a field with this name
return: string  javascript to select form element with first error or

_process_submission($method)   X-Ref
Internal method. Alters submitted data to be suitable for quickforms processing.
Must be called when the form is fully set up.


_validate_files(&$files)   X-Ref
Internal method. Validates all uploaded files.


set_data($default_values, $slashed=false)   X-Ref
Load in existing data as form defaults. Usually new entry defaults are stored directly in
form definition (new entry form); this function is used to load in data where values
already exist and data is being edited (edit entry form).

param: mixed $default_values object or array of default values
param: bool $slased true if magic quotes applied to data values

set_upload_manager($um=false)   X-Ref
Set custom upload manager.
Must be used BEFORE creating of file element!

param: object $um - custom upload manager

is_submitted()   X-Ref
Check that form was submitted. Does not check validity of submitted data.

return: bool true if form properly submitted

no_submit_button_pressed()   X-Ref
No description

is_validated()   X-Ref
Check that form data is valid.

return: bool true if form data valid

is_cancelled()   X-Ref
Return true if a cancel button has been pressed resulting in the form being submitted.

return: boolean true if a cancel button has been pressed

get_data($slashed=true)   X-Ref
Return submitted data if properly submitted or returns NULL if validation fails or
if there is no submitted data.

param: bool $slashed true means return data with addslashes applied
return: object submitted data; NULL if not valid or not submitted

get_submitted_data($slashed=true)   X-Ref
Return submitted data without validation or NULL if there is no submitted data.

param: bool $slashed true means return data with addslashes applied
return: object submitted data; NULL if not submitted

save_files($destination)   X-Ref
Save verified uploaded files into directory. Upload process can be customised from definition()
method by creating instance of upload manager and storing it in $this->_upload_form

param: string $destination where to store uploaded files
return: bool success

get_new_filename()   X-Ref
If we're only handling one file (if inputname was given in the constructor)
this will return the (possibly changed) filename of the file.

return: mixed false in case of failure, string if ok

get_file_content($elname)   X-Ref
Get content of uploaded file.

param: $element name of file upload element
return: mixed false in case of failure, string if ok

display()   X-Ref
Print html form.


definition()   X-Ref
Abstract method - always override!

If you need special handling of uploaded files, create instance of $this->_upload_manager here.

definition_after_data()   X-Ref
Dummy stub method - override if you need to setup the form depending on current
values. This method is called after definition(), data submission and set_data().
All form setup that is dependent on form values should go in here.


validation($data, $files)   X-Ref
Dummy stub method - override if you needed to perform some extra validation.
If there are errors return array of errors ("fieldname"=>"error message"),
otherwise true if ok.

Server side rules do not work for uploaded files, implement serverside rules here if needed.

param: array $data array of ("fieldname"=>value) of submitted data
param: array $files array of uploaded files "element_name"=>tmp_file_path
return: array of "element_name"=>"error_description" if there are errors,

repeat_elements($elementobjs, $repeats, $options, $repeathiddenname,$addfieldsname, $addfieldsno=5, $addstring=null, $addbuttoninside=false)   X-Ref
Method to add a repeating group of elements to a form.

param: array $elementobjs Array of elements or groups of elements that are to be repeated
param: integer $repeats no of times to repeat elements initially
param: array $options Array of options to apply to elements. Array keys are element names.
param: string $repeathiddenname name for hidden element storing no of repeats in this form
param: string $addfieldsname name for button to add more fields
param: int $addfieldsno how many fields to add at a time
param: string $addstring name of button, {no} is replaced by no of blanks that will be added.
param: boolean $addbuttoninside if true, don't call closeHeaderBefore($addfieldsname). Default false.
return: int no of repeats of element in this page

add_checkbox_controller($groupid, $buttontext, $attributes, $originalValue = 0)   X-Ref
Adds a link/button that controls the checked state of a group of checkboxes.

param: int    $groupid The id of the group of advcheckboxes this element controls
param: string $text The text of the link. Defaults to "select all/none"
param: array  $attributes associative array of HTML attributes
param: int    $originalValue The original general state of the checkboxes before the user first clicks this element

html_quickform_toggle_checkboxes(group)   X-Ref
No description

add_action_buttons($cancel = true, $submitlabel=null)   X-Ref
Use this method to a cancel and submit button to the end of your form. Pass a param of false
if you don't want a cancel button in your form. If you have a cancel button make sure you
check for it being pressed using is_cancelled() and redirecting if it is true before trying to
get data with get_data().

param: boolean $cancel whether to show cancel button, default true
param: string $submitlabel label for submit button, defaults to get_string('savechanges')

Class: MoodleQuickForm  - X-Ref

You never extend this class directly. The class methods of this class are available from
the private $this->_form property on moodleform and its children. You generally only
call methods on this class from within abstract methods that you override on moodleform such
as definition and definition_after_data

MoodleQuickForm($formName, $method, $action, $target='', $attributes=null)   X-Ref
Class constructor - same parameters as HTML_QuickForm_DHTMLRulesTableless

param: string      $formName          Form's name.
param: string      $method            (optional)Form's method defaults to 'POST'
param: mixed      $action             (optional)Form's action - string or moodle_url
param: string      $target            (optional)Form's target defaults to none
param: mixed       $attributes        (optional)Extra attributes for <form> tag
param: bool        $trackSubmit       (optional)Whether to track if the form was submitted by adding a special hidden field

setAdvanced($elementName, $advanced=true)   X-Ref
Use this method to indicate an element in a form is an advanced field. If items in a form
are marked as advanced then 'Hide/Show Advanced' buttons will automatically be displayed in the
form so the user can decide whether to display advanced form controls.

If you set a header element to advanced then all elements it contains will also be set as advanced.

param: string $elementName group or element name (not the element name of something inside a group).
param: boolean $advanced default true sets the element to advanced. False removes advanced mark.

setShowAdvanced($showadvancedNow = null)   X-Ref
Set whether to show advanced elements in the form on first displaying form. Default is not to
display advanced elements in the form until 'Show Advanced' is pressed.

You can get the last state of the form and possibly save it for this user by using
value 'mform_showadvanced_last' in submitted data.

param: boolean $showadvancedNow

getShowAdvanced()   X-Ref
No description

accept(&$renderer)   X-Ref
Accepts a renderer

param: HTML_QuickForm_Renderer  An HTML_QuickForm_Renderer object
return: void

closeHeaderBefore($elementName)   X-Ref
No description

setType($elementname, $paramtype)   X-Ref
Should be used for all elements of a form except for select, radio and checkboxes which
clean their own data.

param: string $elementname
param: integer $paramtype use the constants PARAM_*.

setTypes($paramtypes)   X-Ref
See description of setType above. This can be used to set several types at once.

param: array $paramtypes

updateSubmission($submission, $files)   X-Ref
No description

getReqHTML()   X-Ref
No description

getAdvancedHTML()   X-Ref
No description

setDefault($elementName, $defaultValue, $slashed=false)   X-Ref
Initializes a default form value. Used to specify the default for a new entry where
no data is loaded in using moodleform::set_data()

param: string   $elementname        element name
param: mixed    $values             values for that element name
param: bool     $slashed            the default value is slashed
return: void

setHelpButtons($buttons, $suppresscheck=false, $function='helpbutton')   X-Ref
Add an array of buttons to the form

param: array       $buttons          An associative array representing help button to attach to

setHelpButton($elementname, $button, $suppresscheck=false, $function='helpbutton')   X-Ref
Add a single button.

param: string $elementname name of the element to add the item to
param: array $button - arguments to pass to function $function
param: boolean $suppresscheck - whether to throw an error if the element
param: string $function - function to generate html from the arguments in $button

setConstant($elname, $value)   X-Ref
Set constant value not overriden by _POST or _GET
note: this does not work for complex names with [] :-(

param: string $elname name of element
param: mixed $value
return: void

exportValues($elementList= null, $addslashes=true)   X-Ref
No description

addRule($element, $message, $type, $format=null, $validation='server', $reset = false, $force = false)   X-Ref
Adds a validation rule for the given field

If the element is in fact a group, it will be considered as a whole.
To validate grouped elements as separated entities,
use addGroupRule instead of addRule.

param: string     $element       Form element name
param: string     $message       Message to display for invalid data
param: string     $type          Rule type, use getRegisteredRules() to get types
param: string     $format        (optional)Required for extra rule data
param: string     $validation    (optional)Where to perform validation: "server", "client"
param: boolean    $reset         Client-side validation: reset the form element to its original value if there is an error?
param: boolean    $force         Force the rule to be applied, even if the target form element does not exist

addGroupRule($group, $arg1, $type='', $format=null, $howmany=0, $validation = 'server', $reset = false)   X-Ref
Adds a validation rule for the given group of elements

Only groups with a name can be assigned a validation rule
Use addGroupRule when you need to validate elements inside the group.
Use addRule if you need to validate the group as a whole. In this case,
the same rule will be applied to all elements in the group.
Use addRule if you need to validate the group against a function.

param: string     $group         Form group name
param: mixed      $arg1          Array for multiple elements or error message string for one element
param: string     $type          (optional)Rule type use getRegisteredRules() to get types
param: string     $format        (optional)Required for extra rule data
param: int        $howmany       (optional)How many valid elements should be in the group
param: string     $validation    (optional)Where to perform validation: "server", "client"
param: bool       $reset         Client-side: whether to reset the element's value to its original state if validation failed.

getValidationScript()   X-Ref
Returns the client side validation script

The code here was copied from HTML_QuickForm_DHTMLRulesTableless who copied it from  HTML_QuickForm
and slightly modified to run rules per-element
Needed to override this because of an error with client side validation of grouped elements.

return: string    Javascript to perform validation, empty string if no 'client' rules were added

qf_errorHandler(element, _qfMsg)   X-Ref
No description

validate_(var value = \'\';var errFlag = new Array()   X-Ref
No description

validate_(if (skipClientValidation)   X-Ref
No description

_setDefaultRuleMessages()   X-Ref
No description

getLockOptionEndScript()   X-Ref
No description

_getElNamesRecursive($element)   X-Ref
No description

disabledIf($elementName, $dependentOn, $condition = 'notchecked', $value='1')   X-Ref
Adds a dependency for $elementName which will be disabled if $condition is met.
If $condition = 'notchecked' (default) then the condition is that the $dependentOn element
is not checked. If $condition = 'checked' then the condition is that the $dependentOn element
is checked. If $condition is something else (like "eq" for equals) then it is checked to see if the value
of the $dependentOn element is $condition (such as equal) to $value.

param: string $elementName the name of the element which will be disabled
param: string $dependentOn the name of the element whose state will be checked for
param: string $condition the condition to check
param: mixed $value used in conjunction with condition.

registerNoSubmitButton($buttonname)   X-Ref
No description

isNoSubmitButton($buttonname)   X-Ref
No description

_registerCancelButton($addfieldsname)   X-Ref
No description

hardFreeze($elementList=null)   X-Ref
Displays elements without HTML input tags.
This method is different to freeze() in that it makes sure no hidden
elements are included in the form.
Note: If you want to make sure the submitted value is ignored, please use setDefaults().

This function also removes all previously defined rules.

param: mixed   $elementList       array or string of element(s) to be frozen

hardFreezeAllVisibleExcept($elementList)   X-Ref
Hard freeze all elements in a form except those whose names are in $elementList or hidden elements in a form.

This function also removes all previously defined rules of elements it freezes.

param: array   $elementList       array or string of element(s) not to be frozen

isSubmitted()   X-Ref
Tells whether the form was already submitted

This is useful since the _submitFiles and _submitValues arrays
may be completely empty after the trackSubmit value is removed.

return: bool

Class: MoodleQuickForm_Renderer  - X-Ref

A renderer for MoodleQuickForm that only uses XHTML and CSS and no
table tags, extends PEAR class HTML_QuickForm_Renderer_Tableless

Stylesheet is part of standard theme and should be automatically included.

MoodleQuickForm_Renderer()   X-Ref
Whether to display advanced elements (on page load)


setAdvancedElements($elements)   X-Ref
No description

startForm(&$form)   X-Ref
What to do when starting the form

param: MoodleQuickForm $form

startGroup(&$group, $required, $error)   X-Ref
No description

renderElement(&$element, $required, $error)   X-Ref
No description

finishForm(&$form)   X-Ref
No description

renderHeader(&$header)   X-Ref
Called when visiting a header element

param: object     An HTML_QuickForm_header element being visited
return: void

getStopFieldsetElements()   X-Ref
No description

Functions
Functions that are not part of a class:

pear_handle_error($error)   X-Ref
Callback called when PEAR throws an error

param: PEAR_Error $error



Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7