Added validation on Token adding and editing

This commit is contained in:
Tom Bloor 2017-04-08 15:10:05 +01:00
parent ea899d955c
commit cb9819aa2e

View file

@ -17,12 +17,17 @@ sub index {
# POST # POST
sub create { sub create {
my $c = shift; my $c = shift;
my $validation = $c->validation;
$validation->required('token-name', 'trim')->like(qr/^[\w]*$/);
my $token_name = $c->param('token-name'); my $token_name = $validation->param('token-name');
my $token_rs = $c->result_set; my $token_rs = $c->result_set;
if ( $token_rs->find({ accounttokenname => $token_name }) ) { if ( $validation->has_error ) {
# Only one validator, fairly obvious whats broke
$c->flash( error => 'Token name not valid - Alphanumeric characters and Underscore only' );
} elsif ( $token_rs->find({ accounttokenname => $token_name }) ) {
$c->flash( error => 'Token Already Exists' ); $c->flash( error => 'Token Already Exists' );
} else { } else {
$c->flash( success => 'Token Created' ); $c->flash( success => 'Token Created' );
@ -48,13 +53,20 @@ sub read {
# POST # POST
sub update { sub update {
my $c = shift; my $c = shift;
my $validation = $c->validation;
$validation->required('token-name', 'trim')->like(qr/^[\w]*$/);
$validation->required('token-used')->in( qw/ 0 1 / );
my $id = $c->param('id'); my $id = $c->param('id');
if ( my $token = $c->result_set->find($id) ) { if ( $validation->has_error ) {
my $names = $validation->failed;
$c->flash( error => 'Error in submitted data: ' . join(', ', @$names) );
$c->redirect_to( '/admin/tokens/' . $id );
} elsif ( my $token = $c->result_set->find($id) ) {
$token->update({ $token->update({
accounttokenname => $c->param('token-name'), accounttokenname => $validation->param('token-name'),
used => $c->param('token-used'), used => $validation->param('token-used'),
}); });
$c->flash( success => 'Token Updated' ); $c->flash( success => 'Token Updated' );
$c->redirect_to( '/admin/tokens/' . $id ); $c->redirect_to( '/admin/tokens/' . $id );