Create A FormFor 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:
This will create the directory application/forms/ with the classfile Guestbook.php. Open that file and update it so it reads as follows:
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:
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:
Of course, we also need to edit the view script; edit application/views/scripts/guestbook/sign.phtml to read:
|