Dec 14

Custom values in sfWidgetFormDoctrineChoice

Lets consider sfWidgetFormDoctrineChoice form select object with multiple elements:

'books' => new sfWidgetFormDoctrineChoice(array('model' => 'Book', 'expanded' => true, 'multiple' => true)),

So it will obviously take all records from book table and show it as multiple list of checkboxes.
But what if you don’t need to show all values for selection (e.g. you want to show only new books or perform any other kind of filtering). sfWidgetFormDoctrineChoice object provides useful methods which help to handle such situations, e.g.

'books' => new sfWidgetFormDoctrineChoice(array('model' => 'Book', 'table_method' => 'getBooks', 'expanded' => true, 'multiple' => true)),

So we just specified which records should be taken using getBooks method.
Now we should add this method to (attention!) autogenerated table class in lib/model folder:


  public function getBooks() 
    return Doctrine_Query::create()
      ->andWhere('created_at = ? ', date('Y-m-d'))

Worth to mention that sfWidgetFormDoctrineChoice provides other methods eg to change key method, change string method, etc.

Author: symfonian


Massimiliano Arione
December 14, 2010

It’s also worth to mention that using ‘query’ option instead of ‘table_method’ is likely a more elegant solution.

December 17, 2010

Massimiliano, thanks for your comment. Both post and your comment was very helpful for me !

Comments RSS TrackBack Identifier URI

Leave a comment