gapi_forms.inc
Form functions
License
MIT-style license.
Version
v0.3
GAPI Copyright
copyright © 2007 Gregory Koberger http://www.gkoberger.net
ToDo
- Rename to rsPHP
- Accept arrays in validator
- Accept JSON everywhere arrays are accepted
Form functions | |
A class of form tools | |
Validates the information based on the type | |
An extendable class to add elements to the page | |
Get an option’s value | |
Set an option’s value | |
Set an attribute of the element | |
Get an attributes value | |
Validate an element | |
Outputs a HTML string representation of the element | |
A class to create a form, or utilize form related functions | |
Check if the form was submitted | |
Check if theres an error | |
Returns an array of elements in the form | |
Saves the form in a mySQL database | |
Extends gapiElement, has additional functionality specifically for a textbox. | |
Extends gapiElement, has additional functionality specifically for a submit button. | |
Extends gapiElement, has additional functionality specifically for a drop down menu choice. | |
Extends gapiElement, has additional functionality specifically for a hidden textbox. | |
Extends gapiElement, has additional functionality specifically for a textarea. | |
Extends gapiElement, has additional functionality specifically for a checkbox. |
validateError
function validateError( $type, $input, $val = "" )
Validates the information based on the type
Note
This function is named validateError rather than just validate in order to make the following statement sound more logical
if ( $myElement->validateError( 'req', $input ) )
echo "There was an error";
However, even though it’s not documented, you can use validateError() and validate() interchangeably without an issue
Parameters
| $type | Type of validation |
| $input | Information to be validated (Or if custom error, Custom error result) |
| $val | Value to be validated against (Optional) |
Validation Types
| req | Input is required |
| min | The string must be at least $val characters |
| max | The string must be less than $val characters |
| minval | The input must equal at least $val |
| maxval | The input must equal less than $val |
| minword | The input must have at least $val words |
| maxword | The input must have less than $val words |
| num | The input must be numeric |
| The input must be a valid email address | |
| phone | The input must be a valid phone number |
| phonea | The input must be a valid phone number with area code |
| url | The input must be a valid URL |
Example
$atLeastError = gapiForms::validateError('min', $input , 3);
$lessThanError = gapiForms::validateError('max', $input , 10);
if ( ! $atLeastError && ! $lessThanError )
echo "This is a valid input!";
elseif ( $atLeastError )
echo $atLeastError;
elseif ( $lessThanError )
echo $lessThanError;Example of Custom Validation (No Error)
$validate = true;
gapiForms::validateError('customValidation', $validate);
Example of Custom Validation (Error)
$validate = "Error message";
gapiForms::validateError('customValidation', $validate);
Returns
| Error Message | if an error is found |
| false | (boolean) if there is no error |
gapiElement
An extendable class to add elements to the page
Parameters
| id | The id / name of the element |
| options | An array of options (see below) |
Options
| value | Default value of the element |
| attributes | Attributes of the element |
| validate | Things to validate |
| autoValidate | Validate without waiting for a submit |
Get an option’s value | |
Set an option’s value | |
Set an attribute of the element | |
Get an attributes value | |
Validate an element | |
Outputs a HTML string representation of the element |
get
function get( $option )
Get an option’s value
Parameters
| option | The option to get |
Options
| id | The id/name of the element |
| type | The type of the element |
| value | The value of the element |
| error | The first error to be returned |
| errors | An array of all errors for the element |
| validate | An array of all things to be validated others...- Anything can be stored as an option (ie ‘label’) |
Example
echo $element->get('value');validate
function validate( $isolate = false, $validate = false )
Validate an element
Example
$textfield = new gapiTextBox( 'newelement',
array(
'value'=>'test',
'attributes'=> array(
'size'=>5
)
) );
echo $textfield->toString( );
// <input id="newelement" name="newelement" type="text" value="test" size="5" / >
Arguments
| $isolate | If true, won’t update the error information in the element (optional, defaults to false) |
| $validate | Array of things to validate (optional, defaults to stored validation preferences for this element) |
Returns
Array of errors (key is validation type, value is error message)
toString
function toString( $return = "both" )
Outputs a HTML string representation of the element
Example
$textfield = new gapiTextBox( 'newelement',
array(
'value'=>'test',
'attributes'=> array(
'size'=>5
)
) );
echo $textfield->toString( );
// <input id="newelement" name="newelement" type="text" value="test" size="5" / >
Arguments
| $return | ”start” or “end” (optional, defaults to “both”) |
Returns
HTML output
gapiForm
A class to create a form, or utilize form related functions
Extends gapiElement, has additional functionality specifically for form. Inherits all functions from gapiElement.
Parameters
| id | The id / name of the form |
| elements | Elements of the form |
| options | An array of options (see below) |
Notes
Elements must be placed inside the <form></form> tags manually, or they wont be submitted with the form.
Elements must be passed into the form class before their output is displayed.
Currently isSubmitted() is only true if some sort of information is passed along, which often doesn’t meet needs. To ensure that isSubmitted() is always true when the form is submitted, include a hidden field with a default value in your form.
Options
| method | Method of the form, default is “post” |
| action | Action of the form (a URL to submit to) |
| attributes | Attributes of the element |
| validate | Should the form validate its elements? (boolean, default is true) |
Check if the form was submitted | |
Check if theres an error | |
Returns an array of elements in the form | |
Saves the form in a mySQL database |
isSubmitted
function isSubmitted( )
Check if the form was submitted
Will only work if one of the elements passed into the form is submitted
Example
if( $myForm->isSubmitted( ) )
echo "Congrats! The form was submitted";
else
echo "Please fill out the form";
Returns
| true | (boolean) if the form was submitted |
| false | (boolean) if the form was not submitted |
isError
function isError( )
Check if theres an error
Example
if( $myForm->isSubmitted( ) )
{
if( $myForm->isError( ) )
{
echo "There was an error in your submitted form";
}
else
{
echo "Congrats! The form was submitted and there are no errors!";
}
}
else
{
echo "Please fill out the form";
}
Returns
| true | (boolean) if there is an error |
| false | (boolean) if there is no error |
save
function save( )
Saves the form in a mySQL database
Parameters
| $options | An array of options (see below); optional |
Returns
| array() | Array of elements if possible |
| false | If no elements available |
Returns
| true | (boolean) If the data was saved |
| Error Message (String) | If the data could not be saved |
Note
In regards to the return, to check if the save was successful, you need to use the identity operator (===).
if( $myForm->save() === true)
// Means the form was saved
if( $myForm->save() !== true )
// Means the form was NOT saved
if( $myForm->save() == true)
//This will always be true
gapiTextBox
Extends gapiElement, has additional functionality specifically for a textbox. Inherits all functions from gapiElement.
gapiSubmit
Extends gapiElement, has additional functionality specifically for a submit button. Inherits all functions from gapiElement.
gapiChoice
Extends gapiElement, has additional functionality specifically for a drop down menu choice. Inherits all functions from gapiElement.
The syntax for creating a choice is different from the other elements, though
Syntax
$choice = new gapiChoice( "Value", "Display Text", $options);
Produces
<option value="Value">Display Text</option>
Syntax (Value only)
(No options allowed)
$choice = new gapiChoice( "Value" );
Produces
<option value="Value">Value</option>
gapiHidden
Extends gapiElement, has additional functionality specifically for a hidden textbox. Inherits all functions from gapiElement.
gapiTextArea
Extends gapiElement, has additional functionality specifically for a textarea. Inherits all functions from gapiElement.
gapiCheckBox
Extends gapiElement, has additional functionality specifically for a checkbox. Inherits all functions from gapiElement.
Validates the information based on the type
function validateError( $type, $input, $val = "" )
Get an option’s value
function get( $option )
Set an option’s value
function set( $option, $value )
Set an attribute of the element
function setAttribute( $attribute, $value )
Get an attributes value
function getAttribute( $attribute )
Validate an element
function validate( $isolate = false, $validate = false )
Outputs a HTML string representation of the element
function toString( $return = "both" )
Check if the form was submitted
function isSubmitted( )
Check if theres an error
function isError( )
Returns an array of elements in the form
function getElements( )
Saves the form in a mySQL database
function save( )