I have recently created a simple demo for using the Zend_Form. In order to keep it simple I chose to avoid using decorators. You can download the files and place them on your own server.

The demo provides us with a simple BMI calculator. The user get to see a simple form where he should enter his weight and height. Once he presses the calculate button he will get his body mass index.

The demo includes one controller that has two actions. The inputdata action is for showing the form. The calculate action is for calculating the body mass index based on the  values sent.

The following code is of the controller. You can find two separated functions.

<?php

class BmiController extends Zend_Controller_Action
{
    public function inputdataAction()
    {
        $this->view->form = new BMIForm();	     	   
    }      
    public function calculateAction()
    {
	$form = new BMIForm();
        $data  = $this->_request->getPost();
	if($form->isValid($_POST)) 
	{
		$height = $data['height'];
		$weight = $data['weight'];
		$str = "bmi is ".($weight/($height*$height));
		$this->view->text = $str;	
	}
	else
	{
		$str = "error in data";
		$this->view->text = $str;
	}
	
    }
}

class BMIForm extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');
	$this->setAction('calculate');
	//creating weight text field
        $weight = $this->createElement('text','weight');
        $weight->setLabel('Weight:')->setAttrib('size',30);
	$weight->addValidator(new Zend_Validate_Digits());
        //creating height text field
	$height = $this->createElement('text','height');
        $height->setLabel('Hight:')->setAttrib('size',30);
	$height->addValidator(new Zend_Validate_Digits());
	//adding weight and height text fields
        $this->addElements(array($weight,$height));
	$this->addElement('submit','calculate');
    }
}

?>

The pthml file that serves as the view of the inputdata action is the following.

<?php
echo $this->form;
?>

The phtml file that serves as the view of the calculate action is the following:

<?php
echo $this->text;
?>

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *