- 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.
- 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 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
-
-
- config/-> this is where you put the configuration files
-
-
- controllers/-> this is where you put your controllers
-
-
- views/-> this is where you put your view templates
-
-
-
-
- login.phtml, register.phtml, resetpassword.phtml
-
-
- index.php-> your bootstrap file
-
-
- 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)
- knowledge of XML or INI structures
- a site already using Zend Framework MVC
7. Example description
- We're going to build the following forms:
8. userform.xml global structure
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
-
- < action > /app/web/register action >
-
- < name > registerForm name >
-
-
-
-
- < label > Full name: label >
11. userform.xml registration form 2
12. Login form
- Username: the user's username
- Password: a password for the account
13. userform.xml login form 1
-
- < action > /app/web/auth action >
-
- < name > login Form name >
14. Resetpassword form
- E-mail address: the user's e-mail address
15. userform.xml reset pwd form 1
-
- < action > /app/web/resetpassword action >
-
- < name > resetpwd Form name >
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 );
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 >
- < dd >< input type = "submit" name = "submit" id =
"submit" value = "Login" class = "button" > dd >
- automated generation using
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 >
- < dd >< input type = "submit" name = "submit" id =
"submit" value = "Login" class = "button" > dd >
- automated generation using
27. Validation and Filtering details
- All the validation and filtering options can be found
athttp://framework.zend.com
- 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 ?