March 4th, 2009The Action View Helper

Situation:

I use Zend_Layout to create a global view template for my website and the view (.phtml) that corresponds to a controller/action will be loaded into the $this->layout->content variable automatically.

All good but what to do with a form in your global template?
Building the form (Zend_Form) into your view isn’t really a clean option and what about validation  of the form? There is no controller/action from where the form was build and so there is no controller/action where to post the form to.

Solution:

I read through the Zend manual and I discovered there was a view helper named ‘Action’.
What does it do?
It enables you to call a controller/action from within your view script. It will process the controller/action code, render the view and return it into your view script.

Example:

IndexController.php

classIndexControllerextendsZend_Controller_Action {

public function indexAction(){

}

public function formAction(){
//build a form
$form = new Zend_Form();

//create a text element
$elementName = new Zend_Form_Element_Text(’name’);
$element->setRequired(true);    //a validation saying our field has to be filled out

//create a submit button
$elementSubmit = new Zend_Form_Element_Submit(’submit’);
$elementSubmit->setLabel(’Send’);

//add elements to the form
$form->addElement($elementName);
$form->addElement($elementSubmit);

//if we click the ‘Send’ button
if($this->getRequest()->isPost()){
if($form->isValid()){
//form is valid
}
}

//add the form to a view variable
$this->view->form = $form;
}
}

index/form.phtml

<?php echo this->form; ?>

layout.phtml

<?php echo$this->doctype(Zend_View_Helper_Doctype::XHTML1_STRICT) . “\n”; ?>
<html>
<head>
</head>
<body>
<div id=”container”>
<?phpecho$this->layout()->content.”\n”;?>
</div>
<div>
<?php echo $this->action(’form’, ‘index’); ?>
</div>
</body>
</html>

parameter list of the view action

action($action, $controller, $module = null, array $params = array())

Result:

The form action of the index controller is called from the layout.phtml.
The business logic is executed, the view (index/form.phtml) is rendered and is returned to our layout.phtml.
When we press the ‘Send’ button in our form. The form will be submitted to index/form and the validation will happen.


No Tags

I created a form with Zend_Form adding 2 select boxes. One where all the states would be shown and  after selecting a state, it had to update a second select box with all the cities corresponding to the chosen state.

After I had build the form, I created the javascript code (JQuery) to get all the cities corresponding to the selected value of the states select box.

Now when I tried to submit the form, the validation on the form failed.
This was because Zend_Form automatically validates the submited values with the original values added to the select boxes.

Solution:
//cities is our select box created with Zend_Form_Element_Select
$cities->setRegisterInArrayValidator(false);

The validation on the select box ‘cities’ is turned off and you will be able to submit the form.


No Tags

January 15th, 2009Zend: Custom Plugins

The front controller in your bootstrap file (usually index.php) has a function registerPlugin() to register plugins. Plugins are just peaces of code you want to execute at a certain point (event) during the controller’ process lifetime.

There are 6 events (functions) you can use within your plugin. Every function has one parameter which is the request object:

  • routeStartup
  • routeShutdown
  • dispatchLoopStartup
  • preDispatch
  • postDispatch
  • dispatchLoopShutdown

The function I use the most is the preDispatch function. The preDispatch function can be used to alter the request information or perform some extra business logic before the request is dispatched.

CREATE YOUR CUSTOM PLUGIN

class MyPlugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
//perform some business logic or change the request object
}
}

EXAMPLE

This plugin makes sure that our response is sent with utf-8 encoding

class HeaderPlugin extends Zend_Controller_Plugin_Abstract{
public function preDispatch(Zend_Controller_Request_Abstract $request){
header(’Content-Type: text/html; charset=utf-8′);
}
}


No Tags


© 2007 labs.boulevart | iKon Wordpress Theme by TextNData | Powered by Wordpress | rakCha web directory