Graphic Design ServicesandPortfoliobyPhillip DodsonofVisional Echoes
June 16, 2007 at 7:57 pm

FormElement Classes

» Filed Under » php

What does it do, exactly?

This FormElement object makes creating sticky forms in PHP rather easy.

I was looking for an easy way to make sticky forms in PHP without using clunky code chunks in the value attributes of my form elements. I’m sure there are numerous solutions for this, but hey, lets face it, it’s fun to learn new things! And I saw this as a perfect way to get my feet wet in Object Oriented Programming.

So after some work, here we have some classes that do just what I wanted. They create HTML input elements inside of a form while making these elements sticky, in the event of a validation error (or any other reason to post back the form values) occurs.

Download

Download the version 0.6 classes .zip file here. This version only contains classes for text input. I will post the rest as soon as they are finished.

Changelog

v.0.6
8/26/2007

  • Re-wrote the classes due to a logic error causing stripslashes and htmlentities from working correctly
  • Changed the order of arguments after using these classes in live sites. The original order of importance wasn’t correct. The prototype has changed due to this alteration.
  • Stopped using htmlentities to filter output to the form element. In place of htmlentities, I am now using functions from Prolifique. Code snippet from http://www.prolifique.com/entities.php.txt. This has been integrated into the FormXHTMLRenderer class.
  • Added “password” as an input type.

v.0.55
7/9/2007

  • Removed the FormElementLabel object and combined its functionality into the FormElement superclass. Now things arent near as complicated.
  • Removed the tag_before and tag_after attributes and combined them into a tag_between attribute. This is the only necessary tag thats needed. To emulate the old tag_before method, just put the tag ahead of the output method call in the php file.

Description of Classes in the FormElement package

FormElement
The superclass for other input objects. It contains the basic properties for the other classes.
FormXHTMLRenderer
This class handles the output of all FormElement objects to XHTML. This object is created automatically by the FormElement object. It outputs valid XHTML 1.0 Strict code.
FormText
Class necessary for creating input text and textareas. Inherits FormElement.

FormElement class

All input classes inherit FormElement methods.

Methods

mixed var Get_Attr(string attribute)
Returns the value of the attribute for the element if it exists. If attribute doesn’t exist, returns false.
array Get_Attr_All()
Returns all the attributes of the input element as an associate array.
array Get_Label_Attr_All()
Returns all the attributes of the input element’s label as an associate array.
string Get_Label_Position()
Returns the label position.
string Get_Label_Text()
Returns the label text.
string Get_Tag_Between()
Returns the text between the label and the input element.
string Get_Value()
Returns the value of the element.
output([boolean stripslashes[, boolean htmlentities]])
Outputs the element’s XHTML. Stripslahes filters the output through PHP’s stripslashes function (defaults to true). htmlentities filters the output through Prolifique’s makeAllEntities function. (defaults to true)
Set_Attr(array arg)
Sets the element’s attributes using an associate array. The array key is the attribute, the value is the attribute value
Set_Label_Attr(array arg)
Sets the element’s label attributes using an associate array. The array key is the attribute, the value is the attribute value
Set_Label_Position(string arg)
Sets the position of the label. Here are some examples of label positions.
Set_Label_Text(string arg)
Sets the text of the label.
Set_Tag_Between(string arg)
Sets the text between the label and the input element. This could include a line break or a horizontal rule, for example.
Set_Value(string arg)
Sets the default value for the text element. This value will be overridden if the form has been posted back and there is appropriate POST data.

FormText class (text input, textarea, and password)

Prototype

FormText(string name/id[, string type[, string label text[, string label position[, string text between[, string value[, array element attributes[, array label attributes]]]]]]])

Arguments
name/id (string)
name/id of element
type (string)
Options:

input
Sets element type as input text (Default)
textarea
Sets element type as textarea
password
Sets element type to password (input text with masked characters)
label text (string)
Text to use as the label. If this is not specified, no label tag will be generated.
label position (string)
Options: (view examples)

before
Puts the label tag before the input element and creates the association using the for attribute. (Default)
wrap
Creates the label tag before the input element, but closes the label tag after the input element
text between (string)
Text that goes between the label and the input element (i.e. “<br />”) Note: This argument is ignored if the label position argument is set to “wrap”
value (string)
Sets the default value of the element. If the form has been posted back, then the POST data will override this value.
element attributes (array)
associative array of attributes for this element where the attribute is the key, and the value is the array value.
Example: array(’class’=>’required’) will assign class=”required” to the element.
label attributes (array)
associative array of attributes for this element’s label where the attribute is the key, and the value is the array value.
Example: array(’class’=>’required’) will assign class=”required” to the element.

Example of label position:
<!--
   Label Position "Before"
   Generates the entire label before the input (or textarea) tag and associates it using the "for" attribute.
-->
 
<label for="name_first">First Name</label><input type="text" name="name_first" id="name_first" />
 
<!--
   Label Position "Wrap"
   Wraps the label tag around the label text AND input (or textarea) element.
-->
 
<label>First Name<input type="text" name="name_first" id="name_first" /></label>

Simple usage for FormText

To add a text input or textarea element to your page, use the following code:

require_once('FormElement.class.php');  # Include the class
 
# Simple Example
$input_01=new FormText('input_text'); # Creates text input named "input_text"
$input_01->output();                  # Displays object 
 
# This creates an object named $input_01 with the name <em>input_text</em>.
# The output HTML will look like this:
# <input type="text" name="input_text" id="input_text" />

Pretty simple right?

More Examples

FormText examples can be found here. This page contains simple and advanced usages of FormText. It will also demonstrate the sticky aspect of the script by including a submit button.

I’m always looking for input (ba dumb!).

If you would like to see some features added or have any feedback whatsoever, please let me know.

Enjoy.

Phil Dodson
Visional Echoes

Leave a Comment

RSS feed for comments on this post. TrackBack URL

There are no comments on this post.

Sorry, the comment form is closed at this time.