Ordinary Web Guy

Simply loving web development goodies

Zend_Form a Daunting and Cumbersome Task?

with one comment

Zend_Form is simply the easiest way to generate forms in Zend Framework applications. But what if you have a custom web 2.0 form? Is it still applicable to use? I’ve been into several Zend Framework projects but never came to my mind to utilize Zend_Form. There is a tutorial on how to create custom form decorators but for me it’s a daunting and cumbersome task. But are there still other workaround aside with that tutorial? The answer is “YES” and it’s preposterous that those web projects I made for its forms were made manually specially with the validation. At first place, I’ve never known about it. Sometimes ignorance could lead to catastrophe.

How to do it? See below the trick:

class Application_Forms_ForgetPassword extends Zend_Form {
public function init()
{
$this->setName(‘forgetPasswordForm’);
$this->setMethod(‘post’);
$this->setAction(‘/index/retrieve-password’);

$emailExist = new Zend_Validate_Db_RecordExists(‘users’, ‘email’);
$emailExist->setMessage(‘Email doesn\’t exists.’);

$email = new Zend_Form_Element_Text(‘email’, array(‘size’ => 25));
$email->setLabel(‘Email Address:’)
->setRequired(true)
->addFilter(‘StripTags’)
->addFilter(‘StringTrim’)
->addFilter(‘StringToLower’)
->addValidator($emailExist)
->addValidator(‘NotEmpty’)
->addValidator(‘EmailAddress’);

$this->addElements(array($token, $email));
$this->setDecorators( array( array(‘ViewScript’, array(‘viewScript’ => ‘index/forget-password.phtml’))));
}
}

Here’s only the messy part with view.

<form action=”<?php echo $this->escape($this->element->getAction()); ?>” method=”<?php echo $this->escape($this->element->getMethod()); ?>” id=”forgetPasswordForm”>
<h2>Forgot Password</h2>
<?php echo $this->formLabel($this->element->email->getName(), $this->translate($this->element->email->getLabel())) ?>
<br />
<?php echo $this->formText($this->element->email->getName(), $this->element->email->getValue(), $this->element->email->getAttribs())?>
<div><a href=”#”>Send</a></div>
</form>

So Zend_Form is not daunting and cumbersome task for custom forms it’s indeed a time saver.

Advertisement

Written by admin

September 25, 2010 at 6:40 pm

Posted in PHP, Zend Framework

One Response

Subscribe to comments with RSS.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.