Hi,
Sorry for the shakey subject title, but that's the best way I can
think of to sumarise. Anyway, in my project at the moment I often have
forms where the user is moving something, and needs to first search
for a destination. An example is changing the track artist: first the
user searches for a new artist, then they enter this edit with a
moderation note.
My current solution, refactored as best as I could, is the following:
# ----
package Foo::Search;
sub filter_artist : Private
{
my ($self, $c) = @_;
my $form = $c->form(undef, 'Search::Query');
return unless $c->form_posted && $form->validate($c->req->params);
my $artists = $c->model('Artist')->direct_search($form->value('query'));
$c->stash->{artists} = $artists;
}
package Foo::Track;
sub change_artist : Chained('track')
{
my ($self, $c) = @_;
$c->forward('/user/login');
$c->forward('/search/filter_artist');
}
sub confirm_change_artist : Chained('track') Args(1)
{
my ($self, $c, $new_artist) = @_;
$c->forward('/user/login');
my $track = $c->stash->{track};
my $new_artist = $c->model('Artist')->load($new_artist);
$c->stash->{new_artist} = $new_artist;
my $form = $c->form($track, 'Track::ChangeArtist');
$form->context($c);
return unless $c->form_posted && $form->validate($c->req->params);
# Database edit here:
$form->change_artist($new_artist);
my $release = $c->model('Release')->load($track->release);
$c->response->redirect($c->entity_url($release, 'show'));
}
# ----
The user would visit change_artist and be presented with a search
form. They submit back to change_artist, and selecting an artist
navigates them to confirm_change_artist, with a new form.
Is this a sensible approach to this problem? As I said, I have a fair
few actions that follow this idiom of searching for an artist before
entering the moderation - so I've tried to do it with the least
duplication possible.
Any advice on this (or my code in general I suppose!) will be greatly
appreciated :)
- Ollie
_______________________________________________
List: [email protected]
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/