really simple PHP

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
Summary
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.

gapiForms

A class of form tools

Note

This is different from gapiForm (note the ‘s’ at the end)- this can’t initialize a form element, it only contains form related functions

Summary
Validates the information based on the type

Functions

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

$typeType of validation
$inputInformation to be validated (Or if custom error, Custom error result)
$valValue to be validated against (Optional)

Validation Types

reqInput is required
minThe string must be at least $val characters
maxThe string must be less than $val characters
minvalThe input must equal at least $val
maxvalThe input must equal less than $val
minwordThe input must have at least $val words
maxwordThe input must have less than $val words
numThe input must be numeric
emailThe input must be a valid email address
phoneThe input must be a valid phone number
phoneaThe input must be a valid phone number with area code
urlThe 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 Messageif an error is found
false(boolean) if there is no error

gapiElement

An extendable class to add elements to the page

Parameters

idThe id / name of the element
optionsAn array of options (see below)

Options

valueDefault value of the element
attributesAttributes of the element
validateThings to validate
autoValidateValidate without waiting for a submit
Summary
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

Functions

get

function get($option)

Get an option’s value

Parameters

optionThe option to get

Options

idThe id/name of the element
typeThe type of the element
valueThe value of the element
errorThe first error to be returned
errorsAn array of all errors for the element
validateAn array of all things to be validated others...- Anything can be stored as an option (ie ‘label’)

Example

echo $element->get('value');

set

function set($option,
$value)

Set an option’s value

Parameters

optionThe option to set (see get() for options)
valueNew value of the option

Example

$element->set('value', 'New Value!');

setAttribute

function setAttribute($attribute,
$value)

Set an attribute of the element

Parameters

attributeThe option to set
valueNew value of the option

Example

$element->setAttribute('class', 'testClass');

getAttribute

function getAttribute($attribute)

Get an attributes value

Parameters

attributeThe option to get

Example

echo $element->getAttribute('class');

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

$isolateIf true, won’t update the error information in the element (optional, defaults to false)
$validateArray 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

idThe id / name of the form
elementsElements of the form
optionsAn 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

methodMethod of the form, default is “post”
actionAction of the form (a URL to submit to)
attributesAttributes of the element
validateShould the form validate its elements?  (boolean, default is true)
Summary
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

Functions

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

getElements

function getElements( )

Returns an array of elements in the form

Returns

array()Array of elements if possible
falseIf no elements available

save

function save( )

Saves the form in a mySQL database

Parameters

$optionsAn array of options (see below); optional

Returns

array()Array of elements if possible
falseIf 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.

function validateError($type,  
$input,  
$val =  "")
Validates the information based on the type
function get($option)
Get an option’s value
function set($option,
$value)
Set an option’s value
function setAttribute($attribute,
$value)
Set an attribute of the element
function getAttribute($attribute)
Get an attributes value
function validate($isolate =  false,
$validate =  false)
Validate an element
function toString($return = "both")
Outputs a HTML string representation of the element
function isSubmitted( )
Check if the form was submitted
function isError( )
Check if theres an error
function getElements( )
Returns an array of elements in the form
function save( )
Saves the form in a mySQL database
An extendable class to add elements to the page