![]() |
|
Snippets |
|
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; }
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:
===CODE===
<?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;">
</div>
<script type="text/javascript">
//<![CDATA[
function addSelection (li)
{
$('associated_titles').options[ $('associated_titles').options.length] = new Option (li.childNodes[0].nodeValue, li.id);
}
new Ajax.Autocompleter('quick_search', 'quick_search_auto_complete', '/backend_dev.php/search', { updateElement: addSelection });
//]]>
</script>
===CODE===
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.