Form
Form is a complete class which handles many of the tedious things about creating
and validating html forms.
Form handles creation and validation of all regular form elements. It provides clientside
validation via javascript and serverside with build-in and user-defined regular expressions.
Goals
The purpose of the Form project is to provide:
- simple but flexible form creation and validation
- a clean API
Source
Form.phps
Form_example1.php
Form_example2.php
Changes
- 0.9.1
Lots of bugfixes done.
- 0.5.1
Few bugfixes
Added validation against other fields
Added email-adress validation
Cleaned up predefined regexs
- 0.5.0
Bugfixes and cleaning up the code a little
- 0.0.1
First version.
ToDo
- Allow validation of optional fields, if filled
feedback appreciated: jonas[a]delfs[.]dk
API reference
Variables
- array $formAttr
Assosiative array of form-tag attributes.
- int $tabOffset
Integer of number of tabs to indent HTML-code.
- array $tableAttr
Assosiative array of table-tag attributes.
- array $elements
Multidimensional assosiative array of form elements.
- array $values
Element values.
- array $submitVars
Assosiative array of submitted element values.
- array $errors
Assosiative array of error messages to be shown.
- array $reservedKeys
Array of reserved array keys used to sort out array elements that is not going to be
parsed using _getAttrString()
- string $stdErrMsg
Standard error message for required elements.
- bool $showRequired
Boolean that defines whether asterix's (*) should be shown for required elements.
- array $matches
Multidimensional assosiative array of standard regex's (and appropriate error message)
needed to validate form elements. See source for full list.
Public methods
- void addElement(mixed mixed [, string type [, string name [, string value [, array attr [, string validate [, string errmsg]]]]]])
Adds an element-array to the form.
- void addElementGroup(mixed mixed, array elements)
Adds a group of elements to the form. mixed kan be one big array of elements or the caption/label for it.
- string buildElement(mixed mixed [, string name [, string value [, array attr]]])
Builds HTML-part of an element. mixed can be one big array of the element or the element type.
- string buildElementGroup(array element)
Builds HTML-part of an element group.
- void display(void)
Prints out the toHtml() method.
- void Form([array formAttr [, int tabOffset [, array tableAttr [, array elements]]]])
Constructor. Sets class-global variables bsed on arguments given. Generates form name if none given.
- array getElement(mixed mixed [, string type [, string name [, string value [, array attr [, string validate [, string errmsg]]]]]])
Creates and returns assosiative array based on arguments
- array getElementGroup(mixed mixed, array elements)
Creates and returns multidimensional assosiative array of an element group.
- string getJs(void)
Creates and returns javascript validation code.
- mixed getSubmitted([string name])
If given an argument, the submitted value for element name will be returned.
Else, assosiative array of submitted values.
- void insertValue(mixed mixed [, string value])
If given 2 arguments it sets the element with name mixed the value value.
- bool isSubmitted(void)
Returns flag of form submitted-status.
- bool isValid(mixed mixed, array required)
static method to search a given assosiative array for keys.
- string toHtml(void)
Generates HTML output
- bool validate(void)
Validates elements - when negativ errmsg is shown. Returns boolean of form validation.
Private methods
PHP's doesn't handle private methods at this time, but at best PEAR manner I've prefixed methods that are
supposed to be private with an underscore.
- string _getAttrString(array attributes)
Creates and returns attribute-string based on assosiative array.
- string _getErrmsg(array element)
Returns errormessage for the given element, depending on validation-requirements.
- string _getIndent(void)
Return string of tabs using to indent HTML-code.
- void _insertSubmitted(array element, array checked)
Sets submitted value for element element as element's value.
- void _insertSubmitVars(void)
Inserts all submitted values into form.
- void _setInsertedValue(array element)
Sets element's value to submitted value, unless it's of the type 'radio' og 'select'.
- void _validateElement(array element, int return)
Validates element element.
All feedback is appreciated at jonas[a]delfs[.]dk