Code snippets for symfony 1.x


Refine Tags

Snippets tagged "admingenerator"

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.

      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
           // default publish 24 hours from now
      $post->setPublishTime(date('Y-n-j G:i', time() + 60*60*24 ));
      $post = PostPeer::retrieveByPk($this->getRequestParameter($id));
    return $post;
by Nathan Vonnahme on 2007-12-06, tagged admingenerator  create  default  form  value 

Quick Search for admin_double_list

Sometimes in the generator you have a large listing of elements in a admin_double_list.

Well I wanted to write a quick little autocomplete text box that would search for results and when you selected one, it would be added into the associated select box.

In your generator add a new partial: edit: display: [ _quick_search ]

Create a file in your templates directory called _quick_search.php

Copy this code in there:


<?php $response = sfContext::getInstance()->getResponse(); $response->addJavascript(sfConfig::get('sf_prototype_web_dir').'/js/prototype'); $response->addJavascript(sfConfig::get('sf_prototype_web_dir').'/js/effects'); $response->addJavascript(sfConfig::get('sf_prototype_web_dir').'/js/controls'); $response->addStylesheet(sfConfig::get('sf_prototype_web_dir').'/css/input_auto_complete_tag'); ?> <input type="text" id="quick_search" name="quick_search"/>

<div class="auto_complete" id="quick_search_auto_complete" style="position: absolute; left: 445px; top: 421px; width: 113px; opacity: 0.17329; display: none;">


<script type="text/javascript">


function addSelection (li)


$('associated_titles').options[ $('associated_titles').options.length] = new Option (li.childNodes[0].nodeValue,;


new Ajax.Autocompleter('quick_search', 'quick_search_auto_complete', '/backend_dev.php/search', { updateElement: addSelection });




Make sure you change your url from /backend_dev.php/search to whatever your action that will search your database and your good to go!

Just FYI: You can use the Symfony helper autocomplete_tag () because it does not allow for passing the updateElement parameter.

by Myke Hines on 2007-06-29, tagged admin  admindoublelist  admingenerator  ajax  generator