Code snippets for symfony 1.x


Snippets by user Nathan Vonnahme

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 

Generic action to save edit-in-place fields

One stumbler for me was that I didn't realize that the form created by the input_in_place_editor_tag() helper only submits one key/value pair (value=xxx), so to tell the action which PK to use or which object field to update, you have to pass that information on the form URL (field and id).

One way to solve it would be to write a different action for each field you want to update, but I wanted something I could reuse without writing additional actions, so I added two GET parameters:

So, in my showSuccess.php template:

<h1 id="eipTitle"><?php echo $comment->getTitle() ? $comment->getTitle() : "click to edit" ?></h1>
<?php echo input_in_place_editor_tag(
    'cols'   => 40,
    'rows'   => 1,

BTW, you can read about the valid options you can pass in the third argument to input_in_place_editor_tag() at the Wiki

And for the action, I made it as generic as possible (actually it would be nice if Propel generated an action like this next to the regular executeUpdate action):

private function raiseEipError ($message) 
    $this->logMessage($message, 'err');
    return $this->renderText( "ERROR:  $message" );
  public function executeAjaxUpdate()
    $id = $this->getRequestParameter('commentid');
    $field = $this->getRequestParameter('field');
    $this->logMessage("commentid:$id and field='$field'", 'debug');
    // Check for required params.  
    // Return a nice message to the user and in the log if there's a problem.
    if (! $field) 
      return $this->raiseEipError( "No field parameter passed in form action" );
    $valid_fields = CommentPeer::getFieldNames(BasePeer::TYPE_FIELDNAME);
    if (! in_array($field, $valid_fields)) 
      return $this->raiseEipError( "Invalid field parameter '$field' passed in form action.\n".
                                   " Valid fields: (" . implode(", ", $valid_fields) . ")" );
    if (! $id) 
      return $this->raiseEipError( "No ID parameter passed in form action" );
    $comment = CommentPeer::retrieveByPk($id);
    if (! $comment) 
      return $this->raiseEipError( "Object with ID $id not found" );
    $comment->setByName($field, $this->getRequestParameter('value'), BasePeer::TYPE_FIELDNAME);
    return $this->renderText( $comment->getByName($field, BasePeer::TYPE_FIELDNAME) );
by Nathan Vonnahme on 2007-05-04, tagged action  ajax  editinplace  form  inputinplaceeditortag  update