Creating Textarea Fields
Let's create a textarea, a widely used form element that allows developers to create an input field in which the user has ample amount of space to type in any number of characters. This is a great field if you want the user to type in large amounts of data, such as a personal biography or a short story. To add the element to the form, you use the Zend_Form_Element_TextArea class.
Let's use the new Zend_Form_Element_TextArea class to create the update form. The new action will create the update form that enables users to update their personal content stored in the application. You'll also add a new textarea field, About Me, in which users can enter data about themselves to display on their profile page.
Before creating the new action, you must create a new class that abstracts the way in which the application's password, username, and email text fields are created. The new class enables you to create these fields, along with their validators and filters, in a single location and share them between the signup and update forms. Create a new file called Elements.php and save it inside a new directory: application/models/Form. The complete code is shown in Listing 4-35.
Listing4-35. Form/Elements.php class
* Loudbite.com Form Elements.
class Elements {
* Create email text field
* @return Zend_Form_Element_Text
public function getEmailTextField() {
//Create Text Field Object.
$emailElement = new Zend_Form_Element_Text('email');
$emailElement->setLabel('Email:');
$emailElement->setRequired(true);
//Add Validator
$emailElement->addValidator(new Zend_Validate_EmailAddress()); //Add Filter
$emailElement->addFilter(new Zend_Filter_HtmfEntities()); $emailElement->addFilter(new Zend_Filter_StripTags());
return $emailElement;
* Create password text field
* @return Zend Form Element Password
public function getPasswordTextField() {
//Create Password Object.
$passwordElement = new Zend_Form_Element_Password('password');
$passwordElement->setLabel('Password:');
$passwordElement->setRequired(true);
//Add Validator
$passwordElement->addValidator (
new Zend_Validate_StringLength(6,20) );
//Add Filter
$passwordElement->addFilter(new Zend_Filter_HtmlEntities()); $passwordElement->addFilter(new Zend_Filter_StripTags());
return $passwordElement;
* Create username text field.
* @return Zend_Form_Element_Text
public function getUsernameTextField() {
$usernameElement = new Zend_Form_Element_Text('username');
$usernameElement->setLabel('Username:');
$usernameElement->setRequired(true);
//Add validator
$usernameElement->addValidator (
new Zend_Validate_StringLength(6, 20)
//Add Filter
$usernameElement->addFilter(new Zend_Filter_StripTags()); $usernameElement->addFilter(new Zend_Filter_HtmlEntities()); $usernameElement->addFilter(new Zend_Filter_StringToLower());
return $usernameElement;
The code shown in Listing 4-35 is the class Elements, which contains three public methods: getUsernameTextField(), getPasswordTextField(), and getEmailTextField(). Each of these methods creates a Zend_Form_Element object, sets the validators as well as any filters, and returns the object. The code is taken from the account sign-up form created earlier. Now open the AccountController.php file and create the update profile form, as shown in Listing 4-36.
Listing 4-36. Creating an Update Profile Form
* Update Form
private function getUpdateForm(){
//Create Form
$form->setAction('update');
$form->setAttrib('sitename', 'loudbite');
//Load Elements class require "Form/Elements.php"; $LoudbiteElements = new Elements();
//Create Username Field.
$form->addElement($LoudbiteElements->getUsernameTextField()); //Create Email Field.
$form->addElement($LoudbiteElements->getEmailTextField());
//Create Password Field.
$form->addElement($LoudbiteElements->getPasswordTextField()); //Create Text Area for About me.
$textAreaElement = new Zend_Form_Element_TextArea('aboutme'); $textAreaElement->setLabel('About Me:'); $textAreaElement->setAttribs(array('cols' => 15, 'rows' => 5));
$form->addElement($textAreaElement);
//Create a submit button. $form->addElement('submit', 'submit'); $submitElement = $form->getElement('submit'); $submitElement->setLabel('Update My Account');
return $form;
* Update the User's data.
public function updateAction() {
//Check if the user is logged in //Fetch the user's id //Fetch the users information
//Create the form.
//Check if the form has been submitted. //If so validate and process. if($_POST){
//Check if the form is valid.
$username = $form->getValue('username'); $password = $form->getValue('password'); $email = $form->getValue('email'); $aboutMe = $form->getValue('aboutme');
//Otherwise redisplay the form.
else{
//Otherwise display the form.
else{
Listing 4-36 contains the action updateAction() and a new private method: getUpdateForm(). The new private method creates the update form using the Elements class you created and also creates a new textarea form element by instantiating the Zend_Form_Element_Textarea, setting its label to About Me:, setting the cols and rows attributes using setAttribs(), and adding the element to the form. Once the form has been complete, you can return the Zend_Form object stored within the $form variable.
The second method, updateAction(), is the action called by the user to view the update form and when submitting any updates. The action begins by fetching the update form from the getUpdateForm() method. Because this action will also support both creating the form and processing any submitted data, you have to determine whether the form has been submitted. You do this by checking whether the $_POST variable has been set; if it is, validate the form. If the form has not been submitted, you know the user has just arrived and simply wants to see the form to begin the edits.
You now need a view for your updateAction() method: create a file under the views/scripts/account/ directory and save the file with the XHTMl shown in Listing 4-37.
Listing4-37. New update.phtml Page
<?php echo $this->doctype('XHTML1_STRICT'); ?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<?php echo $this->headTitle('LoudBite.com - Update Info'); ?> </head> <body>
<?php echo $this->render('includes/header.phtml') ?>
<h3>Update My Profile</h3> <?php echo $this->form; ?>
If you load the update form http://localhost/account/update and fill out the form, you will go through the complete update process, but your data will not be saved.
Post a comment