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

Review: adding a form helper class to the MVC-driven framework - 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

Just in case you haven’t read the previous part, where I started incorporating a basic HTML form helper class into this example framework, below I listed the partial source code belonging to this class. In this way you can quickly learn how it does its business.

Essentially, the form helper was initially created in the following manner:

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 </form> closing tag

                public static function close()

                {

                                return '</form>';

                }

}// End Form class

Unquestionably, understanding the underlying logic of the above “Form” helper class isn’t rocket science, is it? As you can see, the helper implements only three static methods, where two of them are aimed at rendering the opening and closing tags of a regular HTML form, and the remaining one simply constructs input-type elements.

Finally, I decided to add an additional method within the class, which permits it to return a Singleton instance of the helper -- but this process is entirely optional, especially in those cases where the rendering methods will be called statically.

At this point you should be familiar with the inner workings of the earlier “Form” helper class, since all of its methods are focused on performing basic string manipulations. Nevertheless, as I pointed out at the beginning, the form helper should be capable at least of constructing HTML text areas as well.

So, can this be accomplished in a few simple steps? You bet! And to prove this, in the next section I’m going to show you how to add this extra functionality to the helper via a single static method.

The full details of this process will be discussed in the upcoming segment, so to get there click on the link that appears below and keep reading.



 
 
>>> 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: