Tuesday, March 23, 2010

HTML::FormFu select

When using a FormFu select for a simple relation it is easy to display other item names than the default:

The element looks like:
{
label => 'collection',
type => 'Select',
name => 'collection_id',
options => [ map { [ $_->id, $_->collection . ' - ' . $_->product->product ] }
$c->model('PB::Collections')->search( {}, {order_by => 'product.product, collection', prefetch => 'product'} )->all() ],
constraints => [ 'Required' ],
},
But when using a many to many relation in the listbox it becomes trickier, you have to add an accessor to the result class:

The element looks like:
{
label => 'items',
type => 'Select',
name => 'items',
db => {
model => 'PB::Items',
label_column => 'description',
m_to_m_column => 'item_id',
attributes => { prefetch => [ {book => 'title' }, {book => 'collection' }, {book => 'format' } ] , order_by => 'title.title, collection.collection, format.format ' },
},
multiple => 1,
size => 20,
},

And the following sub has to be added to the items resultset:
sub description {
$_[0]->book->title->title . ' - ' . $_[0]->book->collection->collection . ' - ' . $_[0]->book->format->format . ' - ' . $_[0]->isbn;
}


No comments: