Filtering Input Data and Generating HTML Forms with the HMVC Design Pattern - Building an HTML form helper
(Page 4 of 4 )
Building a production-ready HTML form helper class is a pretty demanding task that I'm definitely not going to tackle in this particular case, as it would be a waste of time. Instead, the helper that I plan to construct as part of this sample framework will have some basic markup generation abilities for creating simple form input elements and text areas.
Essentially, the definition of this helper class will be the following:
(Form.php)
<?php
class Form
{
// private constructor
private function __construct(){}
// 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
As I explained before, the “Form” class implements a few static methods that allow you to render input form elements on screen, along with text areas. Quite possibly, you’ll want to expand the helper’s functionality by adding a method that constructs HTML “selects,” but this process will be left as homework for you.
Now, with the inclusion of this helper class, the framework is capable of generating basic HTML forms, which is an indispensable feature for collecting user data. This could be used, for instance, for developing an isolated MVC module that implements a login system, including the corresponding web form, a validation model and one controller that monitors the whole login process.
This is only a simple example of what can be achieved with the HMVC design pattern. It could be done with this sample framework, except that it currently lacks a model class. However, this one will be coded in the next tutorial.
Final thoughts
That’s all for now. In this fourth episode of the series you learned how to extend the existing functionality of this HMVC-based framework by adding to it a pair of classes that permit you to sanitize incoming data in a basic way and generate simple HTML forms.
For the time being, it seems that the inclusion of these classes doesn’t help too much in understanding how to implement a fully-functional HMVC layer in a concrete case. This is only a misconception that will be refuted when I start developing a web application that puts all of those classes to work side by side, trust me.
In the next tutorial of the series, I’m going to create another core class within the framework that will represent the “M” in the HMVC acronym. Yes, you guessed right! That tutorial will be focused on building a model class, so don’t miss that part!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.