symfony admin generator – populating create form from filters

symfony has basic CRUD-like admin generator, which allows you to create controls for filtering list results. Naturally, if you have filtered list and press ‘create’ button it would be nice to have new form with populated from filter form fields.

Here’s basic idea how to do it. In my example I have Work entity which has foreign key to Category entity. I have list of works filterable by category id.
To populate category id on work creation form you need to override generated action:

class worksActions extends autoworksActions
{
  public function executeEdit()
  {
  	parent::executeEdit();
  	if ($this->getRequestParameter('action') == 'create')
  	{
  		$filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/work/filters');
  		if (isset($filters['category_id']))
  			$this->work->setCategoryId($filters['category_id']);
  	}
  }
}

Look into admin generated actions if you want to more clearly understand why we need such code. It is located at cache/admin/dev/modules/autoWorks/actions/actions.class.php

Tags: ,

AddThis Social Bookmark Button

One Response to “symfony admin generator – populating create form from filters”

  1. roberto Says:

    thanks!! actually this is a very neat tip!

Leave a Reply