Jump to content

auForm: Difference between revisions

From auWiki
add to categories PHP and PHP:auLib
m PHP Class auForm moved to PHP:Class:auForm: change to naming scheme
(No difference)

Revision as of 12:18, 5 February 2007

The auForm class is part of the auLib package. Its purpose is to build form objects that can be used to output HTML and perform simple user input validation. It also attempts to fool spammers using a couple methods which should be undetectable by actual people. The included auForm.css should be integrated into sites using auForm in order to make sure the spam trap fields do not get seen by actual people.

Usage

Create a form using new auForm(), then build it using AddFieldSet(), AddData(), AddText(), AddField(), AddSelect(), AddMultiSelect(), and AddButtons(). Check Submitted() to see if the form was submitted, or to find out which submit button was used. Use CheckInput() to automatically validate fields. Use WriteHTML() to write out HTML code for the form. If any errors were found during CheckInput(), they will display above the problematic field.

Constants

Field Types

  • _AU_FORM_FIELD_NORMAL
  • _AU_FORM_FIELD_NUMERIC
  • _AU_FORM_FIELD_INTEGER
  • _AU_FORM_FIELD_DATETIME
  • _AU_FORM_FIELD_MULTILINE
  • _AU_FORM_FIELD_BBCODE
  • _AU_FORM_FIELD_PASSWORD
  • _AU_FORM_FIELD_CHECKBOX
  • _AU_FORM_FIELD_FILE

Select Types

  • _AU_FORM_SELECT_DROPDOWN
  • _AU_FORM_SELECT_RADIO

MultiSelect Types

  • _AU_FORM_MULTI_LIST
  • _AU_FORM_MULTI_CHECK

Public Functions

new auForm

new auForm($id, $action, $method)

Creates a new form object.

  • $id = CSS id of the form element. Also the value of formid data field.
  • $action = Script to send form data to. Defaults to the current page.
  • $method = How to send form data. Defaults to HTTP POST ('post') -- can be overridden to GET ('get').

AddFieldSet

AddFieldSet($fieldset)

Adds a fieldset to the form.

  • $fieldset = An auFormFieldSet object, with all of its elements already added.

AddData

AddData($name, $value)

Adds data to the form that will not display but will be submitted.

  • $name = Name to submit the data as.
  • $value = Value of the data.

AddText

AddText($title, $text)

Adds text to the form that will display but will not be submitted.

  • $title = Title of the text, which displays like a field label.
  • $text = Text to display.

AddHTML

AddHTML($title, $html)

Adds html to the form that will display but will not be submitted. This can also be used to add custom fields, but they will have to be validated manually.

  • $title = Title of the text, which displays like a field label.
  • $html = The text to display.

AddField

AddField($name, $label, $tooltip, $required, $default, $type, $width, $maxlength)

Adds a field to the form.

  • $name = Name to submit the field as.
  • $label = Label to display with the field. Default is blank.
  • $tooltip = Tooltip text to display when the mouse is over the label. For checkbox fields, the tooltip displays next to the checkbox itself. Default is none.
  • $required = Whether a value is required in this field. Default is not required. Ignored for some field types.
  • $default = Default value for this field. Default is blank / unchecked.
  • $type = Type of field. Should be an _AU_FORM_FIELD_* value.
  • $width = Width of the field in characters. Default is browser default.
  • $maxlength = The maximum number of characters the field can accept. Default is unlimited. Ignored for checkboxes and file fields.

AddSelect

AddSelect($name, $label, $tooltip, $values, $default, $type)

Adds a one-value selection field to the form.

  • $name = Name to submit the field as.
  • $label = Label to display with the field. Default is blank.
  • $tooltip = Tooltip text to display when the mouse is over the label. Default is none.
  • $values = Associative array of possible values (value => display text). If values and display text are all identical, a normal array can be passed through auFormSelect::ArrayIndex($array) to get it into the appropriate format. Default is no possible values.
  • $default = Default value for this field. Default is browser default, which is usually the first of the possible values.
  • $type = Type of field -- set to _AU_FORM_SELECT_RADIO to use radio buttons. Default is _AU_FORM_SELECT_DROPDOWN for a dropdown selection.

AddMultiSelect

AddMultiSelect($name, $label, $tooltip, $values, $default, $type)

Adds a multiple-value selection field to the form.

  • $name = Name to submit the field as.
  • $label = Label to display with the field. Default is blank.
  • $tooltip = Tooltip text to display when the mouse is over the label. Default is none.
  • $values = Associative array of possible values (value => display text). If values and display text are all identical, a normal array can be passed through auFormSelect::ArrayIndex($array) to get it into the appropriate format. Default is no possible values.
  • $default = Array of default values for this field. Default is for no values to be selected.
  • $type = Type of field -- set to _AU_FORM_MULTI_LIST to use list box. Default is _AU_FORM_MULTI_CHECK for checkboxes.

AddButtons

AddButtons($text, $tooltip, $name)

Adds submit buttons to the form.

  • $text = Text to display on the button. Pass an array to have multiple buttons on one line.
  • $tooltip = Tooltip text to display when the mouse is over the button. Pass an array parallel to $text if $text was an array. Default is none.
  • $name = Name that should be used for the button. Must be the same for all buttons. Default is 'submit'.

WriteHTML

WriteHTML($trusted, $indent)

Writes out the form in HTML format. If any fields have failed their check, error messages will be displayed above the field.

  • $trusted = Whether the submitter is trusted. This essentially disables anti-spam measures and is intended to be set true for logged-in users. Default is not trusted, which means anti-spam measures are enabled.
  • $indent = A string of white space to help line the form up with the rest of the HTML. Default is six spaces.

Submitted

Submitted()

Checks if the form has been submitted.

  • @return = Text of the button used to submit the form, or false if the form was not submitted.

CheckInput

CheckInput($trusted)

Checks all the form fields for invalid input.

  • $trusted = Whether the submitter is trusted. This essentially disables checking anti-spam measures and is intended to be set true for logged-in users. This should match the value passed to WriteHTML(). Default is not trusted, which means anti-spam measures are checked.
  • @return = True if all fields are valid.