Form Helper Class

veedeoo 3 Tallied Votes 753 Views Share

The purpose of this class is to be able to create an html form with ease. This is inspired by the form helper in CodeIgniter. This can easily adapt to bootstrap or any css and jquery where id is required.

To use this class, we need to include the class provided below.

require_once('FormHelper.php');

## we create an instance of the class
$form_create = new FormHelper();

we can also dynamically create <select>, by defining the option items as an array

$select_array = array('php','java','html','javascript','python','perl','jquery');

generate the form

echo $form_create->form_option('post', 'processor.php', 'myform', 'coolform');
echo $form_create->form_label('Name', 'name');
echo $form_create->form_input('text', 'name', '', 'input_class', 'type your name here');
echo '<br/>';
echo $form_create->form_label('Select Language','lang');
echo $form_create->form_select($select_array,'language');
echo '<br/>';
echo $form_create->form_submit('submit', 'Submit', 'submit_button', 'submit');
echo $form_create->form_close();

the above should give us this output

<form method="post" action="processor.php" id="myform" class="coolform">
<label class="name" id="">Name</label>
<input type="text" name="name" value="" class="input_class" placeholder="type your name here"/>
<br/>
<label class="lang" id="">Select Language</label>

<select name="language">
<option value="php">Php</option>
<option value="java">Java</option>
<option value="html">Html</option>
<option value="javascript">Javascript</option>
<option value="python">Python</option>
<option value="perl">Perl</option>
<option value="jquery">Jquery</option>
</select>
<br/>
<input type="submit" name="submit" value="Submit" class="submit_button" id="submit"/>
</form>

How to handle the parameters in methods?
Let's take this method as an example

form_input($type, $name, $value = null, $class = null, $placeholder = null)

$type and $name are mandatory, while $value, $class, and $placeholders are not even required. However, if we want to add placeholder, we cannot just put our placeholder parameter after the $name parameter, instead we need to place an empty values for the $value and $class.

 echo $form_create->form_input('text', 'name', '', '', 'type your placeholder here');

I hope this class will help you in your future project.

Disclaimer!
I sincerely apologize for using the $class as a parameter name in some of the methods, I did not realize until today that it is one of the reserved keyword in PHP of which I should not have used it even in naming my variables.

I also posted this class on my github gist.

diafol commented: nice work +15
<?php

/**
 * FormHelper
 * 
 * @package veedeoo HMVC
 * @author @Veedeoo and @Geegler
 * @copyright 2014
 * @version 2.1.0
 * @access public
 */
class FormHelper
{
    /**
     * FormHelper::__construct()
     * 
     * @return
     */
    public function __construct()
    {
	
		
    }

    /**
     * FormHelper::form_option()
     * 
     * @param mixed $method
     * @param mixed $action
     * @param mixed $id
     * @param mixed $class
     * @return
     */
    public function form_option($method, $action, $id = null, $class = null)
    {
        return '<form method="' . $method . '" action="' . $action . '" id="' . $id .
            '" class="' . $class . '">';
       
    }

    /**
     * FormHelper::form_label()
     * 
     * @param mixed $name
     * @param mixed $class
     * @param mixed $id
     * @return
     */
    public function form_label($name, $class = null, $id = null)
    {
        return '<label class="' . $class . '" id="' . $id . '">' . $name . '</label>';
    }

    /**
     * FormHelper::form_input()
     * 
     * @param mixed $type
     * @param mixed $name
     * @param mixed $value
     * @param mixed $class
     * @param mixed $placeholder
     * @return
     */
    public function form_input($type, $name, $value = null, $class = null, $placeholder = null)
    {
        if (!empty($type)) {
            return '<input type="' . $type . '" name="' . $name . '" value="' . $value .
                '" class="' . $class . '" placeholder="' . $placeholder . '"/>';
        }

        return;
    }


    /**
     * FormHelper::form_submit()
     * 
     * @param mixed $name
     * @param mixed $value
     * @param mixed $class
     * @param mixed $id
     * @return
     */
    public function form_submit($name, $value, $class = null, $id = null)
    {
        return '<input type="submit" name="' . $name . '" value="' . $value .
            '" class="' . $class . '" id="' . $id . '"/>';
    }

    /**
     * FormHelper::form_select()
     * 
     * @param mixed $items
     * @param mixed $name
     * @return
     */
    public function form_select($items = array(), $name)
    {
        $select = '';
        if (is_array($items)) {

            $select .= '<select name="' . $name . '">';
            foreach ($items as $item) {
                $select .= '<option value="' . $item . '">' . ucfirst($item) . '</option>';
            }

            $select .= '</select>';
            
        }
        return $select;
    }

    /**
     * FormHelper::form_close()
     * 
     * @return
     */
    public function form_close()
    {
        
        return '</form>';
    }
}
veedeoo 474 Junior Poster Featured Poster

The above class can also be use in Smarty and other template engines that don't have any form generator. Below is a classic example if we are to do it on smarty..

Controller file or any simple PHP file..

$smarty->assign(
                array(
                      'form_open' => $form_create->form_option('post', 'processor.php', 'myform', 'coolform'),
                      'name_label' => $form_create->form_label('Name', 'name'),
                      'name_input' => $form_create->form_input('text', 'name', '', 'input_class', 'type your name here'),
                      'lang_label' => $form_create->form_label('Select Language','lang'),
                    'lang_select' => $form_create->form_select($select_array,'language'),
                    'form_submit' => $form_create->form_submit('submit', 'Submit', 'submit_button', 'submit'),
                    'form_close' => $form_create->form_close()

                    )
                 );  

template file

{$open_form}

{$name_label}

{$name_input}

<br/>

{$lang_label}
{$lang_select}

<br/>

{$form_submit}

{$form_close}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.