Programmer's Reference Guide
| Create a Model and Database Table |
Create A Form
For our guestbook to be useful, we need a form for submitting new entries.
Our first order of business is to create the actual form class. To create the empty form class, execute:
- % zf create form Guestbook
- Creating a form at application/forms/Guestbook.php
- Updating project profile '.zfproject.xml'
This will create the directory application/forms/ with the classfile Guestbook.php. Open that file and update it so it reads as follows:
- // application/forms/Guestbook.php
- class Application_Form_Guestbook extends Zend_Form
- {
- public function init()
- {
- // Set the method for the display form to POST
- $this->setMethod('post');
- // Add an email element
- 'label' => 'Your email address:',
- 'required' => true,
- 'EmailAddress',
- )
- ));
- // Add the comment element
- 'label' => 'Please Comment:',
- 'required' => true,
- )
- ));
- // Add a captcha
- 'label' => 'Please enter the 5 letters displayed below:',
- 'required' => true,
- 'captcha' => 'Figlet',
- 'wordLen' => 5,
- 'timeout' => 300
- )
- ));
- // Add the submit button
- 'ignore' => true,
- 'label' => 'Sign Guestbook',
- ));
- // And finally add some CSRF protection
- 'ignore' => true,
- ));
- }
- }
The above form defines five elements: an email address field, a comment field, a CAPTCHA for preventing spam submissions, a submit button, and a CSRF protection token.
Next, we will add a signAction() to our GuestbookController which will process the form upon submission. To create the action and related view script, execute the following:
- % zf create action sign Guestbook
- Creating an action named sign inside controller
- at application/controllers/GuestbookController.php
- Updating project profile '.zfproject.xml'
- Creating a view script for the sign action method
- at application/views/scripts/guestbook/sign.phtml
- Updating project profile '.zfproject.xml'
As you can see from the output, this will create a signAction() method in our controller, as well as the appropriate view script.
Let's add some logic into our guestbook controller's sign action. We need to first check if we're getting a POST or a GET request; in the latter case, we'll simply display the form. However, if we get a POST request, we'll want to validate the posted data against our form, and, if valid, create a new entry and save it. The logic might look like this:
- // application/controllers/GuestbookController.php
- class GuestbookController extends Zend_Controller_Action
- {
- // snipping indexAction()...
- public function signAction()
- {
- $request = $this->getRequest();
- $form = new Application_Form_Guestbook();
- if ($this->getRequest()->isPost()) {
- if ($form->isValid($request->getPost())) {
- $comment = new Application_Model_Guestbook($form->getValues());
- $mapper = new Application_Model_GuestbookMapper();
- $mapper->save($comment);
- return $this->_helper->redirector('index');
- }
- }
- $this->view->form = $form;
- }
- }
Of course, we also need to edit the view script; edit application/views/scripts/guestbook/sign.phtml to read:
- <!-- application/views/scripts/guestbook/sign.phtml -->
- Please use the form below to sign our guestbook!
- <?php
- $this->form->setAction($this->url());
Note: Better Looking Forms
No one will be waxing poetic about the beauty of this form anytime soon. No matter - form appearance is fully customizable! See the decorators section in the reference guide for details.
Additionally, you may be interested in our tutorial on form decorators.
Note: Checkpoint
Now browse to "http://localhost/guestbook/sign". You should see the following in your browser:
| Create a Model and Database Table |


Comments
Fatal error: Class 'Application_Form_Guestbook' not found in C:\Program Files\Zend\Apache2\htdocs\zendexamplecmd\application\controllers\GuestbookController.php on line 21
although the class 'Application_Form_Guestbook' was definitely in application\forms\Guestbook.php
I opened Zend Studio and created a project using File->New->Example...->Zend Framework->Create project at existing location (from existing source), and navigated to the location of the folder created with zf from the command line. It added a few files and changed some (e.g. the class 'Application_Form_Guestbook' in application\forms\Guestbook.php is called 'Default_Form_Guestbook' now).
Everything works fine now, including the Sign action. I know it is not really a solution as the project should work without Zend Studio as well, but it solved the problem.
Please use the form below to sign our guestbook!
Fatal error: Call to undefined function ctype_space() in /var/www/quickstart/library/Zend/Text/Figlet.php on line 467
I checked /application/controllers/GuestbookController.php, because I thought I come from there, but all is ok.
I would be happy if anyone can help me to solve this error.
I just fixed the error. 'ctype' was not installed. So I installed it.
Either fix the code or rewrite the tut.
Here is the error.
Fatal error: Class 'Application_Form_Guestbook' not found in /Applications/MAMP/htdocs/quickstart/application/controllers/GuestbookController.php on line 15
I also verified that the class 'Application_Form_Guestbook' was definitely in application\forms\Guestbook.php
I am running this on the 1.10 framework.
Fatal error: Call to a member function search() on a non-object in C:\Program Files (x86)\Zend\ZendServer\share\ZendFramework\library\Zend\Tool\Project\Provider\Form.php on line 70
and just lost it. If you cant keep the quickstart guide working either the tests are bad, the features evolve too fast/uncontrolled or something else is just way off. How should someone consider the serious integration and usage of ZF if he needs to debug even at the very basic steps you try to guide him through?
Well maybe i ll find some time left to debug the errors and contribute the fixes but for now i just give up. That alone saddens me as i was really looking forward to the experience.
$loader = new Zend_Loader_Autoloader_Resource (array (
'basePath' => APPLICATION_PATH,
'namespace' => 'Application',
));
Now you can use the loader to set your modules for the current namespace:
$loader -> addResourceType ( 'model', 'models', 'Model');
$loader -> addResourceType ( 'form', 'forms', 'Form');
Now you will be able to load the forms in the sign page. This also explains the solution from Zsolt Monar posted above. If no namespace is loaded by the autoloader then zend will fallback to the default namespace. Renaming the form and model class to Default_ would work as well.