Final minor pass of a full registration form on the website
This commit is contained in:
parent
704f429fa5
commit
1bb483bb5a
4 changed files with 108 additions and 20 deletions
|
@ -1,32 +1,86 @@
|
||||||
package Pear::LocalLoop::Controller::Register;
|
package Pear::LocalLoop::Controller::Register;
|
||||||
use Mojo::Base 'Mojolicious::Controller';
|
use Mojo::Base 'Mojolicious::Controller';
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
|
||||||
|
has error_messages => sub {
|
||||||
|
return {
|
||||||
|
name => 'Full Name is required',
|
||||||
|
email => 'Email Address is required, and must be a valid address that is not already registered',
|
||||||
|
password => 'Password is required, and must match the Confirmation field',
|
||||||
|
postcode => 'Postcode is required, and must be a valid UK Postcode',
|
||||||
|
token => 'Token is required, and must be a valid, unused token',
|
||||||
|
agerange => 'Age Range is required, and must be a selection from the drop-down',
|
||||||
|
unknown => 'Sorry, there was a problem registering!',
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
sub index {
|
sub index {
|
||||||
my $c = shift;
|
my $c = shift;
|
||||||
|
|
||||||
my $agerange_rs = $c->schema->resultset('AgeRange');
|
my $agerange_rs = $c->schema->resultset('AgeRange');
|
||||||
$agerange_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
|
$agerange_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
|
||||||
$c->stash( ageranges => [ $agerange_rs->all ] );
|
$c->stash( ageranges => [ $agerange_rs->all ], form_data => {}, error => undef );
|
||||||
}
|
}
|
||||||
|
|
||||||
sub register {
|
sub register {
|
||||||
my $c = shift;
|
my $c = shift;
|
||||||
my $validation = $c->validation;
|
my $validation = $c->validation;
|
||||||
|
|
||||||
$validation->required('name', 'trim');
|
$validation->required('name');
|
||||||
$validation->required('email')->email;
|
|
||||||
$validation->required('password')->equal_to('password2');
|
$validation->required('password')->equal_to('password2');
|
||||||
$validation->required('postcode')->postcode;
|
$validation->required('postcode')->postcode;
|
||||||
|
|
||||||
|
my $user_rs = $c->schema->resultset('User');
|
||||||
|
$validation->required('email')->email->not_in_resultset('email', $user_rs);
|
||||||
|
|
||||||
my $token_rs = $c->schema->resultset('AccountToken')->search_rs({used => 0});
|
my $token_rs = $c->schema->resultset('AccountToken')->search_rs({used => 0});
|
||||||
$validation->required('token')->in_resultset('accounttokenname', $token_rs);
|
$validation->required('token')->in_resultset('accounttokenname', $token_rs);
|
||||||
|
|
||||||
my $age_rs = $c->schema->resultset('AgeRange');
|
my $age_rs = $c->schema->resultset('AgeRange');
|
||||||
$validation->required('agerange')->in_resultset('agerangeid', $age_rs);
|
$validation->required('agerange')->in_resultset('agerangeid', $age_rs);
|
||||||
|
|
||||||
use Devel::Dwarn;
|
my @error_messages;
|
||||||
Dwarn $validation;
|
if ( $validation->has_error ) {
|
||||||
$c->redirect_to('/register');
|
my $failed_vals = $validation->failed;
|
||||||
|
@error_messages = map {$c->error_messages->{ $_ } } @$failed_vals;
|
||||||
|
|
||||||
|
$c->render( template => 'register/index' );
|
||||||
|
} else {
|
||||||
|
my $new_user = $c->schema->resultset('User')->find_or_new({
|
||||||
|
email => $validation->param('email'),
|
||||||
|
hashedpassword => $c->generate_hashed_password( $validation->param('password') ),
|
||||||
|
joindate => DateTime->now(),
|
||||||
|
customer => {
|
||||||
|
username => $validation->param('name'),
|
||||||
|
postcode => $validation->param('postcode'),
|
||||||
|
agerange_fk => $validation->param('agerange'),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if ( $new_user->in_storage ) {
|
||||||
|
@error_messages = ( $c->error_messages->{unknown} );
|
||||||
|
} else {
|
||||||
|
$new_user->insert;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( scalar @error_messages ) {
|
||||||
|
$age_rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
|
||||||
|
$c->stash(
|
||||||
|
error => \@error_messages,
|
||||||
|
ageranges => [ $age_rs->all ],
|
||||||
|
form_data => {
|
||||||
|
name => $validation->param('name'),
|
||||||
|
email => $validation->param('email'),
|
||||||
|
postcode => $validation->param('postcode'),
|
||||||
|
token => $validation->param('token'),
|
||||||
|
agerange => $validation->param('agerange'),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$c->flash( success => 'Registered Successfully, please log in' );
|
||||||
|
$c->redirect_to('/');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
|
@ -17,6 +17,11 @@ sub register {
|
||||||
return $rs->search({ $key => $value })->count ? undef : 1;
|
return $rs->search({ $key => $value })->count ? undef : 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$app->validator->add_check( not_in_resultset => sub {
|
||||||
|
my ( $validation, $name, $value, $key, $rs ) = @_;
|
||||||
|
return $rs->search({ $key => $value })->count ? 1 : undef;
|
||||||
|
});
|
||||||
|
|
||||||
$app->validator->add_check( postcode => sub {
|
$app->validator->add_check( postcode => sub {
|
||||||
my ( $validation, $name, $value ) = @_;
|
my ( $validation, $name, $value ) = @_;
|
||||||
return Geo::UK::Postcode->new( $value )->valid ? undef : 1;
|
return Geo::UK::Postcode->new( $value )->valid ? undef : 1;
|
||||||
|
|
|
@ -20,42 +20,62 @@ body {
|
||||||
% content_for javascript => begin
|
% content_for javascript => begin
|
||||||
% end
|
% end
|
||||||
<div class="container">
|
<div class="container">
|
||||||
% if ( my $error = flash 'error' ) {
|
% if ( defined $error ) {
|
||||||
<div class="alert alert-danger" role="alert">
|
<div class="alert alert-danger" role="alert">
|
||||||
<strong>Error!</strong> <%= $error %>
|
<strong>Error!</strong><br>
|
||||||
</div>
|
% for my $error_single ( @$error ) {
|
||||||
% } elsif ( my $success = flash 'success' ) {
|
<%= $error_single %><br>
|
||||||
<div class="alert alert-success" role="alert">
|
% }
|
||||||
<strong>Success!</strong> <%= $success %>
|
|
||||||
</div>
|
</div>
|
||||||
% }
|
% }
|
||||||
<div class="row justify-content-center">
|
<div class="row justify-content-center">
|
||||||
<!-- Fluid Gird, this login box stay in the middle of screen -->
|
|
||||||
<div class="col-8">
|
<div class="col-8">
|
||||||
|
|
||||||
<div class="register-form panel">
|
<div class="register-form panel">
|
||||||
<form action="<%= url_for %>" method="post">
|
<form action="<%= url_for %>" method="post">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="register-token">Invitation Token</label>
|
<label for="register-token">Invitation Token</label>
|
||||||
<input type="text" id="register-token" class="form-control" name="token" required>
|
<input type="text"
|
||||||
|
id="register-token"
|
||||||
|
class="form-control"
|
||||||
|
name="token"
|
||||||
|
value="<%= $form_data->{token} %>"
|
||||||
|
required>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="register-name">Full Name</label>
|
<label for="register-name">Full Name</label>
|
||||||
<input type="text" id="register-name" class="form-control" name="name" required>
|
<input type="text"
|
||||||
|
id="register-name"
|
||||||
|
class="form-control"
|
||||||
|
name="name"
|
||||||
|
value="<%= $form_data->{name} %>"
|
||||||
|
required>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="register-email">Email Address</label>
|
<label for="register-email">Email Address</label>
|
||||||
<input type="email" id="register-email" class="form-control" name="email" required>
|
<input type="email"
|
||||||
|
id="register-email"
|
||||||
|
class="form-control"
|
||||||
|
name="email"
|
||||||
|
value="<%= $form_data->{email} %>"
|
||||||
|
required>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="register-postcode">Postcode</label>
|
<label for="register-postcode">Postcode</label>
|
||||||
<input type="text" id="register-postcode" class="form-control" name="postcode" required>
|
<input type="text"
|
||||||
|
id="register-postcode"
|
||||||
|
class="form-control"
|
||||||
|
name="postcode"
|
||||||
|
value="<%= $form_data->{postcode} %>"
|
||||||
|
required>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="register-age">Age Range</label>
|
<label for="register-age">Age Range</label>
|
||||||
<select id="register-age" class="form-control" name="agerange">
|
<select id="register-age" class="form-control" name="agerange" required>
|
||||||
% for my $agerange ( @$ageranges ) {
|
% for my $agerange ( @$ageranges ) {
|
||||||
<option value="<%= $agerange->{agerangeid} %>"><%= $agerange->{agerangestring} %></option>
|
<option value="<%= $agerange->{agerangeid} %>"<%= defined $form_data->{agerange} && $form_data->{agerange} eq $agerange->{agerangeid} ? ' selected' : '' %>>
|
||||||
|
<%= $agerange->{agerangestring} %>
|
||||||
|
</option>
|
||||||
% }
|
% }
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -22,7 +22,16 @@ body {
|
||||||
% end
|
% end
|
||||||
% content_for javascript => begin
|
% content_for javascript => begin
|
||||||
% end
|
% end
|
||||||
<div class="container-fluid">
|
<div class="container">
|
||||||
|
% if ( my $error = flash 'error' ) {
|
||||||
|
<div class="alert alert-danger" role="alert">
|
||||||
|
<strong>Error!</strong> <%= $error %>
|
||||||
|
</div>
|
||||||
|
% } elsif ( my $success = flash 'success' ) {
|
||||||
|
<div class="alert alert-success" role="alert">
|
||||||
|
<strong>Success!</strong> <%= $success %>
|
||||||
|
</div>
|
||||||
|
% }
|
||||||
<div class="row justify-content-center">
|
<div class="row justify-content-center">
|
||||||
<!-- Fluid Gird, this login box stay in the middle of screen -->
|
<!-- Fluid Gird, this login box stay in the middle of screen -->
|
||||||
<div class="col-5">
|
<div class="col-5">
|
||||||
|
|
Reference in a new issue