The Singleton and Factory Patterns in PHP: Building a Form Generator Class - The source code box: listing the full developed classes (
Page 4 of 4 )
As I said before, here is the full list of the classes used over the series:
/*
***************************************
class definitions for form elements
***************************************
*/
// class textinput
class textinput{
private $html;
public function __construct($attributes=array()){
if(count($attributes)<1){
throw new Exception ('Invalid number of attributes');
}
$this->html='<input type="text" ';
foreach($attributes as $attribute=>$value){
$this->html.=$attribute.'="'.$value.'" ';
}
$this->html.='/>';
}
public function getHTML(){
return $this->html;
}
}
// class passwordinput
class passwordinput{
private $html;
public function __construct($attributes=array()){
if(count($attributes)<1){
throw new Exception ('Invalid number of attributes');
}
$this->html='<input type="password" ';
foreach($attributes as $attribute=>$value){
$this->html.=$attribute.'="'.$value.'" ';
}
$this->html.='/>';
}
public function getHTML(){
return $this->html;
}
}
// class hiddeninput
class hiddeninput{
private $html;
public function __construct($attributes=array()){
if(count($attributes)<1){
throw new Exception ('Invalid number of attributes');
}
$this->html='<input type="hidden" ';
foreach($attributes as $attribute=>$value){
$this->html.=$attribute.'="'.$value.'" ';
}
$this->html.='/>';
}
public function getHTML(){
return $this->html;
}
}
// class fileinput
class fileinput{
private $html;
public function __construct($attributes=array()){
if(count($attributes)<1){
throw new Exception ('Invalid number of attributes');
}
$this->html='<input type="file" ';
foreach($attributes as $attribute=>$value){
$this->html.=$attribute.'="'.$value.'" ';
}
$this->html.='/>';
}
public function getHTML(){
return $this->html;
}
}
// class imageinput
class imageinput{
private $html;
public function __construct($attributes=array()){
if(count($attributes)<1){
throw new Exception ('Invalid number of attributes');
}
$this->html='<input type="image" ';
foreach($attributes as $attribute=>$value){
$this->html.=$attribute.'="'.$value.'" ';
}
$this->html.='/>';
}
public function getHTML(){
return $this->html;
}
}
// class radiobutton
class radiobutton{
private $html;
public function __construct($attributes=array()){
if(count($attributes)<1){
throw new Exception ('Invalid number of attributes');
}
$this->html='<input type="radio" ';
foreach($attributes as $attribute=>$value){
$this->html.=$attribute.'="'.$value.'" ';
}
$this->html.='/>';
}
public function getHTML(){
return $this->html;
}
}
// class checkbox
class checkbox{
private $html;
public function __construct($attributes=array()){
if(count($attributes)<1){
throw new Exception ('Invalid number of attributes');
}
$this->html='<input type="checkbox" ';
foreach($attributes as $attribute=>$value){
$this->html.=$attribute.'="'.$value.'" ';
}
$this->html.='/>';
}
public function getHTML(){
return $this->html;
}
}
// class button
class button{
private $html;
public function __construct($attributes=array()){
if(count($attributes)<1){
throw new Exception ('Invalid number of attributes');
}
$this->html='<input type="button" ';
foreach($attributes as $attribute=>$value){
$this->html.=$attribute.'="'.$value.'" ';
}
$this->html.='/>';
}
public function getHTML(){
return $this->html;
}
}
// class submitbutton
class submitbutton{
private $html;
public function __construct($attributes=array()){
if(count($attributes)<1){
throw new Exception ('Invalid number of attributes');
}
$this->html='<input type="submit" ';
foreach($attributes as $attribute=>$value){
$this->html.=$attribute.'="'.$value.'" ';
}
$this->html.='/>';
}
public function getHTML(){
return $this->html;
}
}
// class resetbutton
class resetbutton{
private $html;
public function __construct($attributes=array()){
if(count($attributes)<1){
throw new Exception ('Invalid number of attributes');
}
$this->html='<input type="reset" ';
foreach($attributes as $attribute=>$value){
$this->html.=$attribute.'="'.$value.'" ';
}
$this->html.='/>';
}
public function getHTML(){
return $this->html;
}
}
// class textarea
class textarea{
private $html;
public function __construct($attributes=array()){
if(count($attributes)<1){
throw new Exception ('Invalid number of attributes');
}
$this->html='<textarea ';
$textvalue='';
foreach($attributes as $attribute=>$value){
($attribute!='value')?$this-
>html.=$attribute.'="'.$value.'" ':$textvalue=$value;
}
$this->html=preg_replace("/\"? $/","\">",$this->html);
$this->html.=$textvalue.'</textarea>';
}
public function getHTML(){
return $this->html;
}
}
// class selectbox
class selectbox{
private $html;
public function __construct($attributes=array()){
if(count($attributes)<1){
throw new Exception ('Invalid number of attributes');
}
$this->html='<select ';
$options='';
foreach($attributes as $attribute=>$value){
if($attribute!='options'){
$this->html.=$attribute.'="'.$value.'" ';
}
else{
foreach($value as $values=>$label){
$options.='<option value="'.$values.'">'.$label.'</option>';
}
}
}
$this->html=preg_replace("/\"? $/","\">",$this->html);
$this->html.=$options.'</select>';
}
public function getHTML(){
return $this->html;
}
}
/*
*****************************************
abstract formElementFactory class
*****************************************
*/
// abstract class formElementFactory
// this class is abstract thus cannot be instantiated
abstract class formElementFactory{
private function __construct(){}
public static function createElement($type,$attributes=array
()){
if(!class_exists($type)||!is_array($attributes)){
throw new Exception('Invalid method parameters');
}
// instantiate a new form element object
$formElement=new $type($attributes);
// return HTML of form element code
return $formElement->getHTML();
}
}
/*
**************************************
// formGenerator class definition
**************************************
*/
class formGenerator{
// data member declaration
private $elements=array(); // array of form elements
private $output=''; // dynamic output
private $elementHeader=''; // element header
private $elementFooter='<br />'; // element footer
private $name='theform'; // form name
private $method='post'; // form method
private $action; // form action
// constructor
public function __construct($elements=array()){
if(count($elements)<1){
throw new Exception('Invalid number of elements');
}
// data member initialization
$this->elements=$elements;
$this->action=$_SERVER['PHP_SELF'];
}
// create form code
public function createForm(){
$this->output.='<form name="'.$this->name.'"
action="'.$this->action.'" method="'.$this->method.'">';
foreach($this->elements as $element=>$attributes){
// call the abstract class formElementFactory
$this->output.=$this-
>elementHeader.formElementFactory::createElement
($element,$attributes).$this->elementFooter;
}
$this->output.='</form>';
}
// add form part
public function addFormPart($html='<br />'){
$this->output.=$html;
}
// add element header
public function addElementHeader($header){
$this->elementHeader=$header;
}
// add element footer
public function addElementFooter($footer){
$this->elementFooter=$footer;
}
// set form name
public function setName($name){
$this->name=$name;
}
// set form action
public function setAction($action){
$this->action=$action;
}
// set form method
public function setMethod($method){
if($method!='post'&&$method!='get'){
throw new Exception('Invalid form method');
}
$this->method=$method;
}
// get dynamic form output
public function getFormCode(){
return $this->output;
}
}
That’s about it. Hopefully you’ll have some fun studying the code and tweaking it to fit your personal needs. Also, so as to not make the code excessively long, I’ve not listed the classes suitable for PHP 4, but you may want to take a look at them in the previous article. A final note: all of the examples have been tested on PHP 4.3.4 and PHP 5.0.4. respectively. Happy coding!
| | Discuss The Singleton and Factory Patterns in PHP: Building a Form Generator Class | | | | | | | The final part of this series goes trough the makings of a form generator class, by... | | | | | | Just wondering how you would have a web-designer and a developer team-develop the... | | | | | | Well, thank you for the comments on my article, since that proves it was quite... | | | | | | Widgets would be my suggestion as well. Just ensuring that those less versed in... | | | | | | Thanks to you for the quick and positive response! Also, I'm glad to know that you... | | | | | | Excellent write up Alejandro. May I ask for the reason this last part is written for... | | | | | | Hello Matthijs,
Thank you for the compliments on this PHP article series. It's... | | | | | | Hi Alejandro,
Thanks for your reply. Your suggestion did solve the problem I had!... | | | | | |
Hi Matthijs,
Good to know that your problem is now fixed up. Of course it's... | | | | | | That's really helpful Alejandro. I'll take your suggestions and build something. I... | | | | | | Thank you Matthijs. I'm happy to know that my suggestions were helpful to you.
My... | | | | | | Hi Alejandro,
Thinking about patterns, and comparing your earlier articles about... | | | | | | Hello Matthijs,
Well, it's not a hard question, but perphaps it's a little long... | | | | | | Your comments are certainly helpful. And thanks for the links. I did see the site... | | | | | | Hi, Mathjis. It's good to know that my answers were useful to you, as well as the... | | | | | | Thanks for a great article it's gone along way in selling the benefits of OOP in... | | | | | | Thank you for the kind comments on my article. I really appreciate your feedback.... | | | | | | Many thanks for the quick reply Alejandro. I'll give that a go - it should give some... | | | | | | Thanks - worked a treat. I might try using this pattern for field validation as... | | | | | | Thank you. Also, I'm glad to know that this form generator class has been useful to... | | | | | | Thank you for the comments. Yes, adding form validation routines would be a nice... | | | | | | Your form generator example must be excellent, as it was very straightforward for me... | | | | | | Hello - Your form script is great - I'm just wondering how you would include a users... | | | | | | Thank you I was having the same issue with this code. | | | | | | Hi Regan,
Thanks for the kind words on my PHP article. There’s a few simple ways... | | | | | | Hi Alejandro,
I am using your form generator class for the project I am working... | | | | | | Hi Alejandro,
First, I have to thank you for the quick reply! Based on your form... | | | | | | Hello Many,
Thanks for the comments. I already answered this post to your email... | | | | | | Hello again Many,
Thanks for the comments and it's good to know you solved your... | | | | | | >>> Post your comment now! | | | | | |
|
 |
|