Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Zend_Form to the Rescue - A Brief Introduction to Zend_Form
1. Zend Form to the Rescue!
A Brief Introduction to Zend_Form
2. About Me
Jeremy Kendall
PHP Developer since 2001
Organizer Memphis PHP (MemphisPHP.org)
Contributor to FRAPI project (getFRAPI.com)
jeremy@jeremykendall.net
@JeremyKendall
http://jeremykendall.net
3. Forms in General
● Ubiquitous
● Tedious
● Challenging to get right
● Security risk
● Primary job responsibility
4. Typical Form Requirements
● Collect data
● Filter input
● Validate input
● Display validation messages
● Include default data (ex. List of US States)
● Pre-populate fields (for edit/update operations)
● Should be easy to test
● . . . and more.
5. Typical PHP Form
● Tons of markup
● Tons of code
● Confusing conditionals
● Client side validation likely
● Server side validation?
● Requires two scripts: form & processor
● Not at all easy to test
● I could go on and on . . .
6. Zend Form to the Rescue!
● Introduced in ZF 1.5, early 2008
● Generates markup
● Filters and validates user input
● Displays validation advice
● Object oriented, easily extended
● Completely customizable
● Can be used apart from ZF MVC
● Easy to test
7. Standard Form Elements
● Button
● Captcha
● Checkbox
● File
● Hidden
● Hash
● Image
● MultiCheckbox
● MultiSelect
● Password
● Radio
● Reset
● Select
● Text
● TextArea
8. Standard Filter Classes
● Alnum
● Alpha
● Basename
● Boolean
● HtmlEntities
● StringToLower
● StringToUpper
● StringTrim
● And many more . . .
9. Standard Validation Classes
● Alnum
● Alpha
● Barcode
● Between
● Callback
● CreditCard
● Date
● Db
● RecordExists
● NoRecordExists
● Digits
● EmailAddress
● File
● Float
● GreaterThan
● Hex
● Hostname
● Iban
● Identical
● And many more . . .
10. Simple Contact Form
<?php
class Application_Form_Contact extends Zend_Form
{
public function init()
{
// Form elements and such will go here
}
}
11. Simple Contact Form
<?php
class Application_Form_Contact extends Zend_Form
{
public function init()
{
// Form elements and such will go here
}
}
12. Simple Contact Form
<?php
class Application_Form_Contact extends Zend_Form
{
public function init()
{
// Form elements and such will go here
}
}
13. Name Element
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Your name');
$name->setRequired(true);
$name->addValidators(array(
new Zend_Validate_Regex('/^[- a-z]+$/i'),
new Zend_Validate_StringLength(array('min' => 5))
));
$this->addElement($name);
14. Name Element
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Your name');
$name->setRequired(true);
$name->addValidators(array(
new Zend_Validate_Regex('/^[- a-z]+$/i'),
new Zend_Validate_StringLength(array('min' => 5))
));
$this->addElement($name);
15. Name Element
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Your name');
$name->setRequired(true);
$name->addValidators(array(
new Zend_Validate_Regex('/^[- a-z]+$/i'),
new Zend_Validate_StringLength(array('min' => 5))
));
$this->addElement($name);
16. Name Element
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Your name');
$name->setRequired(true);
$name->addValidators(array(
new Zend_Validate_Regex('/^[- a-z]+$/i'),
new Zend_Validate_StringLength(array('min' => 5))
));
$this->addElement($name);
17. Name Element
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Your name');
$name->setRequired(true);
$name->addValidators(array(
new Zend_Validate_Regex('/^[- a-z]+$/i'),
new Zend_Validate_StringLength(array('min' => 5))
));
$this->addElement($name);
18. Name Element
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Your name');
$name->setRequired(true);
$name->addValidators(array(
new Zend_Validate_Regex('/^[- a-z]+$/i'),
new Zend_Validate_StringLength(array('min' => 5))
));
$this->addElement($name);
19. Email Element
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email Address');
$email->setRequired(true);
$email->addValidators(array(
new Zend_Validate_EmailAddress(),
new Zend_Validate_StringLength(array('min' => 6))
));
$this->addElement($email);
20. Email Element
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email Address');
$email->setRequired(true);
$email->addValidators(array(
new Zend_Validate_EmailAddress(),
new Zend_Validate_StringLength(array('min' => 6))
));
$this->addElement($email);
21. Message Element
$message = new Zend_Form_Element_Textarea('message');
$message->setLabel('Whatcha got to say?');
$message->setRequired(true);
$message->setAttrib('cols', 40);
$message->setAttrib('rows', 20);
$this->addElement($message);
22. Message Element
$message = new Zend_Form_Element_Textarea('message');
$message->setLabel('Whatcha got to say?');
$message->setRequired(true);
$message->setAttrib('cols', 40);
$message->setAttrib('rows', 20);
$this->addElement($message);
23. Submit Element
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Make contact!');
$submit->setIgnore(true);
$this->addElement($submit);
24. Submit Element
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Make contact!');
$submit->setIgnore(true);
$this->addElement($submit);
25. Add Filters to All Elements
$this->setElementFilters(array(
new Zend_Filter_StringTrim(),
new Zend_Filter_StripTags()
));
26. MVC Action
public function indexAction()
{
$form = new Application_Form_Contact();
$this->view->form = $form;
if (!$this->getRequest()->isPost()) {
return;
}
if (!$form->isValid($_POST)) {
return;
}
// Send email, persist data, etc.
}
27. MVC Action
public function indexAction()
{
$form = new Application_Form_Contact();
$this->view->form = $form;
if (!$this->getRequest()->isPost()) {
return;
}
if (!$form->isValid($_POST)) {
return;
}
// Send email, persist data, etc.
}
28. MVC Action
public function indexAction()
{
$form = new Application_Form_Contact();
$this->view->form = $form;
if (!$this->getRequest()->isPost()) {
return;
}
if (!$form->isValid($_POST)) {
return;
}
// Send email, persist data, etc.
}
29. MVC Action
public function indexAction()
{
$form = new Application_Form_Contact();
$this->view->form = $form;
if (!$this->getRequest()->isPost()) {
return;
}
if (!$form->isValid($_POST)) {
return;
}
// Send email, persist data, etc.
}
30. MVC Action
public function indexAction()
{
$form = new Application_Form_Contact();
$this->view->form = $form;
if (!$this->getRequest()->isPost()) {
return;
}
if (!$form->isValid($_POST)) {
return;
}
// Send email, persist data, etc.
}
31. MVC Action
public function indexAction()
{
$form = new Application_Form_Contact();
$this->view->form = $form;
if (!$this->getRequest()->isPost()) {
return;
}
if (!$form->isValid($_POST)) {
return;
}
// Send email, persist data, etc.// Send email, persist data, etc.
}