Posts Tagged ‘HTML_QuickForm’

Make Zend_Form Bend To Your Will

Posted in Code, Programming on September 23rd, 2009 by Eric Lamb – 4 Comments

The first thing you need when working with any new web language or framework is figure out how to work with forms. Forms are one of the constants when working online and it’s the rare occasion when you can bypass dealing with them. They are also one of the more rote and boring portions of a web app to build; they’re, pretty much, always the same requiring the same fields on the same pages with the same validation rules and the same database schema.

Make Zend_Form Bend To Your Will

Make Zend_Form Bend To Your Will

If you’re smart dealing with forms effectively becomes a priority. My previously held favorite method was using HTML_QuickForm for handling forms. If you’ve never used; IT IS AWESOME.

Here’s an example login form script:

<?php
/**
 * Include HTML_QuickForm library
 */
require 'HTML/QuickForm.php';
 
/**
 * Include HTML_QuickForm Smarty Renderer
 */
require('HTML/QuickForm/Renderer/ArraySmarty.php');
 
$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($smarty);
$form = new HTML_QuickForm('login');
 
$form->addElement('text','Email','Email:' ,'class="textfield" size="30" maxlength="128"');
$form->addRule('Email','Please Enter Your Email','required', null, 'client');
$form->addRule('Email','Please enter a valid email address','email', true, 'client');
 
$form->addElement('password','Password','Password:','class="textfield" size="30" maxlength="128"');
$form->addRule('Password','Please Enter Your Password','required', null, 'client');
$form->addElement('checkbox','remember',null,'Remember Me?');
$form->addElement('submit','save','Submit','class="csubmit"');
 
if ($form->validate()) {
    //do the processing stuff here...
 
}
 
$form->accept($renderer);
$smarty->assign('LoginForm', $renderer->toArray());
?>

Very straightforward I think; instantiate the object, declare the elements, attach the rules and send to smarty (view) object.

The corresponding Smarty template is just as straightforward:

{$LoginForm.javascript}
<form {$LoginForm.attributes}>
{$LoginForm.hidden}
 
{if $LoginForm.Email.error}<span class="errorMessage">{$LoginForm.Email.error}</span><br />{/if}
<div id="email">
	Email: {$LoginForm.Email.html}
</div>
 
<div id="password">
	Password: {$LoginForm.Password.html}
</div>
 
<div id="remember">
	{$LoginForm.remember.html}
</div>
 
 
<div id="submit">
	{$LoginForm.submit.html}
</div>
</form>

That’s my preferred method when layout of the form is important and/or outside the box; it’s totally possible to use HTML_QuickForm without the Smarty integration but I like the control. It allows me to build forms as complicated in layout as the I Am Legend community was (my baseline for form complexity):

Register for I Am Legend Community

Register for I Am Legend Community

Since I’m now using the Zend Framework, and it’s form component is Zend_Form, I can honestly say after the flexibility of HTML_QuickForm and Smarty Zend_Form is, at first, not so awesome. A lot of the resources and tutorials/articles for Zend_Form tout it’s use of the decorator design pattern and all the examples I found looked the same. Not. Good.

Almost none of the references I looked at mentioned anything about removing decorators and rendering just the raw form elements like I needed. It is possible though; you just have to attache a couple extra methods to remove the decorators as laid out below. Make note of the calls to “removeDecorator” and “setAttrib”. Those are the key pieces.

As a counter point here’s the above script using Zend_Form:

<?php
//APPLICATION_PATH/forms/Login.php
<?php
 
class Form_Login extends Zend_Form
{
	public function __construct($options = null)
	{
		parent::__construct($options);
 
		$email = new Zend_Form_Element_Text('email');
		$email->setLabel('Email')
				->setRequired(true)
				->addFilter('StripTags')
				->addFilter('StringTrim')
				->addValidator('NotEmpty', TRUE)
				->addValidator(new Zend_Validate_Db_RecordExists('users', 'email'))
				->removeDecorator('label')
				->removeDecorator('htmlTag')
				->removeDecorator('description')
				->setAttrib('class', 'transperentInput');
 
		$password = new Zend_Form_Element_Password('password');
		$password->setLabel('Password')
				->setRequired(true)
				->addFilter('StripTags')
				->addFilter('StringTrim')
				->addValidator('NotEmpty')
				->removeDecorator('label')
				->removeDecorator('htmlTag')
				->removeDecorator('description')
				->setAttrib('class', 'transperentInput');
 
		$submit = new Zend_Form_Element_Submit('submit');		
		$this->addElements(array($email, $password, $submit));
    }
}
?>

Since Zend_Form is a part of the Zend Framework MVC the above is just the form class and the below is the actual processing portion:

<?php
//APPLICATION_PATH/controllers/Login.php
class LoginController extends Zend_Controller_Action
{
	public function indexAction()
	{
		$form = new Form_Login;
		if ($this->getRequest()->isPost()) {
 
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
                            //do the processing stuff here...
 
			}
 
		}
 
		$this->view->form = $form;
	}
}
?>

Sigh…
And then in the view script:

<form action="<?php echo $this->escape($this->form->getAction()); ?>" 
method="<?php echo $this->escape($this->form->getMethod()) ?>" 
id="sumbitForm">
<div id="email">
	Email: <?=$this->form->email; ?>
</div>
 
<div id="password">
	Password: <?=$this->form->password; ?>
</div>
 
<div id="remember">
	<?=$this->form->remember; ?>
