X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fchatty;a=blobdiff_plain;f=lib%2FChatty%2FController%2FChat.pm;h=73fe2744e99c32ecfcfba61b6a222feabad8857c;hp=9be0e796e28449eefb4e31fbaad02c3e7cef997d;hb=b1babc5fbdaf0cfe573d1a126b9a6d88deb64352;hpb=6177b7c3d795a2505f95269ab2dd1d4c6f05a525 diff --git a/lib/Chatty/Controller/Chat.pm b/lib/Chatty/Controller/Chat.pm index 9be0e79..73fe274 100644 --- a/lib/Chatty/Controller/Chat.pm +++ b/lib/Chatty/Controller/Chat.pm @@ -7,6 +7,7 @@ BEGIN { extends 'Catalyst::Controller' } #__PACKAGE__->config(namespace => 'room'); use Chatty::Form::RoomCreate; +use Chatty::Form::MessageCreate; has 'roomcreate_form' => ( isa => 'Chatty::Form::RoomCreate', @@ -15,6 +16,13 @@ has 'roomcreate_form' => ( default => sub { Chatty::Form::RoomCreate->new } ); +has 'messagecreate_form' => ( + isa => 'Chatty::Form::MessageCreate', + is => 'rw', + lazy => 1, + default => sub { Chatty::Form::MessageCreate->new } +); + =head1 NAME Chatty::Controller::Chat - Catalyst Controller @@ -93,6 +101,26 @@ sub view :Chained(room) :PathPart('') :Args(1) { $c->detach('/missing') if !$c->stash->{room}; $c->stash(messages => [$c->model('DB::Message')->search(room => $room)]); + + $c->stash(form => $self->messagecreate_form); + + my $new_message = $c->model('DB::Message')->new_result({ + author => $c->user->obj->id, + room => $c->stash->{room}->id + }); + $self->messagecreate_form->process( + item => $new_message, + params => $c->req->params + ); + + if (!$self->messagecreate_form->is_valid) { + if ($c->req->method eq 'POST') { + $c->stash->{error} = "The form has a validation error. Try again..."; + } + return; + } + + $c->res->redirect($c->uri_for_action('/chat/view', $c->stash->{room}->id)); } =head1 AUTHOR