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

Finishing the HTML form helper class: a method for rendering text area elements - 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.

  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



As I explained in the segment that you just read, it’s mandatory to give the HTML form helper class the ability to construct basic text areas, to expand its existing functionality a bit further. Therefore, in the following lines I included the definition of a brand method that will be added to the helper later on, which renders those HTML elements in a straightforward fashion.

Take a look at it, please:

// 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;



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




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


From the previous code fragment, it’s easy to see how the “textarea()” static method functions. As its name suggests, all it does is generate the HTML markup required to render text area elements.

Additionally, the method performs a basic checking process to determine which HTML attributes are valid to assign to the text area being created. This process, logically, is subject to further improvements as well.

So far, so good, right? Having defined and implemented a method that constructs text areas in web forms, the helper class is almost set. Of course, it’s possible to add more methods to it that render selects, buttons, <fieldset> and <legend> elements, but this task will be left as homework for you, for those moments where you’re bored and want to do something really useful.

All right, now that you grasped the logic behind the previous “textarea()” methods, it’s time to show the complete source code of the from helper class, after including this last rendering method.

This topic will be covered in the section to come. Thus, jump ahead and read the following lines.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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