</div>
 
 
<div id="submit">
	<?=$this->form->submit; ?>
</div>
</form>

There you go: Zend_Form is your bitch.

Bookmark and Share

HTML_QuickForm Validation Functions

Posted in Code, Programming on April 3rd, 2009 by Eric Lamb – Be the first to comment

I’ve been using HTML_QuickForm for, what feels like, forever. I’m happy to say that I haven’t, manually, written a form in years. God, do they suck to write….

I may actually be in love with HTML_QuickForm.

HTML QuickForm

HTML QuickForm

Sigh…

Not everyone like HTML_QuickForm but it meets my needs pretty well. I don’t work with any php framework, I use Smarty extensively (QuickForm fits like a glove with Smarty) and have yet to get hung up on the crossover between business and design logic with it.

It is lacking some core validation functionality though so in the spirit of open source here are just a few of the validation functions I’ve written; free and clear.

NOTE: I’m assuming you already know about HTML_QuickForm already. If you aren’t already familiar, the below functions just won’t mean that much to you.

In the below are QuickForm ready validation functions that do the following:
1. Validate US PhoneNumber
2. Look up a URL
3. Check IP
4. Ensure a date isn’t set to the past
5. Ensure MySQL date format
6. Ensure positive number
7. Check decimal on number
8. Ensure a URL is valid
9. Make sure a file was uploaded (good for multi “file” field forms)

<?php
/**
 * Validation Functions
 *
 * Contains all the system validation.
 *
 * @author Eric Lamb <eric@ericlamb.net>
 * @version 1.0
 * @copyright	@author
 * @filesource
 * @link http://blog.ericlamb.net
 * @copyright 2005-2009 Eric Lamb
 */
 
/**
 * Ensures phone numbers are in the proper format
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	value of form field to check
 * @return  bool
 */
function CheckUSPhoneNum($element_name, $element_value)
{ 
	$PhoneNumber = ereg_replace("[^0-9]", "", $element_value); // Strip out non-numerics
	if(ereg("^([2-9][0-9]{2})([2-9][0-9]{2})([0-9]{4})$", $PhoneNumber, $NumberParts)){
		return "(" . $NumberParts[1] . ") " . $NumberParts[2] . "-" . $NumberParts[3];
	} else {
		return false;
	}
}
 
/**
 * Looks up a domain and makes sure it's valid; only works on *nix
 *
 * @param   string  $type	Type of banning to check
 * @param   string  $data	The cooresponding data to check
 * @return  bool
 */
function FormDomainLookup($element, $value, $arg) { 
    $value = str_replace('http://','',$value);
	exec("host -t ns $value",$hasil); 
    if (ereg("host $value not found.",strtolower(trim($hasil[0])))) { 
        return false; 
    } else { 
        return true; 
    }
}
 
/**
 * Makes sure an IP address is valid
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	IP Address to check
 * @return  bool
 */
function CheckIP ($element_name,$element_value) {
 
	$ip = $element_value;
	if (($longip = ip2long($ip)) !== false)
	{
		if ($ip == long2ip($longip))
		{
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	} 
}
 
/**
 * Makes sure a "date" isn't set to a past date.
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	Date to Check
 * @return  bool
 */
function StopPastDate($element_name, $element_value){
	global $vars, $now;
 
	//check that it's a valid date first
	$temp = explode('-', $element_value);
	if(!checkdate($temp['1'],$temp['2'],$temp['0'])){
		return false;
	}
 
	//now make sure the date isn't in the past.
	if($element_value < $now){
		return false;
	} else {
		return true;
	}
 
}
 
/**
 * Makes sure a "date" is a valid MYSQL format.
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	Date to Check
 * @return  bool
 */
function ValidateMysqlDate($element_name, $element_value){
	global $vars, $now;
 
	//check that it's a valid date first
	$temp = explode('-', $element_value);
	if(!checkdate($temp['1'],$temp['2'],$temp['0'])){
		return false;
	}
	return true;
}
 
/**
 * Makes sure an integer is positive
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	Int to Check
 * @return  bool
 */
function IsIntPositive($element_name, $element_value){
 
	if(!is_numeric($element_value){
		$element_value = (int)$element_value;
	}
	if($element_value < 1){
		return FALSE;
	}
	//exit;
 
	return TRUE;
}
 
/**
 * Makes sure an integer isn't a decimal
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	Int to Check
 * @return  bool
 */
function IsIntDecimal($element_name, $element_value){
 
	if(!is_numeric($element_value){
		$element_value = (int)$element_value;
	}
 
	if(strpos($element_value,'.') === FALSE){
		return TRUE;
	}
	//exit;
 
	return FALSE;
}
 
/**
 * Makes sure a URL is valid
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	URL to Check
 * @return  bool
 */
function IsURLValid($element_name,$element_value){
	if (preg_match("/^(http(s?):\/\/|ftp:\/\/{1})((\w+\.){1,})\w{2,}$/i", $element_value)) {
		return TRUE;
	} else {
		return FALSE;
	}
}
 
/**
 * Makes sure at least one file was uploaded
 *
 * @param   string  $element_name	name of form field to check
 * @param   string  $element_value	
 * @return  bool
 */
function EnsureUploadedFile($element_name, $element_value){
	foreach($_FILES AS $name => $data){
		if($data['error'] == '0'){
			return TRUE;
		}
	}
	return FALSE;
}
?>
Bookmark and Share