Code snippets for symfony 1.x

Navigation

Set create default value to filter defined value for a specific field

This is a very usefull and simple tip if you want to defined default value in edit form (only on creation of a new record) with the value that the user defined in the list filter.

An exemple for a product module which define the category select field with the value of the same field in the list filter.

action.class.php

  public function executeEdit ()
  {
    $filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/product/filters');
    if (!$this->getRequestParameter('product_id', 0) && isset($filters['category_id']))
    {
      $this->product = new Product();
      $this->produit->setCatergoryId($filters['category_id']);
    }
    parent::executeEdit();
  }
 
  protected function getProductOrCreate ($product_id = 'product_id')
  {
    if (isset($this->product)) return $this->product;
    else return parent::getProductOrCreate($product_id);
by Sylvain PAPET on 2006-10-20, tagged admin  filter  generator 

Comments on this snippet

gravatar icon
#1 Pierre Minnieur on 2006-11-07 at 05:34

This is a great thing. Very user friendly to support that.

gravatar icon
#2 Y. K. on 2008-01-02 at 02:08

change: protected function getProductOrCreate ($product_id = 'product_id') to: protected function getProductOrCreate ($product_id = 'id')

else: the getProductOrCreate method of the overloaded actions.class.php always runs into a "new" statement.

gravatar icon
#3 diego urrutibehety on 2009-03-19 at 04:21

the snippet has an error on line 7, i believe. it says produit instead of product