From e872d7b7c02a1006e4481271d902ad1ca1e0b8d4 Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Thu, 13 Oct 2011 18:42:19 -0600 Subject: [PATCH] switch to FormHandler for validation --- lib/Chatty/Controller/Root.pm | 64 ++++++++++++++++++++++------- lib/Chatty/Form/Login.pm | 12 ++++++ lib/Chatty/Form/Register.pm | 20 +++++++++ lib/Chatty/Schema/Result/Account.pm | 18 +++++--- lib/Chatty/Schema/Result/Message.pm | 6 +-- root/static/css/common.css | 25 ++--------- root/tt/login.tt | 7 +++- root/tt/register.tt | 3 ++ 8 files changed, 108 insertions(+), 47 deletions(-) create mode 100644 lib/Chatty/Form/Login.pm create mode 100644 lib/Chatty/Form/Register.pm diff --git a/lib/Chatty/Controller/Root.pm b/lib/Chatty/Controller/Root.pm index 14aef87..ab0bfcd 100644 --- a/lib/Chatty/Controller/Root.pm +++ b/lib/Chatty/Controller/Root.pm @@ -10,6 +10,23 @@ BEGIN { extends 'Catalyst::Controller' } # __PACKAGE__->config(namespace => ''); +use Chatty::Form::Login; +use Chatty::Form::Register; + +has 'login_form' => ( + isa => 'Chatty::Form::Login', + is => 'rw', + lazy => 1, + default => sub { Chatty::Form::Login->new } +); + +has 'register_form' => ( + isa => 'Chatty::Form::Register', + is => 'rw', + lazy => 1, + default => sub { Chatty::Form::Register->new } +); + =head1 NAME Chatty::Controller::Root - Root Controller for Chatty @@ -38,21 +55,24 @@ Allow a user to login. sub login :Local :Args(0) { my ($self, $c) = @_; - if ($c->req->method eq 'POST' && exists($c->req->params->{handle})) { - eval { - if ($c->authenticate({ - username => $c->req->params->{handle}, - password => $c->req->params->{password} - })) { - $c->change_session_id; - my $user = $c->user->get('username'); - $c->flash->{message} = "Hi, $user! You are now logged in."; - $c->response->redirect($c->uri_for('/')); - } - else { - $c->flash->{error} = "Log-in failed! Try again, I guess."; - $c->response->redirect($c->uri_for('login')); - } + + $c->stash(form => $self->login_form); + $self->login_form->process($c->req->params); + return unless $self->login_form->is_valid; + + eval { + if ($c->authenticate({ + username => $self->login_form->value->{username}, + password => $self->login_form->value->{password} + })) { + $c->change_session_id; + my $user = $c->user->get('username'); + $c->flash->{message} .= "Hi, $user! You are now logged in."; + $c->response->redirect($c->uri_for('/')); + } + else { + $c->flash->{error} = "Log-in failed! Try again, I guess."; + $c->response->redirect($c->uri_for('login')); } } } @@ -79,6 +99,20 @@ Register a new account. =cut sub register :Local :Args(0) { + my ($self, $c) = @_; + + $c->stash(form => $self->register_form); + + my $new_account = $c->model('DB::Account')->new_result({}); + $self->register_form->process( + item => $new_account, + params => $c->req->params + ); + + return unless $self->register_form->is_valid; + + $c->flash->{message} = "Registration complete. "; + $c->forward('login'); } =head2 default diff --git a/lib/Chatty/Form/Login.pm b/lib/Chatty/Form/Login.pm new file mode 100644 index 0000000..aa25b1d --- /dev/null +++ b/lib/Chatty/Form/Login.pm @@ -0,0 +1,12 @@ +package Chatty::Form::Login; + +use HTML::FormHandler::Moose; +extends 'HTML::FormHandler'; + +has_field 'username' => (label => 'Username', required => 1); +has_field 'password' => (type => 'Password', required => 1); +has_field 'submit' => (type => 'Submit', value => 'Login'); + +no HTML::FormHandler::Moose; +__PACKAGE__->meta->make_immutable; +1; diff --git a/lib/Chatty/Form/Register.pm b/lib/Chatty/Form/Register.pm new file mode 100644 index 0000000..8532815 --- /dev/null +++ b/lib/Chatty/Form/Register.pm @@ -0,0 +1,20 @@ +package Chatty::Form::Register; + +use HTML::FormHandler::Moose; +extends 'HTML::FormHandler::Model::DBIC'; + +has '+item_class' => (default => 'Account'); + +has_field 'email' => (type => 'Email', label => 'Email address'); +has_field 'username' => (label => 'User Nickname', required => 1, unique => 1); +has_field 'password' => (type => 'Password', required => 1); +has_field 'password_confirm' => (type => 'PasswordConf', required => 1); +has_field 'submit' => (type => 'Submit', value => 'Register'); + +has '+unique_messages' => (default => sub { + {username => 'Username is already registered'}; + }); + +no HTML::FormHandler::Moose; +__PACKAGE__->meta->make_immutable; +1; diff --git a/lib/Chatty/Schema/Result/Account.pm b/lib/Chatty/Schema/Result/Account.pm index 1589333..8e55393 100644 --- a/lib/Chatty/Schema/Result/Account.pm +++ b/lib/Chatty/Schema/Result/Account.pm @@ -11,7 +11,7 @@ use MooseX::NonMoose; use namespace::autoclean; extends 'DBIx::Class::Core'; -__PACKAGE__->load_components("InflateColumn::DateTime"); +__PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp"); =head1 NAME @@ -29,6 +29,11 @@ __PACKAGE__->table("account"); is_auto_increment: 1 is_nullable: 0 +=head2 email + + data_type: 'text' + is_nullable: 1 + =head2 username data_type: 'text' @@ -37,7 +42,7 @@ __PACKAGE__->table("account"); =head2 password data_type: 'text' - is_nullable: 1 + is_nullable: 0 =head2 status @@ -50,14 +55,17 @@ __PACKAGE__->table("account"); __PACKAGE__->add_columns( "id", { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, + "email", + { data_type => "text", is_nullable => 1 }, "username", { data_type => "text", is_nullable => 1 }, "password", - { data_type => "text", is_nullable => 1 }, + { data_type => "text", is_nullable => 0 }, "status", { data_type => "text", default_value => "active", is_nullable => 1 }, ); __PACKAGE__->set_primary_key("id"); +__PACKAGE__->add_unique_constraint("username_unique", ["username"]); =head1 RELATIONS @@ -77,8 +85,8 @@ __PACKAGE__->has_many( ); -# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-10-12 22:20:29 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:D8HUHJmSfJwylSeDYjfeHA +# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-10-13 16:46:39 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:pPJdUbHgHvUo4FxblDaJ2g # You can replace this text with custom code or comments, and it will be preserved on regeneration diff --git a/lib/Chatty/Schema/Result/Message.pm b/lib/Chatty/Schema/Result/Message.pm index 56ea868..796a797 100644 --- a/lib/Chatty/Schema/Result/Message.pm +++ b/lib/Chatty/Schema/Result/Message.pm @@ -11,7 +11,7 @@ use MooseX::NonMoose; use namespace::autoclean; extends 'DBIx::Class::Core'; -__PACKAGE__->load_components("InflateColumn::DateTime"); +__PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp"); =head1 NAME @@ -82,8 +82,8 @@ __PACKAGE__->belongs_to( ); -# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-10-12 22:20:29 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dORhf3WubIeixtSujgUgrg +# Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-10-13 16:46:39 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dgMXiWuIhmCQeExkpcxorA # You can replace this text with custom code or comments, and it will be preserved on regeneration diff --git a/root/static/css/common.css b/root/static/css/common.css index ff89b5b..023d30e 100644 --- a/root/static/css/common.css +++ b/root/static/css/common.css @@ -23,7 +23,7 @@ a:hover { } label { - width: 100px; + width: 150px; float: left; } @@ -41,7 +41,7 @@ label { #outer { margin: 0 auto 0 auto; - width: 640px; + width: 720px; border: 2px solid black; background: #99c; } @@ -92,25 +92,6 @@ label { /* error message */ .error { - /* supply height to ensure consistent positioning for every browser */ - height:15px; - background-color:#FFFE36; - border:1px solid #E1E16D; - font-size:11px; - color:#000; - padding:3px 10px; - margin-left:-2px; - - - /* CSS3 spicing for mozilla and webkit */ - -moz-border-radius:4px; - -webkit-border-radius:4px; - -moz-border-radius-bottomleft:0; - -moz-border-radius-topleft:0; - -webkit-border-bottom-left-radius:0; - -webkit-border-top-left-radius:0; - - -moz-box-shadow:0 0 6px #ddd; - -webkit-box-shadow:0 0 6px #ddd; + background: #c66; } diff --git a/root/tt/login.tt b/root/tt/login.tt index d6ad93c..3720e26 100644 --- a/root/tt/login.tt +++ b/root/tt/login.tt @@ -1,6 +1,7 @@ [% META title = 'Log In' -%]

Log In

[% IF ! c.user_exists -%] + +[% form.render %] +

Register

-If you haven't yet registered, go do that now! +If you don't already have an account, go register!

[% ELSE -%]

You are already logged in.

diff --git a/root/tt/register.tt b/root/tt/register.tt index 9c15f92..c73e161 100644 --- a/root/tt/register.tt +++ b/root/tt/register.tt @@ -7,6 +7,7 @@ $('#form button[type=reset]').click(function() { [% END -%]

Register

[% IF ! c.user_exists -%] + +[% form.render %] [% ELSE -%]

You are already registered and logged in. There is no need to register again.

[% END -%] -- 2.45.2