Home arrow PHP arrow Page 4 - Finishing a Form Helper Class for an MVC-based Framework in PHP 5

The complete definition of the HTML form helper class - PHP

In this sixth part of the series, I finish building a basic HTML form helper class. Doing this adds yet another important component to the sample MVC-driven framework we're developing in this group of tutorials.

TABLE OF CONTENTS:
  1. Finishing a Form Helper Class for an MVC-based Framework in PHP 5
  2. Review: adding a form helper class to the MVC-driven framework
  3. Finishing the HTML form helper class: a method for rendering text area elements
  4. The complete definition of the HTML form helper class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
June 21, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The best way to understand how the static “textarea()” method coded in the preceding segment fits into the structure of the web form helper class is by examining the full source code of this class. Below I included the complete definition of the helper, so you can see it in one single place for a closer analysis. Here’s the file containing this class:

(Form.php)

<?php

class Form

{

                private static $instance = NULL;

               

                // get Singleton instance of Form class

                public static function getInstance()

                {

                                if (self::$instance === NULL)

                                {

                                                self::$instance = new self;

                                }

                                return self::$instance;

                }

               

                // render <form> opening tag

                public static function open(array $attributes)

                {             

                                $html = '<form';

                                if (!empty($attributes))

                                {

                                                foreach ($attributes as $attribute => $value)

                                                {

                                                                if (in_array($attribute, array('action', 'method', 'id', 'class', 'enctype')) and !empty($value))

                                                                {

                                                                                // assign default value to 'method' attribute

                                                                                if ($attribute === 'method' and ($value !== 'post' or $value !== 'get'))

                                                                                {

                                                                                                $value = 'post';

                                                                                }

                                                                                $html .= ' ' . $attribute . '="' . $value . '"';

                                                                }

                                                }

                                }

                                return $html . '>';

                }

               

                // render <input> tag

                public static function input(array $attributes)

                {             

                                $html = '<input';

                                if (!empty($attributes))

                                {

                                                foreach ($attributes as $attribute => $value)

                                                {

                                                                if (in_array($attribute, array('type', 'id', 'class', 'name', 'value')) and !empty($value))

                                                                {

                                                                                $html .= ' ' . $attribute . '="' . $value . '"';

                                                                }

                                                }

                                }

                                return $html . '>';

                }

               

                // render <textarea> tag

                public static function textarea(array $attributes)

                {             

                                $html = '<textarea';

                                $content = '';

                                if (!empty($attributes))

                                {

                                                foreach ($attributes as $attribute => $value)

                                                {

                                                                if (in_array($attribute, array('rows', 'cols', 'id', 'class', 'name', 'value')) and !empty($value))

                                                                {

                                                                                if ($attribute === 'value')

                                                                                {

                                                                                                $content = $value;

                                                                                                continue;

                                                                                }

                                                                                $html .= ' ' . $attribute . '="' . $value . '"';

                                                                }

                                                }

                                }

                                return $html . '>' . $content . '</textarea>';

                }

               

                // render </form> closing tag

                public static function close()

                {

                                return '</form>';

                }

}// End Form class

Not too bad, eh? Now that the development of the above “Form” class is over, the functionality of the sample framework has been considerably enhanced. Now it is able to render different elements of an HTML form in a pretty automatic manner.

As I said before, I encourage you to add more methods to this helper class. You can give it the ability to construct selects, buttons and other useful elements, which will save you from having to explicitly code any HTML markup.

Final thoughts

Over this sixth part of the series, I finished building a basic HTML form helper class, in this way adding yet another important component to the sample MVC-driven framework currently being developed in this group of tutorials.

At this point, the framework has been packaged with a decent variety of classes that make it pretty functional. Even so, it’d be useful to make it able to cache both database result sets and HTML output. This extra feature would turn in into a more efficient piece of software.

With that idea in mind, in the following tutorial I’m going to incorporate into the framework a handy caching class. So, if you want to learn how this class will be created, don’t miss the next article!    



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: