• Like
  • Save

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Zend_Form to the Rescue - A Brief Introduction to Zend_Form

on

  • 4,845 views

 

Statistics

Views

Total Views
4,845
Views on SlideShare
4,835
Embed Views
10

Actions

Likes
2
Downloads
34
Comments
1

2 Embeds 10

http://lanyrd.com 9
http://static.slidesharecdn.com 1

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • ritakoneh@hotmail.co.uk
    Hello My New friend
    My name is rita i saw your profile at(www.slideshare.net) and i love it i think we can click so please i will like you to email me back through my email address thus: so that i can told you more about me and give you my sweet picture so that you can know me will ok.
    Awaiting to see your lovely reply soonest.
    Miss rita ritakoneh@hotmail.co.uk
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Zend_Form to the Rescue - A Brief Introduction to Zend_Form Zend_Form to the Rescue - A Brief Introduction to Zend_Form Presentation Transcript

    • Zend Form to the Rescue! A Brief Introduction to Zend_Form
    • About Me Jeremy Kendall PHP Developer since 2001 Organizer Memphis PHP (MemphisPHP.org) Contributor to FRAPI project (getFRAPI.com) [email_address] @JeremyKendall http://jeremykendall.net
    • Forms in General
      • Ubiquitous
      • Tedious
      • Challenging to get right
      • Security risk
      • Primary job responsibility
    • 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.
    • 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 . . .
    • 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
    • Standard Form Elements
      • Button
      • Captcha
      • Checkbox
      • File
      • Hidden
      • Hash
      • Image
      • MultiCheckbox
      • MultiSelect
      • Password
      • Radio
      • Reset
      • Select
      • Text
      • TextArea
    • Standard Filter Classes
      • Alnum
      • Alpha
      • Basename
      • Boolean
      • HtmlEntities
      • StringToLower
      • StringToUpper
      • StringTrim
      • And many more . . .
    • 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 . . .
    • Simple Contact Form <?php class Application_Form_Contact extends Zend_Form { public function init () { // Form elements and such will go here } }
    • Simple Contact Form <?php class Application_Form_Contact extends Zend_Form { public function init() { // Form elements and such will go here } }
    • Simple Contact Form <?php class Application_Form_Contact extends Zend_Form { public function init () { // Form elements and such will go here } }
    • 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 );
    • 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 );
    • 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 );
    • 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 );
    • 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 );
    • 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 );
    • 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 );
    • 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 );
    • 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 );
    • 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 );
    • Submit Element $submit = new Zend_Form_Element_Submit ( 'submit' ); $submit -> setLabel ( 'Make contact!' ); $submit -> setIgnore (true); $this -> addElement ( $submit );
    • Submit Element $submit = new Zend_Form_Element_Submit('submit'); $submit ->setLabel('Make contact!'); $submit -> setIgnore (true); $this ->addElement( $submit );
    • Add Filters to All Elements $this -> setElementFilters ( array ( new Zend_Filter_StringTrim (), new Zend_Filter_StripTags () ));
    • 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. }
    • 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. }
    • 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. }
    • 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. }
    • 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. }
    • 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. }
    • View <?php echo $this ->form; ?>
    • Default Markup <form enctype = &quot;application/x-www-form-urlencoded&quot; action = &quot;&quot; method = &quot;post&quot; > <dl class = &quot;zend_form&quot; > <dt id = &quot;name-label&quot; > <label for = &quot;name&quot; class = &quot;required&quot; > Your name </label> </dt> <dd id = &quot;name-element&quot; > <input type = &quot;text&quot; name = &quot;name&quot; id = &quot;name&quot; value = &quot;&quot; > </dd> . . . </dl> </form>
    • What does it look like?
    • How about with errors?
    • Can I use Zend Form by itself?
    • Standalone - library/base.php <?php // Ensure library/ is on include_path set_include_path ( implode (PATH_SEPARATOR, array ( dirname (__FILE__), get_include_path (), ))); require_once dirname (__FILE__) . '/Zend/Loader/Autoloader.php' ; $autoloader = Zend_Loader_Autoloader:: getInstance (); $autoloader -> registerNamespace ( 'Application_' ); $view = new Zend_View ();
    • Standalone - library/base.php <?php // Ensure library/ is on include_path set_include_path ( implode (PATH_SEPARATOR, array ( dirname (__FILE__), get_include_path (), ))); require_once dirname(__FILE__) . '/Zend/Loader/Autoloader.php'; $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader ->registerNamespace('Application_'); $view = new Zend_View();
    • Standalone - library/base.php <?php // Ensure library/ is on include_path set_include_path(implode(PATH_SEPARATOR, array ( dirname(__FILE__), get_include_path(), ))); require_once dirname (__FILE__) . '/Zend/Loader/Autoloader.php' ; $autoloader = Zend_Loader_Autoloader:: getInstance (); $autoloader ->registerNamespace('Application_'); $view = new Zend_View();
    • Standalone - library/base.php <?php // Ensure library/ is on include_path set_include_path(implode(PATH_SEPARATOR, array ( dirname(__FILE__), get_include_path(), ))); require_once dirname(__FILE__) . '/Zend/Loader/Autoloader.php'; $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader -> registerNamespace ( 'Application_' ); $view = new Zend_View();
    • Standalone - library/base.php <?php // Ensure library/ is on include_path set_include_path(implode(PATH_SEPARATOR, array ( dirname(__FILE__), get_include_path(), ))); require_once dirname(__FILE__) . '/Zend/Loader/Autoloader.php'; $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader ->registerNamespace('Application_'); $view = new Zend_View ();
    • Standalone – form script <?php require_once dirname (__FILE__) . '/library/base.php' ; $form = new Application_Form_Contact (); if ( strtolower ( $_SERVER [ 'REQUEST_METHOD' ]) == 'get' || ! $form -> isValid ( $_POST )) { ?> <!-- Header file might go here --> <?php echo $form -> render ( $view ) ?> <!-- Footer file might go here --> <?php } elseif ( $form -> isValid ( $_POST )) { // Form valid, persist data, send email, etc. }
    • Standalone – form script <?php require_once dirname (__FILE__) . '/library/base.php' ; $form = new Application_Form_Contact(); if (strtolower( $_SERVER ['REQUEST_METHOD']) == 'get' || ! $form ->isValid( $_POST )) { ?> <!-- Header file might go here --> <?php echo $form ->render( $view ) ?> <!-- Footer file might go here --> <?php } elseif ( $form ->isValid( $_POST )) { // Form valid, persist data, send email, etc. }
    • Standalone – form script <?php require_once dirname(__FILE__) . '/library/base.php'; $form = new Application_Form_Contact (); if (strtolower( $_SERVER ['REQUEST_METHOD']) == 'get' || ! $form ->isValid( $_POST )) { ?> <!-- Header file might go here --> <?php echo $form ->render( $view ) ?> <!-- Footer file might go here --> <?php } elseif (s $form ->isValid( $_POST )) { // Form valid, persist data, send email, etc. }
    • Standalone – form script <?php require_once dirname(__FILE__) . '/library/base.php'; $form = new Application_Form_Contact(); if ( strtolower ( $_SERVER [ 'REQUEST_METHOD' ]) == 'get' || ! $form -> isValid ( $_POST )) { ?> <!-- Header file might go here --> <?php echo $form ->render( $view ) ?> <!-- Footer file might go here --> <?php } elseif (strtolower( $form ->isValid( $_POST )) { // Form valid, persist data, send email, etc. }
    • Standalone – form script <?php require_once dirname(__FILE__) . '/library/base.php'; $form = new Application_Form_Contact(); if (strtolower( $_SERVER ['REQUEST_METHOD']) == 'get' || ! $form ->isValid( $_POST )) { ?> <!-- Header file might go here --> <?php echo $form -> render ( $view ) ?> <!-- Footer file might go here --> <?php } elseif ( $form ->isValid( $_POST )) { // Form valid, persist data, send email, etc. }
    • Standalone – form script <?php require_once dirname(__FILE__) . '/library/base.php'; $form = new Application_Form_Contact(); if (strtolower( $_SERVER ['REQUEST_METHOD']) == 'get' || strtolower( $_SERVER ['REQUEST_METHOD']) == 'post' && ! $form ->isValid( $_POST )) { ?> <!-- Header file might go here --> <?php echo $form ->render( $view ) ?> <!-- Footer file might go here --> <?php } elseif ( $form -> isValid ( $_POST )) { // Form valid, persist data, send email, etc. }
    • Test time! <?php /** * ContactFormTest */ class ContactFormTest extends PHPUnit_Framework_TestCase { // Setup, teardown, and tests . . . }
    • Setting up our test public function setUp () { parent:: setUp (); $this ->_form = new Application_Form_Contact (); $this ->_data = array ( 'name' => 'Jeremy Kendall' , 'email' => 'jeremy@jeremykendall.net' , 'message' => 'Your slides are really, really ugly.' ); }
    • Setting up our test public function setUp() { parent::setUp(); $this ->_form = new Application_Form_Contact (); $this ->_data = array ( 'name' => 'Jeremy Kendall', 'email' => 'jeremy@jeremykendall.net', 'message' => 'Your slides are really, really ugly.' ); }
    • Setting up our test public function setUp() { parent::setUp(); $this ->_form = new Application_Form_Contact(); $this ->_data = array ( 'name' => 'Jeremy Kendall' , 'email' => 'jeremy@jeremykendall.net' , 'message' => 'Your slides are really, really ugly.' ); }
    • Test Valid Data public function testValidDataPassesValidation () { $this -> assertTrue ( $this ->_form-> isValid ( $this ->_data)); }
    • Test Invalid Data public function testShortNameInvalidatesForm () { $this ->_data[ 'name' ] = 'Bo' ; $this -> assertFalse ( $this ->_form-> isValid ( $this ->_data)); $messages = $this ->_form-> getMessages (); $this -> assertArrayHasKey ( 'name' , $messages ); $this -> assertArrayHasKey ( 'stringLengthTooShort' , $messages [ 'name' ] ); $this -> assertEquals ( &quot;'Bo' is less than 5 characters long&quot; , $messages [ 'name' ][ 'stringLengthTooShort' ] ); }
    • Test Invalid Data public function testShortNameInvalidatesForm() { $this ->_data[ 'name' ] = 'Bo' ; $this ->assertFalse( $this ->_form->isValid( $this ->_data)); $messages = $this ->_form->getMessages(); $this ->assertArrayHasKey('name', $messages ); $this ->assertArrayHasKey( 'stringLengthTooShort', $messages ['name'] ); $this ->assertEquals( &quot;'Bo' is less than 5 characters long&quot;, $messages ['name']['stringLengthTooShort'] ); }
    • Test Invalid Data public function testShortNameInvalidatesForm() { $this ->_data['name'] = 'Bo'; $this -> assertFalse ( $this ->_form-> isValid ( $this ->_data)); $messages = $this ->_form->getMessages(); $this ->assertArrayHasKey('name', $messages ); $this ->assertArrayHasKey( 'stringLengthTooShort', $messages ['name'] ); $this ->assertEquals( &quot;'Bo' is less than 5 characters long&quot;, $messages ['name']['stringLengthTooShort'] ); }
    • Test Invalid Data public function testShortNameInvalidatesForm() { $this ->_data['name'] = 'Bo'; $this ->assertFalse( $this ->_form->isValid( $this ->_data)); $messages = $this ->_form-> getMessages (); $this ->assertArrayHasKey('name', $messages ); $this ->assertArrayHasKey( 'stringLengthTooShort', $messages ['name'] ); $this ->assertEquals( &quot;'Bo' is less than 5 characters long&quot;, $messages ['name']['stringLengthTooShort'] ); }
    • Test Invalid Data public function testShortNameInvalidatesForm() { $this ->_data['name'] = 'Bo'; $this ->assertFalse( $this ->_form->isValid( $this ->_data)); $messages = $this ->_form->getMessages(); $this -> assertArrayHasKey ( 'name' , $messages ); $this -> assertArrayHasKey ( 'stringLengthTooShort' , $messages [ 'name' ] ); $this -> assertEquals ( &quot;'Bo' is less than 5 characters long&quot; , $messages [ 'name' ][ 'stringLengthTooShort' ] ); }
    • PHPUnit Green, my Favorite Color!
    • Zend Form - Pros
      • Object oriented
      • Easy input validation
      • Easy input filtering
      • Markup generation
      • Reusable
      • Easy to test
    • Zend Form - Cons
      • Learning curve
      • Custom layouts can be challenging
      • Default markup blows isn't my favorite
    • Wrapping Up
      • Zend Form can save you from form hell
      • Powerful in MVC
      • Very simple to use outside of MVC
      • Easy to test!
    • Questions?
    • Resources
      • Zend_Form Quickstart
        • http://bit.ly/ba8fr0
      • Rob Allen's talk, “Working with Zend_Form”
        • http://akrabat.com/talks/
      • Zend_Form_Element_Multi – Tips and Tricks
        • http://bit.ly/bEZl37
    • Thanks! Rate this talk: http://joind.in/2388 [email_address] @JeremyKendall http://jeremykendall.net