Home arrow PHP arrow Page 3 - Adding a Form Helper to an MVC-based Framework in PHP 5

Building a basic web form helper class - PHP

In this fifth episode of the series, I start building an HTML form helper class to add to this sample MVC framework the ability to render some basic form elements.

TABLE OF CONTENTS:
  1. Adding a Form Helper to an MVC-based Framework in PHP 5
  2. Review: the framework’s source files
  3. Building a basic web form helper class
  4. Adding a method to render input elements
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
June 15, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Expressed in simple terms, the web form helper class that I plan to build in the following lines will be pretty basic. It will implement some simple methods aimed at rendering different form elements, including different types of input boxes, radio buttons, text areas and so forth. So far, nothing unexpected, right?

In most cases, the methods of this helper will be called statically to prevent an  unnecessary instantiation of the originating class. However, similarly to other classes developed before, this one will implement a “getInstance()” method that will return a Singleton instance of the class in question.

Now that I have outlined how the form helper is going to work, please take a look at its partial source code, which looks as simple as this:

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

                public static function close()

                {

                                return '</form>';

                }

}// End Form class

Apart from the implementation of the aforementioned “getInstance()” method that returns a Singleton object, which may look a bit tricky, the rest of the logic of the above “Form” class should be extremely easy to follow for you. As shown above, the class defines just a couple of methods for rendering the opening and closing tags of an HTML form. Period.

These methods have been declared static, meaning that there’s no need to create an instance of the helper to invoke them directly. Also, it must be admitted that the class in its current incarnation could be largely improved, to make it more functional. But in keeping with the requirements of this particular project, it’ll be kept simple.

So far, so good, right? At this moment, I’m sure that you've grasped how the previous “Form” class does its thing, so it’s time to add more methods to it. In the last segment of this tutorial I’m going to code one that will be tasked with rendering some common form input elements.

To learn how this new method will be implemented, go ahead and read the following section. It’s only one click away.



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