Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "value"

Defining a default value for fields on the create form using admin generator

Say you want the "create" form to have default values in some of the fields.

Unfortunately, if you hardcode values like this in the edit section of generator.yml, it will break the edit form for existing objects.

  edit:
    fields:
      thing_field: {params:value=foo}  # WRONG
 

The solution is to override the getThingOrCreate method the admin generator generates in its actions.class.php file.

If you're dealing with a class/module named 'post', you can copy and paste the generated function out of cache/frontend/dev/modules/autoPost/templates/_edit_form.php into apps/frontend/modules/post/actions/actions.class.php and add the defaults like this:

protected function getPostOrCreate($id = 'id')
  {
    if (!$this->getRequestParameter($id))
    {
      $post = new Post();
      // add some default values
      $post->setThing('foo');
           // default publish 24 hours from now
      $post->setPublishTime(date('Y-n-j G:i', time() + 60*60*24 ));
    }
    else
    {
      $post = PostPeer::retrieveByPk($this->getRequestParameter($id));
 
      $this->forward404Unless($post);
    }
 
    return $post;
  }
 
by Nathan Vonnahme on 2007-12-06, tagged admingenerator  create  default  form  value