Zend_Form a Daunting and Cumbersome Task?
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.


[...] http://ordinarywebguy.wordpress.com/2010/09/25/zend_form-a-daunting-and-cumbersome-task/ [...]
Zend Framework News » Blog Archive » Ist Arbeiten mit Zend_Form eine schwierige und ermüdende Aufgabe
September 27, 2010 at 7:26 pm