zend form tutorial

Download Zend Form Tutorial

If you can't read please download the document

Upload: michelangelo-van-dam

Post on 17-May-2015

64.530 views

Category:

Technology


3 download

DESCRIPTION

A mini tutorial about using Zend_Form and Zend_Config_Xml

TRANSCRIPT

  • 1. Zend_Form tutorial by Michelangelo van Dam

2. Who is Michelangelo van Dam ?

  • Freelance PHP consultant with over 7 years of enterprise level PHP development experience.
  • Started using Zend Framework in 2007 and contributes to this framework for Zend_Ldap.
  • web:http://www.in2it.be
  • blog:http://dragonbe.blogspot.com
  • e-mail: dragonbe [at] google mail

3. Zend_Form

  • Zend_Form simplifies form creation and handling in your web application. It accomplishes the following goals:
  • Element input filtering and validation
  • Element ordering
  • Element and Form rendering, including escaping
  • Element and form grouping
  • Element and form-level configuration

4. Zend_Form (continued)

  • It heavily leverages other Zend Framework components to accomplish its goals, including Zend_Config, Zend_Validate, Zend_Filter, Zend_Loader_PluginLoader, and optionally Zend_View.
  • More info can be found athttp://framework.zend.com/manual/en/zend.form.html

5. Directory structure

  • application_root/
    • application/
      • config/-> this is where you put the configuration files
        • userform.xml
      • controllers/-> this is where you put your controllers
        • UserController.php
      • views/-> this is where you put your view templates
        • user/
          • login.phtml, register.phtml, resetpassword.phtml
    • web/
      • index.php-> your bootstrap file
    • library/
      • Zend-> This is where you put Zend Framework

6. Requirements

  • Zend Framework 1.5 ( http://framework.zend.com )
  • an editor (vi) or IDE (Zend Studio Neon)
  • a (huge) cup of coffee
  • knowledge of XML or INI structures
  • a site already using Zend Framework MVC

7. Example description

  • We're going to build the following forms:
    • User registration form
    • User login form
    • Password retrieval form

8. userform.xml global structure

  • < forms >
    • < localhost >
      • < user >
        • < login >

9. Registration form

  • Full name: the user's full name
  • E-mail address: the user's e-mail address
  • Desired username: the user's username
  • Password: a password for the account
  • Password check: retype the password

10. userform.xml registration form 1

  • < register >
    • < action > /app/web/register action >
    • < method > post method >
    • < name > registerForm name >
    • < elements >
      • < fullname >
        • < type > text type >
        • < options >
          • < label > Full name: label >
        • text
          • E-mail address:
  • text
          • Username:
        • password
          • Password:
        • password
          • Retype password:
      • ...

11. userform.xml registration form 2

  • ...
        • submit
          • Register
          • button

12. Login form

  • Username: the user's username
  • Password: a password for the account

13. userform.xml login form 1

  • < login >
    • < action > /app/web/auth action >
    • < method > post method >
    • < name > login Form name >
    • < elements >
  • text
          • Username:
        • password
          • Password:
      • < submit >
        • submit
          • Login
          • button
      • submit >
    • elements >

14. Resetpassword form

  • E-mail address: the user's e-mail address

15. userform.xml reset pwd form 1

  • < resetpassword >
    • < action > /app/web/resetpassword action >
    • < method > post method >
    • < name > resetpwd Form name >
    • < elements >
  • text
          • E-mail address:
        • submit
          • Reset password
          • button

16. UserController - getForm

  • We create a method getForm
  • public functiongetForm( $myForm )
  • {
    • $config=newZend_Config_Xml( '../app/config/forms.xml' ,'localhost' );
    • $form=newZend_Form( $config ->user-> $myForm );
    • return $form ;
  • }

17. UserController - registerAction

  • We create a method registerAction
  • public functionregisterAction()
  • {
    • $this ->view->form=$this ->getForm( 'register' );
  • }
  • And we put in the view script (register.phtml):

18. UserController - loginAction

  • We create a method loginAction
  • public functionloginAction()
  • {
    • $this ->view->form=$this ->getForm( 'login' );
  • }
  • And we put in the view script (login.phtml):

19. UserController - resetpassworAction

  • We create a method resetpasswordAction
  • public functionresetpasswordAction()
  • {
    • $this ->view->form=$this ->getForm( 'resetpassword' );
  • }
  • And we put in the view script (resetpassword.phtml):

20. Rendered register page 21. Rendered login page 22. Rendered reset password page 23. Source code of login page

  • < form name = "loginForm" id = "loginForm" enctype = "application/x-www-form-urlencoded" action = "/app/web/auth" method = "post" >
  • < dl class = "zend_form" >
  • < dt >< label for = "username" class = "optional" >Username: label > dt >
  • < dd >< input type = "text" name = "username" id = "username" value = "" > dd >
  • < dt >< label for = "password" class = "optional" >Password: label > dt >
  • < dd >< input type = "password" name = "password" id = "password" value = "" > dd >
  • < dt > dt >
  • < dd >< input type = "submit" name = "submit" id = "submit" value = "Login" class = "button" > dd >
  • dl >
  • form >
  • automated generation using
  • Zend_Form
  • Zend_Config_Xml

24. Validation and Filtering

  • Of course we need to validate input !
  • And we filter the input !
  • This is done easily in our configuration file

25. userform.xml login enhancement 26. New source code of login page

  • < form name = "loginForm" id = "loginForm" enctype = "application/x-www-form-urlencoded" action = "/app/web/auth" method = "post" >
  • < dl class = "zend_form" >
  • < dt >< label for = "username" class = "required" >Username: label > dt >
  • < dd >< input type = "text" name = "username" id = "username" value = "" > dd >
  • < dt >< label for = "password" class = "required" >Password: label > dt >
  • < dd >< input type = "password" name = "password" id = "password" value = "" > dd >
  • < dt > dt >
  • < dd >< input type = "submit" name = "submit" id = "submit" value = "Login" class = "button" > dd >
  • dl >
  • form >
  • automated generation using
  • Zend_Form
  • Zend_Config_Xml

27. Validation and Filtering details

  • All the validation and filtering options can be found athttp://framework.zend.com
  • Zend_Validate
  • Zend_Filter
  • Of course you can implement your own filters and validators.

28. More information

  • More information can be found at the Zend Framework websitehttp://framework.zend.com
  • The example files can be downloaded athttp://www.in2it.be/files/zend_form_example.zip

29. Thank you Questions ?