switch to FormHandler for validation
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Fri, 14 Oct 2011 00:42:19 +0000 (18:42 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Fri, 14 Oct 2011 00:42:19 +0000 (18:42 -0600)
lib/Chatty/Controller/Root.pm
lib/Chatty/Form/Login.pm [new file with mode: 0644]
lib/Chatty/Form/Register.pm [new file with mode: 0644]
lib/Chatty/Schema/Result/Account.pm
lib/Chatty/Schema/Result/Message.pm
root/static/css/common.css
root/tt/login.tt
root/tt/register.tt

index 14aef8799fc1ae8628c54dffd5818b414654e5b6..ab0bfcd8ee1e07d6f48e1cbad556553552f10e30 100644 (file)
@@ -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 (file)
index 0000000..aa25b1d
--- /dev/null
@@ -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 (file)
index 0000000..8532815
--- /dev/null
@@ -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;
index 1589333de1493d2947b0c62d960bb54e4a6ba2a8..8e55393f2f67a0c5bc92729a19bee7cd92d34001 100644 (file)
@@ -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
index 56ea8681f821e0fc64b9cae43a33f685484c55c6..796a7978a69e447da07fb6307c6556b5da01e57e 100644 (file)
@@ -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
index ff89b5b6774fbf8ac726a2936b43eb29ad32de8f..023d30e255d13b47b6d25ead20702370d25f5dad 100644 (file)
@@ -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;
 }
 
index d6ad93cecbdd06b4975c35d8f1926c48f3a9b0bb..3720e2611122f7c388e1fd3c5856a8cf2c07a8bd 100644 (file)
@@ -1,6 +1,7 @@
 [% META title = 'Log In' -%]
 <h2>Log In</h2>
 [% IF ! c.user_exists -%]
+<!--
 <form action="[% c.uri_for('login')  %]" method="post">
        <fieldset>
                <p>
                <input type="submit" value="Login">
        </fieldset>
 </form>
-<h3>Don't have an account?</h3>
+-->
+[% form.render %]
+<h3>Register</h3>
 <p>
-If you haven't yet registered, <a href="[% c.uri_for('register') %]">go do that now</a>!
+If you don't already have an account, <a href="[% c.uri_for('register') %]">go register</a>!
 </p>
 [% ELSE -%]
 <p>You are already logged in.</p>
index 9c15f929c4b61ec2dea62c7548c241683640dc35..c73e161a41964e920b9cfa05c2d95d2838cf33ea 100644 (file)
@@ -7,6 +7,7 @@ $('#form button[type=reset]').click(function() {
 [% END -%]
 <h2>Register</h2>
 [% IF ! c.user_exists -%]
+<!--
 <form id="form" action="[% c.uri_for('register') %]" method="post">
        <fieldset>
                <p>
@@ -29,6 +30,8 @@ $('#form button[type=reset]').click(function() {
                <button type="reset">Reset</button>
        </fieldset>
 </form>
+-->
+[% form.render %]
 [% ELSE -%]
 <p>You are already registered and logged in.  There is no need to register again.</p>
 [% END -%]
This page took 0.038733 seconds and 4 git commands to generate.