Simple Zend_Form Demo PRO

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;
?>

 

 

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences