Formatting the Form

Continuing with the Zend_Form functionality, you now focus on element ordering and form decorating.

By default, the order of the elements is shown to the user in the same way as created in the code. Using the example shown in Listing 4-29, the username text field is initially created, followed by the email text field, the password text field, and finally the submit button. This same order is then shown in the view. To change the order, use the Zend_Form_Element setOrder() method.

The setOrder() method accepts a single integer parameter, from 1 to an arbitrary high number. The numerical value passed into the method represents the order in which the element will be displayed. If you set the value to 1 for the username text field, 2 for password, and 3 for email, the form will display the elements in that order. This is a great feature if you require your form to display each element in a different order based on previous user input on your site.

Open the AccountController.php file once again and make the modifications shown in Listing 430.

Listing 4-30. Setting the Order in the AccountController

* Account Sign Up.

public function newAction(){

//Create Form


$form->setDescription("sign up form");

$form->setAttrib('sitename', 'loudbite');

//Add Elements //Create Username Field. $form->addElement('text', 'username'); $usernameElement = $form->getElement('username'); $usernameElement->setLabel('Username:'); $usernameElement->setOrder(1);

//Create Email Field. $form->addElement('text', 'email'); $emailElement = $form->getElement('email'); $emailElement->setLabel('Email:'); $emailElement->setOrder(3);

//Create Password Field. $form->addElement('password', 'password'); SpasswordElement = $form->getElement('password'); $passwordElement->setLabel('Password:'); SpasswordElement->setOrder(2);

$form->addElement('submit', 'submit'); SsubmitButton = $form->getElement('submit'); $submitButton->setLabel('Create My Account!'); $submitButton->setOrder(4);

//Add the form to the view $this->view->form = $form;

Reloading the URL http://localhost/account/new will give you an updated form with updated ordering.

■ Note A form usually loops through its elements and wraps them in its <form> tag in the order you specify. If you want to change this order, you can use setOrder(), as I have done here.

Was this article helpful?

0 0

Post a comment