Yearly Archives: 2011


Reset button doesn’t work on selected=”selected” form element

Using Zend_Form, it generated a select element where the selection option had an attribute of selected=”selected” instead of just adding “selected” to the element. The form’s reset button no longer works on this element. Because it’s being generated, I can’t simply change how the attribute is specified in the element, so I used some JavaScript to reset all the form elements when a reset button is clicked. If you have multiple forms and multiple reset buttons, then you’ll need to modify this to point at a specific form’s elements rather than all on a page.

 $("input:reset").click(function(event) {
	 event.preventDefault();
 
	// Whitelist of fields to minimize unintended side effects.
	$(':text, :password, :file, SELECT', 'form').val('');
 
	// De-select any checkboxes, radios and drop-down menus
	$(':input').removeAttr('checked').removeAttr('selected');
	$("select").find('option').removeAttr("selected");
 });

Zend Form inline errors not displaying 1

If the inline errors from zend validator are not displaying after submitting your form, you may need to add the “Errors” decorator to the form like

//apply decorators to each element.
foreach ($elements as $fieldName => $element) {
       	$element->setDecorators(array('ViewHelper','Errors',array()));
}

Also be sure the validation is taking place on your form after submission. The controller should look something like this (use the isValid() method)

public function createNewUserAction()
{
$form = new User_Form_CreateUserForm();
if ($this->getRequest()->isPost()) {
	if ($form->isValid($this->getRequest()->getPost())) {
		$params = $form->getValues();
 
		//save the user
 
		$this->view->messages = array('User successfully saved.');
		$this->_redirect('user/user-admin');
	}
}
$this->view->form = $form;
}

Netflix Price Increase

We’ve seen this one coming for months and today it finally happened. Netflix is increasing prices again, and why not? With 24 million subscribers, putting their current monthly revenue into the $300 million dollar range, increasing prices by 60% will help pay for a few people’s extra vacations. To be fair, the netflix price hike is mostly due to the drastic licensing costs from the film studios that provide the videos.

The $9.99 per month combined DVD rental and streaming plan will be gone on Sept 1 2011 for existing subscribers and immediately for new subscribers. The services have been broken up into $7.99 per month for streaming and $7.99 per month for a single DVD rental, or you may check out two discs at a time for $11.99 per month. What do you think about the netflix price increase?


Zend Framework: Action Helper by name Agent not found

I came across the following error today while reorganizing my zend framework application:
“Action Helper by name Agent not found”. This error occurred when I tried to use an action helper in a controller file. When it looked for the “redirector” helper, it decided to look for a helper named “_agent” instead.

How did I get here? Previously on this site, I was not using Zend_Auth, so I called php’s session_start() in the index file. I had to remove session_start() because I implemented Zend_Auth in one of the modules today. Apparently, helpers do not work if the session has not been started. Start the session by whichever method you choose and the error along with the missing helper issue will go away.


Zend Framework – how to autoload forms within a module 6

I got to thinking that it didn’t make sense to have a common /forms directory that would share forms between two modules when the forms would not be shared. A /forms directory within the specific module directory seemed more appropriate. The documentation on this isn’t very clear, but after some digging and trial and error I figured it out.

First, the directory structure.  Notice the location of the forms directories.

/Application

/modules

/default

/controllers

/forms

/views

/admin

/controllers

/forms

/views

First, add the two below lines to your application.ini file.

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""

Second, create a boostrap file for each module.  The file, named Bootstrap.php, should be placed in the root of the module directory and the class name should be {module name}_Boostrap. This bootstrap file will cause zend framework to automatically add the new forms directory to the autoloader.

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap {}

Third, add a form class to the /forms directory. A login form would have a filename of Login.php and a class name of {module name}_Form_Login.

class Admin_Form_Login extends Zend_Form

Lastly, call your form from a controller file from within the same module.

$form = new Admin_Form_Login();

Be careful if you’re adding a Bootstrap.php file to the default module! From Zend:

The reason that the default module is skipped is because the documented use case – particularly how we setup an application in Zend_Tool – is that the default module is directly beneath application/ and contains the application bootstrap. If we then allow loading the default module’s bootstrap… we get into a recursive loop.